Your First Request
A step-by-step guide to making your first chat completion request with AllRoutes.
Overview
This guide walks you through making your first API call to AllRoutes, from getting your API key to handling the response and checking your usage.
Step 1: Get Your API Key
- Sign up or log in at allroutes.ai/dashboard
- Go to Settings > API Keys
- Click Create New Key, give it a name, and copy the key
- Store it securely as an environment variable:
export ALLROUTES_API_KEY="allroutes_sk_..."
Step 2: Install Your SDK
Choose the SDK for your language:
# Python (requires 3.8+)
pip install allroutes
# Node.js / TypeScript (requires Node 18+)
npm install @allroutes/sdk
# Go (requires 1.21+)
go get github.com/allroutes-ai/allroutes-go/allroutes
Step 3: Send a Completion Request
Python
import os
from allroutes import AllRoutesClient
client = AllRoutesClient(api_key=os.environ["ALLROUTES_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What are the three laws of thermodynamics?"},
],
temperature=0.7,
max_tokens=500,
)
print(response.content)
Node.js
import AllRoutes from "@allroutes/sdk";
const client = new AllRoutes({ apiKey: process.env.ALLROUTES_API_KEY });
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What are the three laws of thermodynamics?" },
],
temperature: 0.7,
max_tokens: 500,
});
console.log(completion.choices[0].message.content);
cURL
curl https://api.allroutes.ai/v1/chat/completions \
-H "Authorization: Bearer $ALLROUTES_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What are the three laws of thermodynamics?"}
],
"temperature": 0.7,
"max_tokens": 500
}'
Step 4: Handle the Response
The API returns an OpenAI-compatible JSON response:
{
"id": "chatcmpl-abc123def456",
"object": "chat.completion",
"created": 1714000000,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The three laws of thermodynamics are..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 28,
"completion_tokens": 156,
"total_tokens": 184
}
}
Response Fields
| Field | Description |
|---|---|
id | Unique identifier for the completion |
model | The model that generated the response |
choices[].message.content | The generated text |
choices[].finish_reason | Why generation stopped: stop, length, tool_calls |
usage.prompt_tokens | Number of tokens in the input |
usage.completion_tokens | Number of tokens generated |
usage.total_tokens | Total tokens consumed |
Step 5: Check Usage and Cost
After making requests, check your credit balance and usage:
Python
credits = client.get_credits()
print(f"Balance: ${credits['balance_usd']:.2f} USD")
print(f"Lifetime spend: ${credits['lifetime_spend_usd']:.2f} USD")
Node.js
const credits = await client.getCredits();
console.log(`Balance: $${credits.balance_usd.toFixed(2)} USD`);
cURL
curl https://api.allroutes.ai/v1/credits \
-H "Authorization: Bearer $ALLROUTES_API_KEY"
Error Handling
Always wrap API calls in error handling:
import httpx
from allroutes import AllRoutesClient
client = AllRoutesClient(api_key="allroutes_sk_...")
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
except httpx.HTTPStatusError as e:
if e.response.status_code == 401:
print("Invalid API key")
elif e.response.status_code == 429:
print("Rate limit exceeded, try again later")
elif e.response.status_code == 402:
print("Insufficient credits")
else:
print(f"API error {e.response.status_code}: {e.response.text}")
except httpx.TransportError as e:
print(f"Network error: {e}")
The SDKs automatically retry on transient errors (5xx and 429) with exponential backoff.
See Also
- Chat Completions API -- full parameter reference
- Streaming -- SSE protocol and chunk format
- Smart Routing -- automatic provider selection
- Error Handling -- production-ready retry and fallback patterns
- Python SDK -- complete Python SDK reference