---
title: "Making Your First API Request"
description: "Generate a scoped developer API token and programmatically create, test, and query an edge short link using cURL or TypeScript in under 4 minutes."
---

> Documentation Index
> Fetch the complete documentation index at: https://shaf.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Making Your First API Request

# 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](https://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:

   ```bash
   export SHAF_API_TOKEN="shaf_live_9a7d8f2b3c4e5a6f7b8c9d0e1f2a3b4c"
   ```

   > **Token Display Policy**
   >
   > Shaf hashes API tokens using SHA-256 before persisting them in D1. The raw plaintext string is displayed only once upon generation.

   **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

   ```bash
   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"
     }'
   ```

   ### TypeScript / Node.js

   ```typescript
   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`:

   ```json
   {
     "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:

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

   **Expected output:**

   ```http
   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:

   ```bash
   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:**

   ```json
   {
     "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:

- **REST API v1 Reference** — Inspect full OpenAPI schemas, query parameters, pagination, and bulk operations.
- **Webhook Event Subscriptions** — Subscribe to real-time `link.clicked` and `domain.verified` event payloads.
- **Model Context Protocol (MCP) Server** — Connect Claude Code, Cursor, or Windsurf to manage links via natural language prompts.
- **API Keys & Token Lifecycle** — Configure automated token rotation, service account isolation, and audit logging.

Source: https://shaf.app/docs/tutorials/api-first-link/index.mdx
