Server-SideREST APIEndpoints

Fetch Token

Retrieve creative token for returning users via the REST API

The Fetch Token endpoint sends device metrics and returns a token you use for webhook events. The React Web SDK calls this on initialization and caches the token.

Endpoint

POST /fetch-token

When to Call

Call this endpoint when:

  • You initialize the app/SDK and need a token
  • The cached token is missing or expired

Cache the token locally. The SDK avoids repeat calls when the token is already stored.

Request Body

Prop

Type

Device Metrics Fields

Prop

Type

Example Request

curl -X POST https://analytics.geeklab.app/fetch-token \
  -H "Content-Type: application/json" \
  -H "geeklab-api-key: your_api_key" \
  -d '{
    "type": "device-metrics",
    "created_at": "2024-10-10T10:15:00.000Z",
    "data": {
      "device_name": "MacBook Pro",
      "device_model": "MacBookPro18,3",
      "os_system": "macOS 14.5",
      "timezone": "America/Los_Angeles",
      "dpi": 220,
      "window_height": 982,
      "window_width": 1512
    }
  }'

Response

Success Response (200 OK)

{
  "token": "abc123def456"
}

Response Fields

Prop

Type

Error Responses

400 Bad Request - Missing required fields:

{
  "error": "Bad request, data not formatted properly."
}

401 Unauthorized - Invalid API key:

{
  "error": "API key is not valid."
}

Flow Comparison

Initialize App:
  Fetch Token → Cache token → Send Webhook Events

Subsequent Opens:
  Use cached token → Send Webhook Events

Best Practices

  1. Cache the token: Store the token to avoid extra API calls
  2. Only fetch when needed: Call this endpoint only if you don't have a cached token

What's Next

After fetching the token:

  1. Send a retention event - Track the session start with webhook events
  2. Track in-app events - Send purchase/ad/custom events as they occur via /webhook