OpenAI-Compatible API
AkaRouter provides an OpenAI-compatible API. If your SDK supports a custom baseURL, you usually only need to replace the Base URL and API key.
Base URL
Section titled “Base URL”https://api.akarouter.com/v1Do not use the console URL, documentation URL, payment URL, or a full endpoint path as the API Base URL.
Node.js
Section titled “Node.js”import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.AKAROUTER_API_KEY, baseURL: "https://api.akarouter.com/v1"});
const completion = await client.chat.completions.create({ model: process.env.AKAROUTER_MODEL, messages: [{ role: "user", content: "Give me one concise onboarding tip." }]});
console.log(completion.choices[0]?.message?.content);Read AKAROUTER_API_KEY and AKAROUTER_MODEL from server-side environment variables. Do not hard-code them in source code.
Python
Section titled “Python”import osfrom openai import OpenAI
client = OpenAI( api_key=os.environ["AKAROUTER_API_KEY"], base_url="https://api.akarouter.com/v1",)
completion = client.chat.completions.create( model=os.environ["AKAROUTER_MODEL"], messages=[{"role": "user", "content": "Give me one concise onboarding tip."}],)
print(completion.choices[0].message.content)curl https://api.akarouter.com/v1/chat/completions \ -H "Authorization: Bearer $AKAROUTER_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "'"$AKAROUTER_MODEL"'", "messages": [{ "role": "user", "content": "ping" }] }'Integration Tips
Section titled “Integration Tips”- Read
AKAROUTER_API_KEYon the server side. Do not expose it in the browser. - Use the model names shown on the Model Status page in the console.
- Retry timeouts briefly, but do not retry forever.
- Use exponential backoff for 429, 502, 503, and 504 responses.
- For 401, 402, and 403 responses, check your key, balance, and account access before retrying.
Reading the Response
Section titled “Reading the Response”A successful chat completion response includes choices. With the OpenAI SDK, the content is usually available here:
completion.choices[0]?.message?.contentIf the response is not 200, follow the status-code guidance in Common Errors.