Skip to content

Managing Developer API Keys and Token Scopes

Issue, rotate, and revoke scoped Bearer tokens to authenticate programmatic requests to the Shaf REST API.

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: Owner or Admin role in your Shaf organization.
  • A secure environment variable store or secrets manager (e.g., Doppler, AWS Secrets Manager, GitHub Actions Secrets).
  • A terminal with curl or a Node.js 18+ runtime.

Implementation Recipe

  1. 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"
    }
  2. Available Token Scopes

    Assign minimal privilege by choosing exact scope permissions:

    Scope Allowed Operations
    links:read List, query, and search short link metadata.
    links:write Create, update, archive, and delete short links.
    domains:read Inspect custom domain status and verification records.
    domains:write Register domains, update fallback rules, and trigger DNS checks.
    analytics:read Query click events, geographic distributions, and telemetry exports.
    webhooks:write Register, update, test, and delete webhook subscription endpoints.
    admin Full read-write privileges across all workspace resources.
  3. Authenticate Requests with Bearer Headers

    Pass the API token in the Authorization header 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.`);
  4. Rotate Tokens with Zero Downtime

    To rotate credentials without breaking production services:

    1. Create a replacement token with identical scopes.
    2. Update the secret in your application or CI runner.
    3. Verify that requests succeed using the new token.
    4. 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.

Type to search…

↑↓ navigate↵ selectEsc close