Skip to content

Making Your First API Request

Generate a scoped developer API token and programmatically create, test, and query an edge short link using cURL or TypeScript in under 4 minutes.

Making Your First API Request

Generate a scoped developer API token and programmatically create, test, and query an edge short link using cURL or TypeScript in under 4 minutes.


Prerequisites

  • A Shaf account with Owner or Admin privileges in your workspace.
  • A terminal with curl or a Node.js 18+ runtime.

Step-by-Step Instructions

  1. Generate a Scoped API Key

    Navigate to dash.shaf.app/dashboard/settings/api-keys and click Create API Key.

    Set the key parameters:

    • Name: Deployment Pipeline Automation
    • Expiration: 90 Days (or Never Expire for backend services)

    Click Generate Token and immediately copy the token value:

    export SHAF_API_TOKEN="shaf_live_9a7d8f2b3c4e5a6f7b8c9d0e1f2a3b4c"

    Expected output: The API keys table lists the key with its cryptographic prefix, creation date, and status Active.

  2. Create a Short Link Programmatically

    Execute a POST request against /api/v1/links with your Bearer token and JSON payload:

    curl -X POST "https://shaf.app/api/v1/links" \
      -H "Authorization: Bearer $SHAF_API_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "url": "https://github.com/meladhani01/shaf",
        "slug": "api-quickstart",
        "domain": "shaf.is"
      }'
    const token = process.env.SHAF_API_TOKEN;
    
    const response = await fetch("https://shaf.app/api/v1/links", {
      method: "POST",
      headers: {
        "Authorization": `Bearer ${token}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        url: "https://github.com/meladhani01/shaf",
        slug: "api-quickstart",
        domain: "shaf.is",
      }),
    });
    
    if (!response.ok) {
      throw new Error(`API error [${response.status}]: ${await response.text()}`);
    }
    
    const payload = await response.json();
    console.log("Created short link:", payload.data.shortUrl);

    Expected output: Shaf returns a standardized JSON response envelope with HTTP status 201 Created:

    {
      "code": 0,
      "data": {
        "id": 142,
        "slug": "api-quickstart",
        "url": "https://github.com/meladhani01/shaf",
        "domain": "shaf.is",
        "shortUrl": "https://shaf.is/api-quickstart",
        "createdAt": "2026-11-21T14:35:00.000Z",
        "clicks": 0
      }
    }
  3. Verify Edge Redirect Execution

    Verify the redirect header directly from Cloudflare’s Anycast edge without following location jumps:

    curl -I https://shaf.is/api-quickstart

    Expected output:

    HTTP/2 308 Permanent Redirect
    location: https://github.com/meladhani01/shaf
    cf-cache-status: HIT
    cache-control: public, max-age=86400, stale-while-revalidate=604800
  4. Query Realtime Click Telemetry

    Generate a test click and immediately query aggregated telemetry for your slug:

    curl -sL -o /dev/null https://shaf.is/api-quickstart
    
    curl -X GET "https://shaf.app/api/v1/links/api-quickstart/analytics?domain=shaf.is" \
      -H "Authorization: Bearer $SHAF_API_TOKEN"

    Expected output:

    {
      "code": 0,
      "data": {
        "totalClicks": 1,
        "uniqueVisitors": 1,
        "topCountries": [
          { "country": "US", "count": 1 }
        ],
        "topBrowsers": [
          { "browser": "cURL", "count": 1 }
        ]
      }
    }

API Error Reference

HTTP Code Error Condition Remediation
400 Bad Request Target url is malformed or slug contains invalid characters. Verify that destination begins with http:// or https:// and slug uses only a-z, 0-9, and -.
401 Unauthorized Missing, malformed, or expired Bearer token. Verify that header follows Authorization: Bearer shaf_live_....
409 Conflict Requested slug already exists on domain. Pass an alternate slug or omit slug to let Shaf generate a random 6-character identifier.
429 Too Many Requests Request rate exceeded 1,200 requests/minute. Throttle client calls and inspect the Retry-After: <seconds> response header.

Next Steps

Integrate programmatic link workflows into production infrastructure:

Type to search…

↑↓ navigate↵ selectEsc close