All AI inference requests to ModelSwitch require a Bearer token in the Authorization header:
Authorization: Bearer ms-xxxxxxxx
Keys are created in Dashboard → API Keys. Each key starts with the ms- prefix and is hashed before storage — the full value is only shown once at creation.
Copy your API key immediately after creating it. Once you close the creation dialog, it cannot be retrieved. If you lose a key, deactivate it and create a new one.
Key types
ModelSwitch uses two types of API keys for different purposes:
| Type | Prefix | Used for |
|---|
| Proxy key | ms- | AI inference requests (/v1/*) |
| Billing key | mk- | Account management API (/api/*) |
For sending AI requests — chat completions, embeddings, model listings — use a proxy key (ms-). For programmatic access to your account (balance, usage, transactions), use a billing key (mk-).
See API key management for details on creating, rotating, and deactivating keys.
Authenticating a request
Pass your ms- key as a Bearer token:
curl https://modelswitch.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ms-YOUR_KEY" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Store your API key in an environment variable (e.g., MODELSWITCH_API_KEY) and read it at runtime. Never hardcode keys in source code or commit them to version control.
Error responses
Authentication and authorization failures return a JSON error body:
{
"error": {
"message": "Missing or invalid Authorization header",
"type": "authentication_error",
"code": "invalid_api_key"
}
}
| Status | When it occurs |
|---|
401 Unauthorized | Authorization header is missing, malformed, or the key is invalid or expired |
403 Forbidden | Key is deactivated or its spend limit has been reached |
402 Payment Required | Account balance is insufficient to process the request |
Top up your balance in Dashboard → Billing if you receive a 402 response.
Validate your key
Check that your key is valid and see your current balance limit with one request:
curl https://modelswitch.io/v1/auth/key \
-H "Authorization: Bearer ms-YOUR_KEY"
Response:
{
"data": {
"label": "My Production Key",
"usage": 0,
"limit": 1500.50,
"is_free_tier": false,
"rate_limit": {
"requests": 1000,
"interval": "10s"
}
}
}
The limit field reflects your current account balance in USD. This endpoint is useful for verifying a key is active before deploying it to production.