Create a key
You create a key in the API dashboard. It starts withoc_sk_. Copy it when it appears, and keep it on your server.
Where the key goes
You can pass the key in either of these headers. One of them is enough.Authorization and x-api-key contain a value, the API rejects the request with HTTP 401 and Ambiguous credentials. On Chat Completions, models, and Files, the error code is invalid_credential. This also applies when both headers contain the same key.
Try the key with the models endpoint. This example needs the models:read scope and does not spend inference usage.
object: "list" and a data array of model objects. The key’s model allowlist determines which models appear.
Scopes
Each key carries a set of scopes. A request goes through when the key includes the scope for that operation.
When you create a key, enable the scopes needed by your application. A key used for both inference APIs needs both
chat:completions and messages:create. Server-side web search and web fetch use the scope of the inference endpoint; they do not need an extra web-tool scope.
To upload a PDF, ask a model about it by file ID, and delete it afterward, enable files:write, files:read, and files:delete, plus the relevant inference scope. Referring to an existing file ID needs files:read, even when you do not download the file yourself.
If a required scope is missing, you’ll get HTTP 403 and the message Forbidden.
Chat Completions, the models list, and Files return this body. The response includes X-Request-Id.
request-id, and the same identifier inside the JSON as request_id.
Model allowlist
Some keys are limited to a list of model IDs. A key with no list can call every model on the models page, andGET /v1/models returns that full list. A key with a list only sees the models on it. Changing the model field in a request does not add that model to the key’s permissions.
If you call Chat Completions with a model the key is not allowed to use, you’ll get HTTP 403:
When the key is not accepted
You’ll get HTTP 401 and the messageInvalid API key if the key is missing, the Authorization value is malformed, or the key is not recognized or is no longer valid.
On Chat Completions, the models list, and Files:
X-Request-Id header. On Messages, request-id and request_id are that same value.
