Authentication and API Keys
Call every endpoint with a gk_ API key — the Authorization Bearer header, capability scopes, per-key access restrictions, and 401 / 403 authentication errors.
Every /v1/* endpoint authenticates with an API key. Keys start with gk_ and are created on the API Keys page of your region's console (China region https://cn.inf.space, international region https://ai.inf.space). A key is shown in full only once, at creation — save it immediately; afterwards the console shows only its prefix.
Sending the Key
Pass the key in the Authorization header with the Bearer scheme:
Authorization: Bearer gk_xxxxxxxxxxxxxx# Global region; China region is https://cn.inf.space (accelerated) or https://global.inf.space (international)
curl "https://ai.inf.space/v1/chat/completions" \
-H "Authorization: Bearer $INFERENCE_SPACE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"messages": [{ "role": "user", "content": "Hello" }]
}'import os
import requests
resp = requests.post(
# Global region; China region is https://cn.inf.space (accelerated) or https://global.inf.space (international)
"https://ai.inf.space/v1/chat/completions",
headers={"Authorization": f"Bearer {os.environ['INFERENCE_SPACE_API_KEY']}"},
json={
"model": "gpt-5.6-sol",
"messages": [{"role": "user", "content": "Hello"}],
},
timeout=600,
)
print(resp.status_code, resp.json())// Global region; China region is https://cn.inf.space (accelerated) or https://global.inf.space (international)
const resp = await fetch("https://ai.inf.space/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.INFERENCE_SPACE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-5.6-sol",
messages: [{ role: "user", content: "Hello" }],
}),
});
console.log(resp.status, await resp.json());- The
Bearerprefix is required; a bareAuthorization: gk_...is treated as no key at all. - The
x-api-key: gk_...header used by the Anthropic SDK / Claude Code also works; if both headers are present,Authorizationwins. - With the OpenAI or Anthropic SDK, just set the SDK's
api_key; the SDK puts it in the right header. - A key only works in its own region: China-region keys use
cn.inf.space/global.inf.space, international-region keys useai.inf.space.
Capability Scopes
Each key is granted "capabilities" in the console, each mapping to a scope. To call an endpoint, the key must hold that endpoint's scope (or the wildcard ai:*).
| Console capability | Scope | Endpoints |
|---|---|---|
| LLM | ai:llm | /v1/chat/completions, /v1/messages, /v1/responses |
| Image generation | ai:image | /v1/images/generations, /v1/images/edits, and async image jobs |
| Video generation | ai:video | Video generation and query endpoints; see Enterprise Video |
| Speech recognition | ai:asr | /v1/audio/transcriptions |
| Speech synthesis | ai:tts | /v1/audio/speech |
| Text recognition | ai:ocr | /v1/recognize |
| Detection & segmentation | ai:vision-segment | /v1/vision-segment/predictions, /v1/vision-segment/video, and result file downloads |
| All capabilities | ai:* | Everything above |
Grant the least privilege: a chat-only app needs only "LLM", an image-only app only "Image generation". Use separate keys per application so you can audit, limit, and revoke them independently.
Per-Key Access Restrictions
Beyond scopes, the console can put the following restrictions on each key. A request that violates one gets 403:
| Restriction | Effect | Error when hit |
|---|---|---|
| Model scope | Only the listed models may be called (supports * wildcards, e.g. gpt-image-*); applies to LLM, image, and video endpoints | LLM: model_not_allowed_for_api_key; image: image_model_not_allowed |
| Image size tiers | Only the listed image size tiers (e.g. 1K / 2K) are allowed; if exactly one tier is allowed, requests without size use it automatically | image_size_not_allowed |
| IP allowlist | Only the listed IPs / CIDRs may call | ip_not_allowed |
| Expiry | The key stops working after it expires | Same as an invalid key (401) |
Authentication Errors
Authentication failures return 401 or 403 with a JSON body. The body shape differs slightly by endpoint family:
Image, speech, OCR, vision segmentation, video, and similar endpoints: the error code is in error.type.
| HTTP | error.type | Meaning |
|---|---|---|
401 | missing_api_key | No key sent, or the Authorization header is not Bearer gk_... |
401 | invalid_api_key | The key does not exist, or is revoked, disabled, or expired |
403 | insufficient_scope | The key is valid but lacks the scope this endpoint requires |
403 | ip_not_allowed | The source IP is not on the key's allowlist |
{
"error": {
"message": "API key lacks scope for /v1/images/generations",
"type": "insufficient_scope"
}
}Chat endpoints (/v1/chat/completions, /v1/responses):
| HTTP | Error code | Meaning |
|---|---|---|
401 | error.type = missing_api_key | No key sent |
401 | error.code = authentication_required | Key invalid, revoked / expired, or missing the ai:llm scope |
403 | error.type = ip_not_allowed | Source IP not on the allowlist |
{
"error": {
"code": "authentication_required",
"message": "无法识别 API 密钥,请检查认证信息后重试。"
}
}/v1/messages (native Anthropic protocol): an invalid key or missing scope returns the Anthropic format {"type": "error", "error": {"type": "authentication_error", "message": "..."}}.
Chat endpoints do not distinguish "invalid key" from "key lacks ai:llm" — both are 401. If you are sure the key is correct but keep getting 401, check in the console that the key has the "LLM" capability.
Troubleshooting:
missing_api_key: make sure the request carriesAuthorization: Bearer gk_...with one space betweenBearerand the key, and that your environment variable is actually loaded.invalid_api_key/authentication_required: check that the key is not mistyped, expired, or revoked, and that it belongs to the region you are calling; for chat endpoints also check that it hasai:llm.insufficient_scope: add the capability to the key in the console, or use a key with sufficient permissions.ip_not_allowed: the error message includes the source IP the gateway saw; add it to the key's IP allowlist.
Recommendations
- Keep keys on the server side; never ship them to browsers, mobile apps, or frontend code.
- If a key may have leaked, revoke it in the console immediately and create a new one.
- Avoid sharing one
ai:*key long-term.
Regions and Network Routes
How to pick a region (China / Global) and, within it, the right route domain — cn.inf.space, global.inf.space, or ai.inf.space.
NewAPI Compatibility
Model discovery, audio aliases, balance, request log, and daily consumption endpoints you can keep using when migrating from NewAPI.