Appearance
API keys and request signing
Use an API key when a script, a billing system or an AI agent talks to Bitstream. A key acts as the user who made it, limited to the scopes you give it. A reseller key can never do more than that reseller can.
Who can have keys
Staff can create keys when their role has apikeys:write (see Permissions and roles). For resellers, API keys are a group permission (apikeys:read and apikeys:write), and it is off by default. Turn it on for a group under Groups & permissions in the panel when you want resellers to run bots or billing scripts.
When you turn it off again, the existing keys of that group stop working at once. They answer 403 with the code api_keys_not_allowed.
As an admin you can list every key, resellers' included, with GET /api-keys?all=true, or one user's keys with ?user_id=N.
Create a key
Creating a key needs a normal sign-in token, not another API key:
bash
curl -s -X POST $PANEL/api-keys \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{
"name": "billing",
"scopes": ["lines:read", "lines:write"],
"allowed_ips": ["203.0.113.10/32"],
"expires_at": null
}' | jq -r .keyThe answer has the full key, for example bsk_a1b2c3d4_Q2xvdWQgc2VjcmV0IGtleSBleGFtcGxl. It is shown only once. Store it now.
scopesmust be a subset of your own permissions.GET /system/permissionslists them all.allowed_ipsis a list of CIDRs. Empty means any IP.expires_atis a date in RFC 3339, ornullfor no expiry.
Use it
Send it like a sign-in token:
bash
curl -s $PANEL/lines -H "Authorization: Bearer bsk_a1b2c3d4_Q2xvdWQgc2VjcmV0IGtleSBleGFtcGxl"A few actions refuse API keys on purpose and need a normal sign-in token: 2FA, changing a password, and creating, editing or rotating keys. So a leaked key cannot loosen its own IP list, signature rule or expiry. A password change from a key answers 403 api_key_not_allowed.
Forced 2FA does not apply to API keys. A user who must still set up 2FA can keep using existing keys.
Rotate a key
Rotating gives the key a new secret and keeps its name, scopes and settings. Give your systems time to switch with grace_seconds (up to 86400). The old secret keeps working that long:
bash
curl -s -X POST $PANEL/api-keys/7/rotate \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"grace_seconds": 3600}' | jq -r .keyDelete a key with DELETE /api-keys/{id}.
Sign requests
For extra safety, a key can require a signature on every request. Then a stolen key is not enough: the attacker also needs to sign, and each request can be used only once.
Turn it on with "require_signature": true when you create the key, or later with PATCH /api-keys/{id} (with a sign-in token).
Each request then carries three headers:
| Header | Value |
|---|---|
X-BS-Timestamp | Unix time in seconds. Must be within 5 minutes of the server clock. |
X-BS-Nonce | A random string of 16 to 64 characters. Each nonce works once. |
X-BS-Signature | Lowercase hex of HMAC-SHA256(secret, string_to_sign). |
secret is the part of the key after the second underscore. The string to sign is five lines joined with \n:
METHOD
PATH_WITH_QUERY
TIMESTAMP
NONCE
hex(sha256(BODY))METHODin upper case, for examplePOST.PATH_WITH_QUERYexactly as sent, starting at/api/v1, for example/api/v1/lines?page=2.- For an empty body, use the SHA-256 of the empty string (
e3b0c442...b855).
In shell:
bash
KEY=bsk_a1b2c3d4_Q2xvdWQgc2VjcmV0IGtleSBleGFtcGxl
SECRET=${KEY#bsk_*_}
ts=$(date +%s); nonce=$(openssl rand -hex 16); body='{"periods":1}'
sig=$(printf 'POST\n/api/v1/lines/812/renew\n%s\n%s\n%s' "$ts" "$nonce" \
"$(printf %s "$body" | sha256sum | cut -d' ' -f1)" \
| openssl dgst -sha256 -hmac "$SECRET" -hex | sed 's/^.* //')
curl -X POST https://panel.example.com/api/v1/lines/812/renew \
-H "Authorization: Bearer $KEY" -H "X-BS-Timestamp: $ts" -H "X-BS-Nonce: $nonce" \
-H "X-BS-Signature: $sig" -H 'Content-Type: application/json' -d "$body"When something is wrong, the API answers 401 with one of these codes:
| Code | Meaning |
|---|---|
bad_signature | The signature does not match. Check the path, the body hash and the secret. |
stale_timestamp | The clock on your machine is more than 5 minutes off. |
nonce_reused | You sent the same nonce twice. Make a new one per request. |
Limits
The admin API allows 600 requests per minute. Over that you get 429. Slow down and try again.