Clatter LabsHelp Center

How do I call the Model API with Python's requests library?

How-toUpdated
import os, requests

r = requests.post(
    "https://api.gateway.clatterlabs.xyz/v1/chat/completions",
    headers={
        "Authorization": f"Bearer {os.environ['CLATTER_API_KEY']}",
        "X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"],
    },
    json={"model": "supercosmos-flash", "messages": [{"role": "user", "content": "Hello!"}]},
    timeout=120,
)
data = r.json()
if r.status_code != 200:
    raise RuntimeError(f"{r.status_code} {data['error']['code']}: {data['error']['message']}")
print(data["choices"][0]["message"]["content"], r.headers.get("x-request-id"))

json= sets Content-Type: application/json for you. For streaming, the OpenAI SDK is simpler.

Good to know#

  • Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits
  • Every call shows in Console › Logs and Usage as Model API, with its price. See what you spent
  • New accounts get $5 of free credit; top up from $1 to $800 per purchase in Console › Billing › Credits. Billing

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.