Skip to main content
A
Docs

Gateway API reference

The Gateway inference API: endpoints, authentication, request and response examples, streaming and errors. It accepts the OpenAI request format.

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.

MethodPathPurpose
GET/v1/modelsList the models that you can call
POST/v1/chat/completionsChat completion. Supports streaming.
POST/v1/completionsText completion
POST/v1/responsesResponses format
POST/v1/embeddingsEmbeddings
POST/v1/audio/transcriptionsSpeech to text
POST/v1/audio/speechText to speech
POST/v1/images/generationsImage generation
POST/v1/responses/input_tokensCount the tokens of an input
POST, GET/v1/batchesCreate and list batch jobs
POST, GET/v1/filesUpload 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:

FieldTypeMeaning
fallbacksarray of stringsprovider/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: ..."}
}
StatusTypeCauseWhat to do
400The request is not validCorrect the request body
401The key or token is missing or not validSend a valid virtual key or token
402budget_exceededA budget is used upWait for the budget to reset, or ask your administrator to raise it
403access_blocked, model_blocked, provider_blockedThe key cannot make this requestAsk your administrator for access
429rate_limited, token_limited, request_limitedA rate limit is reachedWait and try again
5xxThe provider failedTry again. Add fallbacks to the request.