Documentation
Quickstart
From nothing to a working request. Space Router follows the OpenAI API format, so if you have used that, most of this will look familiar.
1. Get a key
Create an account, add credits and create an API key. Keep the key out of your source code: put it in an environment variable, here called SPACEROUTER_API_KEY.
Every request carries the key in an Authorization: Bearer header. The base URL for all requests is https://api.spacerouter.ai/v1.
2. Send a request
A chat completion takes a model ID and a list of messages, and returns the model’s reply.
curl https://api.spacerouter.ai/v1/chat/completions \
-H "Authorization: Bearer $SPACEROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4.6",
"messages": [
{"role": "user", "content": "Explain what an API gateway does in one sentence."}
]
}'The reply is in choices[0].message.content. The usage object reports how many tokens the request consumed, which is what you are billed for.
3. Use an OpenAI SDK
You do not need a new client library. Point the official OpenAI SDK at Space Router by setting the base URL and the key.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.spacerouter.ai/v1",
api_key=os.environ["SPACEROUTER_API_KEY"],
)
reply = client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[{"role": "user", "content": "Hello"}],
)
print(reply.choices[0].message.content)Moving an existing application is the same two changes. See integrations for other tools.
4. Choose a model
The model field takes a model ID such as anthropic/claude-sonnet-4.6. The models page lists every ID with its context length and price, and compare puts models side by side.
To switch models, change that one string. The endpoint, the key and the response format stay the same.
You can also list the models your key may use:
curl https://api.spacerouter.ai/v1/models \
-H "Authorization: Bearer $SPACEROUTER_API_KEY"5. Stream the response
Set stream to true to receive the reply piece by piece as it is written, which makes an interface feel faster. These examples continue from the client created in step 3.
stream = client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[{"role": "user", "content": "Write a haiku about routing."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)6. Handle errors
Errors come back as an HTTP status code with a JSON body containing an error object and a message.
| Status | What it means | What to do |
|---|---|---|
| 400 | The request was malformed, or the model does not accept one of the parameters. | Fix the request; retrying will not help. |
| 401 | The key is missing, mistyped or revoked. | Check the Authorization header. |
| 403 | The key is not allowed to call this model. | Use a permitted model or change the key's model list. |
| 404 | No model has that ID. | Check the ID on the models page. |
| 429 | A rate limit was reached, or a budget has been used up. | Wait and retry with increasing delays; if a budget is spent, add credits or raise it. |
| 5xx | The model provider or the gateway had a problem. | Retry after a short delay. |
Retry only on 429 and 5xx, with a growing delay between attempts and a cap on the number of tries.
7. Budgets and limits
Each key can carry its own budget, rate limit and list of permitted models. Use a separate key for each environment or customer, so usage is reported separately and a problem with one cannot exhaust the others.
When a budget or your balance is used up, requests are refused until you add credits or raise the limit. Nothing is charged beyond what you loaded.
Questions? See support.