Docs · Management API

Manage keys
from your own code.

Give each customer, service or teammate their own key with its own limits, rotate keys on a schedule and pull spend into your reports. Requests use a management token, not an API key.

Authentication

An owner or admin creates a management token in Dashboard → Developers. It starts with th_mgmt_, acts for one workspace with admin rights over its keys, and is either read-only or read and write. It can’t call models, and API keys can’t manage anything. Send it as a bearer token:

Header
Authorization: Bearer th_mgmt_…

Base URL: https://tokenhaven.ai/api/v1. Errors use the same shape as the model API: {"error": {"message", "type", "code"}}.

Endpoints

GET/keysList keys, with lifetime and this month's spend. ?tag= filters by project tag.
POST/keysCreate a key. The response holds the secret, once.(write)
GET/keys/{id}One key, with its settings and spend.
PATCH/keys/{id}Change only the fields you send. status: "active" or "disabled".(write)
POST/keys/{id}/rotateNew secret, same key. grace_hours: 0, 24 (default) or 72.(write)
DELETE/keys/{id}Delete a key. It stops working at once; its usage is kept.(write)
GET/usageSpend by day and by model. ?days=1–365 (default 30), ?key_id=.
GET/balanceThe workspace's prepaid credit in USD.

Create a key

curl
curl https://tokenhaven.ai/api/v1/keys \
  -H "Authorization: Bearer $TOKENHAVEN_MANAGEMENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name": "customer-42", "tag": "acme", "monthly_cap_usd": 50}'
Response · 201
{
  "object": "api_key",
  "id": "5f0c…",
  "name": "customer-42",
  "tag": "acme",
  "status": "active",
  "key_hint": "sk-th-v1-3a9f…c21d",
  "expires_at": null,
  "settings": { "model_limits": [], "monthly_cap_usd": 50, "total_cap_usd": null, "total_mode": "hard", "allow_ips": [], "qps": 0 },
  "key": "sk-th-v1-…"
}

Store key straight away: it isn’t returned again.

Key settings

name
Required when creating. Up to 50 characters.
tag
Project tag for grouping and filtering, up to 32 characters.
expires_at
ISO 8601 time, or null for never. Or send expires_in_days (1–3650).
model_limits
Model ids the key may call, e.g. ["openai/gpt-4.1"]. Empty = every model.
monthly_cap_usd
Most the key may spend per calendar month (UTC). null = no limit.
total_cap_usd
Most the key may ever spend. null = no limit.
total_mode
"hard" stops the key at a limit; "soft" lets a request in flight finish.
allow_ips
Up to 20 IPv4/IPv6 addresses or CIDR ranges. Empty = any address.
qps
Requests per second. 0 = no limit.

Change, rotate, disable

curl
# Raise the monthly limit
curl -X PATCH https://tokenhaven.ai/api/v1/keys/$KEY_ID \
  -H "Authorization: Bearer $TOKENHAVEN_MANAGEMENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"monthly_cap_usd": 100}'

# New secret; the old one works for 24 more hours
curl -X POST https://tokenhaven.ai/api/v1/keys/$KEY_ID/rotate \
  -H "Authorization: Bearer $TOKENHAVEN_MANAGEMENT_TOKEN"

# Turn the key off
curl -X PATCH https://tokenhaven.ai/api/v1/keys/$KEY_ID \
  -H "Authorization: Bearer $TOKENHAVEN_MANAGEMENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"status": "disabled"}'

Limits are checked against charged spend, which is brought up to date every minute, so a key can go slightly over a limit during a burst of requests.