AI Gateway: chat requests through one API
How to send chat requests through AI Gateway, in the OpenAI Chat Completions format, with an hzx_ key and usage drawn from your plan's quota.
Last updated:
AI Gateway accepts chat requests in the OpenAI Chat Completions format, authenticated with an hzx_ key. Usage is drawn from your plan’s quota and you can see it, request by request, in the console.
The examples below use the $HAZELOFT_BASE_URL variable in place of the gateway address. The address ends in /v1. This page does not print it; if you do not have it yet, write to us through the contact page.
What you need
- An account on a plan that includes AI Gateway. The minimum plan is on the product page.
- An API key. In the AI Gateway section of the platform, open “API keys”, press “Create API key” and give it a name. The secret is shown once, in the “Save your key” dialog; the platform keeps only its hash and its last four characters.
A lost key cannot be recovered: you replace it with a new one.
Authentication
Every request carries the key in the Authorization header:
Authorization: Bearer hzx_...
A key means full access: there are no per-key permissions, per-key budgets or model restrictions. Whoever holds the key spends your quota, so keep it on the server and never put it in code that reaches the browser.
HAZELOFT_BASE_URL=... # the gateway address, ending in /v1
HAZELOFT_API_KEY=hzx_...
Endpoints
The gateway has four endpoints, all under /v1. All of them answer in JSON; only chat can also answer as a stream.
| Method | Path | What it does |
|---|---|---|
POST |
/v1/chat/completions |
A conversation with a model, with or without streaming. |
GET |
/v1/models |
The models and modes your account can use. |
POST |
/v1/search |
Web search. Takes query (at most 400 characters) and returns results: at most 6 entries, each with title, url and snippet. |
POST |
/v1/transcribe |
Audio to text. Takes audio in base64 (at most 20 MiB decoded) and mediaType; returns text, language and durationInSeconds. |
/v1/search and /v1/transcribe were built for the Hazeloft apps and have their own format. They accept the same key.
Your first request
curl "$HAZELOFT_BASE_URL/chat/completions" \
-H "Authorization: Bearer $HAZELOFT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [{ "role": "user", "content": "Hello" }]
}'
The same call from Node, with fetch:
const response = await fetch(`${process.env.HAZELOFT_BASE_URL}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.HAZELOFT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "auto",
messages: [{ role: "user", content: "Hello" }],
}),
});
const data = await response.json();
console.log(data.choices[0].message.content);
The response is a chat.completion object. The values below are illustrative:
{
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "Hello!" },
"finish_reason": "stop"
}
]
}
Models and modes
In the model field you write a model id or a mode: auto, fast or extended.
GET /v1/models returns your account’s list: one row per model, with id, hazeloft_label, hazeloft_default, hazeloft_allowed and, where applicable, hazeloft_min_plan and hazeloft_reasoning_efforts. The modes available to the account come in hazeloft_chat_tiers.
On a mode, the server picks the model and the reasoning effort, and a reasoning_effort you send is not applied. This page does not name models, because the list changes with the platform’s configuration.
Streaming
With "stream": true, the response arrives as server-sent events: chat.completion.chunk frames, then [DONE]. With stream_options.include_usage, the last frame before [DONE] carries the token counts.
const response = await fetch(`${process.env.HAZELOFT_BASE_URL}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.HAZELOFT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "auto",
messages: [{ role: "user", content: "Count from 1 to 5." }],
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() ?? "";
for (const line of lines) {
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
if (data === "[DONE]") continue;
const chunk = JSON.parse(data);
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
}
Chat request fields
The chat endpoint applies a subset of the OpenAI Chat Completions format:
| Field | Notes |
|---|---|
model |
A model id from /v1/models, or a mode. |
messages |
The conversation messages. A user message can contain text and image_url parts. |
stream, stream_options |
Streamed response; see above. |
max_tokens, max_completion_tokens |
The response limit. Lowered to the model’s limit when that is smaller. |
temperature |
Between 0 and 2. |
tools, tool_choice |
Tool calls. You send results back as messages with the tool role. When the model asks for a tool, finish_reason is tool_calls. |
reasoning_effort |
none, minimal, low, medium, high, xhigh or max. Reasoning, when the model emits it, comes in reasoning_content. |
Images travel inside the request, as a base64 data: URL. http addresses are not accepted.
Fields outside this list are ignored, not rejected: response_format, top_p and others pass with no error and no effect. Do not rely on structured output.
Errors
Every error has the shape { "error": { "message", "type", "code" } }.
| Status | When |
|---|---|
| 400 | Invalid request. The message names the wrong fields. |
| 401 | Invalid or deleted key. |
| 403 | The account’s plan does not include AI Gateway. |
| 429 | The plan’s quota is used up (code usage_limit_exceeded); the model is not called. |
The gateway checks the plan on every request: if you leave the plan that includes it, your keys stop working.
Usage
There is no per-token price and no usage to buy on top of the plan’s. The quota has two windows, 5 hours and 7 days, which refresh on their own.
The “Usage” page in the platform shows requests, tokens, cost and response times, broken down by “Models” or by “API keys”, in the “Summary” or “Log” view. It covers the most recent 5,000 requests in the chosen range.
Limits
The limits of a request (body size, number of messages and tools, rate limit) come from the platform’s configuration and are shown, up to date, on the product page, under “Specifications”.
What the gateway does not do: it does not generate images, video or voice, and it has no embeddings endpoint.