Base address
Anar gives you the Gateway address during deployment. In the examples, https://gateway.example.gov stands for that address.
Authentication
Send a virtual key in the x-anar-vk header, or a token from your identity provider in the Authorization header:
x-anar-vk: <virtual-key>
Authorization: Bearer <token>
Read Access control.
Endpoints
The Gateway accepts the OpenAI request and response format on these endpoints.
| Method | Path | Purpose |
|---|---|---|
| GET | /v1/models | List the models that you can call |
| POST | /v1/chat/completions | Chat completion. Supports streaming. |
| POST | /v1/completions | Text completion |
| POST | /v1/responses | Responses format |
| POST | /v1/embeddings | Embeddings |
| POST | /v1/audio/transcriptions | Speech to text |
| POST | /v1/audio/speech | Text to speech |
| POST | /v1/images/generations | Image generation |
| POST | /v1/responses/input_tokens | Count the tokens of an input |
| POST, GET | /v1/batches | Create and list batch jobs |
| POST, GET | /v1/files | Upload and list files for batch jobs |
| DELETE | /v1/files/{file_id} | Delete a file |
An endpoint works only when the selected provider supports that kind of request.
Chat completion
Request
curl https://gateway.example.gov/v1/chat/completions \
-H "x-anar-vk: $VIRTUAL_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "provider/your-model",
"messages": [
{"role": "system", "content": "Answer in Arabic."},
{"role": "user", "content": "Summarise the attached policy."}
]
}'
Response
{
"id": "chatcmpl-...",
"object": "chat.completion",
"model": "your-model",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "..."},
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 42, "completion_tokens": 128, "total_tokens": 170}
}
Gateway fields
The Gateway adds one optional field to the request body:
| Field | Type | Meaning |
|---|---|---|
fallbacks | array of strings | provider/model targets to try in order when the first call fails |
Use an OpenAI client library
Set the base address to the Gateway. Send your virtual key in the x-anar-vk header.
from openai import OpenAI
client = OpenAI(
base_url="https://gateway.example.gov/v1",
api_key="<token>", # your Gateway role credential, if your deployment uses Gateway roles
default_headers={"x-anar-vk": "<virtual-key>"},
)
reply = client.chat.completions.create(
model="provider/your-model",
messages=[{"role": "user", "content": "مرحبا"}],
)
print(reply.choices[0].message.content)
Streaming
Set "stream": true. The Gateway returns server-sent events in the OpenAI format. The stream ends with data: [DONE].
Errors
The Gateway returns errors in this format:
{
"is_anar_error": true,
"status_code": 402,
"type": "budget_exceeded",
"error": {"message": "Budget exceeded: ..."}
}
| Status | Type | Cause | What to do |
|---|---|---|---|
| 400 | The request is not valid | Correct the request body | |
| 401 | The key or token is missing or not valid | Send a valid virtual key or token | |
| 402 | budget_exceeded | A budget is used up | Wait for the budget to reset, or ask your administrator to raise it |
| 403 | access_blocked, model_blocked, provider_blocked | The key cannot make this request | Ask your administrator for access |
| 429 | rate_limited, token_limited, request_limited | A rate limit is reached | Wait and try again |
| 5xx | The provider failed | Try again. Add fallbacks to the request. |