Managing Developer API Keys and Token Scopes
Automated CI/CD pipelines, backend services, and CLI scripts require authenticated access to the Shaf API without sharing personal user credentials. This guide demonstrates how to generate scoped API tokens, perform zero-downtime rotation, and revoke compromised credentials.
Prerequisites
- Workspace permissions:
OwnerorAdminrole in your Shaf organization. - A secure environment variable store or secrets manager (e.g., Doppler, AWS Secrets Manager, GitHub Actions Secrets).
- A terminal with
curlor a Node.js 18+ runtime.
Implementation Recipe
Create a Scoped API Key
In the Shaf Dashboard, navigate to Settings → API Keys and click + Create API Key, or use your existing admin token to issue a scoped key:
curl -X POST "https://shaf.app/api/v1/api-keys" \ -H "Authorization: Bearer $SHAF_ADMIN_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "name": "GitHub Actions Deployment", "scopes": ["links:read", "links:write", "analytics:read"], "expiresInDays": 90 }'Expected response (
201 Created):{ "id": "key_01j8k9x2y3z4", "name": "GitHub Actions Deployment", "token": "shaf_live_9a7d8f2b3c4e5a6f7b8c9d0e1f2a3b4c", "prefix": "shaf_live_9a7d", "scopes": ["links:read", "links:write", "analytics:read"], "createdAt": "2026-11-21T17:00:00Z", "expiresAt": "2027-02-19T17:00:00Z" }Available Token Scopes
Assign minimal privilege by choosing exact scope permissions:
Scope Allowed Operations links:readList, query, and search short link metadata. links:writeCreate, update, archive, and delete short links. domains:readInspect custom domain status and verification records. domains:writeRegister domains, update fallback rules, and trigger DNS checks. analytics:readQuery click events, geographic distributions, and telemetry exports. webhooks:writeRegister, update, test, and delete webhook subscription endpoints. adminFull read-write privileges across all workspace resources. Authenticate Requests with Bearer Headers
Pass the API token in the
Authorizationheader for all requests (see REST API Overview for envelope details):curl -X GET "https://shaf.app/api/v1/links?limit=5" \ -H "Authorization: Bearer $SHAF_API_TOKEN" \ -H "Content-Type: application/json"const token = process.env.SHAF_API_TOKEN; const res = await fetch('https://shaf.app/api/v1/links?limit=5', { headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', }, }); if (!res.ok) { throw new Error(`API error [${res.status}]: ${await res.text()}`); } const data = await res.json(); console.log(`Retrieved ${data.links.length} links.`);Rotate Tokens with Zero Downtime
To rotate credentials without breaking production services:
- Create a replacement token with identical scopes.
- Update the secret in your application or CI runner.
- Verify that requests succeed using the new token.
- Revoke the old token via the dashboard or API:
curl -X DELETE "https://shaf.app/api/v1/api-keys/key_old_credential_id" \ -H "Authorization: Bearer $SHAF_ADMIN_TOKEN"
Verification
Test token validity against the authentication verification endpoint:
curl -i "https://shaf.app/api/v1/auth/me" \
-H "Authorization: Bearer $SHAF_API_TOKEN"Expected response (200 OK):
HTTP/2 200 OK
content-type: application/json
{
"workspaceId": "ws_01j8m4n5p6",
"keyName": "GitHub Actions Deployment",
"scopes": ["links:read", "links:write", "analytics:read"],
"rateLimitRemaining": 998
}Common Gotchas
| Issue / Error | Root Cause | Solution |
|---|---|---|
401 Unauthorized |
Token is expired, malformed, or missing the Bearer prefix. |
Check token expiration date and ensure header is formatted as Authorization: Bearer shaf_live_.... |
403 Forbidden |
Token lacks the scope required for this endpoint (e.g. links:write). |
Re-issue the token with the necessary scopes, or upgrade your key privileges. |
| Lost Token Plaintext | Token was closed without saving and cannot be retrieved from D1. | Revoke the lost token and generate a new key immediately. |
429 Too Many Requests |
Request volume exceeded plan rate limits. | Implement exponential backoff, cache frequent queries, or upgrade your organization tier. |