Documentation menu

Client tokens

Let a browser or mobile app call the gateway directly for one customer, without a key.

A key belongs on your server. When your web or mobile app should call the gateway itself, for instance to stream an answer straight to the browser, your server mints a client token and hands it to the app. A token:

  • lasts minutes (10 by default, at most an hour, never longer than the key that minted it);
  • acts for one customer, and optionally one member, which the app cannot change;
  • can only narrow what the minting key may do: fewer models, specific browser origins.

The customer's wallet, limits and model allowlist apply to every request made with it, exactly as for a request your server makes with the key and those headers.

Mint a token on your server#

POST /v1/client_tokens on the gateway, with a key that has the inference scope:

curl https://api.hixel.dev/v1/client_tokens \
  -H "Authorization: Bearer $HIXEL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customer": "org_42",
    "member": "user_7",
    "expires_in": 600,
    "models": ["claude-sonnet-5-5"],
    "allowed_origins": ["https://app.example.com"]
  }'
{
  "object": "client_token",
  "token": "hx_ct_test_eyJ2Ijox….N2Rk…",
  "expires_at": 1795601400,
  "customer": "org_42",
  "member": "user_7"
}
FieldRules
customerYour id for the customer. Required, unless the key is pinned to a customer (then left out or equal to it). A blocked customer gets no token.
memberOptional. Your id for the member; the token then acts for that member only.
expires_inSeconds, 30 to 3600. Default 600.
modelsOptional. 1 to 16 model names (or aliases) the token may call. Left out: whatever the key may call.
allowed_originsOptional. 1 to 8 origins as a browser sends them: https://host[:port], lowercase, no path, no wildcard; http:// only for localhost, 127.0.0.1 and [::1]. Left out: no check.

expires_at is in Unix seconds. The token starts with hx_ct_ (hx_ct_test_ from a test key). It is shown once and never logged; mint a new one when it expires. A client token cannot mint another.

Use it from the app#

The app sends the token where it would send a key: Authorization: Bearer hx_ct_…, or x-api-key. The official SDKs work in the browser with it:

import OpenAI from "openai";

// `token` came from your server.
const client = new OpenAI({
  apiKey: token,
  baseURL: "https://api.hixel.dev/v1",
  dangerouslyAllowBrowser: true, // safe here: the token is short-lived and scoped
});

const stream = await client.responses.create({
  model: "claude-sonnet-5-5",
  input: "Draft a reply to this email.",
  stream: true,
});

The app does not need to send x-hixel-customer or x-hixel-member: the token carries them. If it does send them, they must match the token's.

A token works on every inference route its key may use (the three chat formats, the endpoints beside chat, GET /v1/models, which then lists only the token's models). It does not work on the management API.

Browsers and CORS#

The gateway answers CORS preflights on its inference routes and adds the CORS headers to a response only when the request presented a client token. A request with a secret key gets none, so a key pasted into a web page does not work from a browser by accident. With allowed_origins, a request whose Origin is missing or not listed is refused.

Revoking#

A token cannot be revoked on its own. Revoking or expiring the key that minted it ends all of its tokens. Keep expires_in short.

Failures#

StatuscodeWhen
401client_token_invalidMalformed, altered, or minted by a key that is revoked
401client_token_expiredPast expires_at, or its key expired
403client_token_customer_mismatchThe request names another customer than the token
403client_token_member_mismatchThe request names another member than the token
403client_token_origin_not_allowedOrigin missing or not in allowed_origins
403client_token_model_not_allowedA model the token's models does not list
403client_token_cannot_mintMinting with a client token instead of a key
400invalid_parameterA minting field out of its rules; param names it