Navigation
On this page

Track Usage API


The Track Usage API allows your application to record usage events in UsageFlow.

Customer-linked ledger ingestion is gated and disabled by default. The contract below applies only when the pilot gate is explicitly enabled; enabling it is outside this ticket.

Customer ledger acceptance contract

For a new Customer-linked event, send a nonblank Idempotency-Key, an active Customer's exact external ID in customerId, a metric, a positive integer amount, and an explicit timestamp with a timezone offset. An active Organization Subscription is also required. A successful response is { "success": true, "eventId": "...", "acceptance": "ACCEPTED" }; the ID names the committed LEDGER_ONLY UsageEvent. The row records the same captured server receipt instant used to validate its time window. Acceptance commits a durable PENDING processing intent with the event, even if queue dispatch fails. It does not mean the event was processed, rated, or billed. Customer-linked ingestion remains disabled in the deployed configuration.

The canonical billable fields for this contract are the submitted external Customer ID (case sensitive, with surrounding whitespace rejected), the nonblank metric after trimming and uppercasing, the positive integer quantity, and the UTC occurrence instant. Timestamps may use Z or a numeric offset and at most millisecond precision. Equivalent offsets for one instant mean one occurrence time; precision beyond milliseconds is rejected. Metadata is stored on first acceptance and is not a billable field. An identical retry with the same Organization and key returns the original { "success": true, "eventId": "...", "acceptance": "ACCEPTED" } even if the Customer or Subscription later changes or the 72-hour window closes. Changed billable contents return HTTP 409 and create no new event. Invalid billable contents return HTTP 400 and cannot match an accepted event.

The occurrence instant determines its UTC calendar month. A new event may arrive until 72 hours after that month ends, including the exact close instant. A new event more than five minutes after server receipt is rejected; exactly five minutes is allowed. Missing or invalid timestamps are rejected and never replaced with receipt time. Retain and replay original keys and billable contents: UsageFlow retains keys, fingerprints, and original acceptance results for the proposed 12-month raw event retention period. Retry guarantees end after lawful purge. The gate stays off until the remaining ledger tickets and pilot checks are complete.

Every time a billable action occurs in your product (for example an API call or storage usage), your backend should send a request to this endpoint.

UsageFlow stores the event and uses it later to calculate invoices.


Endpoint

POST /api/track

This endpoint records a single usage event.


Authentication

Requests must include a valid API key.

You can generate API keys from:

Dashboard → API Keys → Generate Key

Example key:

uf_live_abc123xyz

Include the key in the request header.


Headers

HeaderRequiredDescription
x-usageflow-api-keyYesAPI key used to authenticate the request
Idempotency-KeyRecommendedPrevents duplicate usage events
Content-TypeYesMust be application/json

Example:

x-usageflow-api-key: YOUR_API_KEY
Idempotency-Key: unique-event-id
Content-Type: application/json

Request Body

FieldTypeRequiredDescription
metricstringYesMetric key that defines the type of usage
amountnumberYesAmount of usage
customerIdstringOptionalIdentifier of the customer triggering the event
metadataobjectOptionalAdditional event data

Example Request:

POST /api/track
{
  "metric": "API_CALL",
  "amount": 1,
  "customerId": "user_123",
  "metadata": {
    "endpoint": "/v1/chat",
    "region": "us-east-1"
  }
}

This example records a single API call made by user_123.

Example Using Node.js

await fetch("https://your-domain.com/api/track", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-usageflow-api-key": process.env.USAGEFLOW_API_KEY,
    "Idempotency-Key": crypto.randomUUID(),
  },
  body: JSON.stringify({
    metric: "API_CALL",
    amount: 1,
    customerId: "user_123",
  }),
});

This request records usage whenever an API request is made in your application.


Successful Response

{
  "success": true
}

Status code:

200 OK

Error Responses

  • Missing API Key 401 Unauthorized

    {
      "error": "Missing API key"
    }
    
  • Invalid API Key 401 Unauthorized

    {
      "error": "Invalid API key"
    }
    

Duplicate Usage Event

For the legacy route, reusing the same Idempotency-Key does not record another event. The gated Customer ledger route does not yet offer a stable retry or conflict response; that work is planned for the next ticket.

This prevents accidental double billing.


Best Practices

Use Idempotency Keys

Always include an Idempotency-Key to avoid duplicate usage events.

Example:

Idempotency-Key: event_12345

Send Usage from Backend Only

Usage events should only be sent from your server, not from the client.

This prevents users from manipulating billing data.

Keep Events Lightweight

Usage events should contain only essential data.

Example:

metric
amount
customerId

Avoid sending large payloads in metadata.


How UsageFlow Processes Events

After receiving a usage event, UsageFlow processes it through the following pipeline:

Usage Event Received
↓
Event Stored in Database
↓
Aggregation Worker
↓
Aggregated Usage Records
↓
Invoice Calculation

Usage events are the source of truth for billing.


Example Billing Scenario

Imagine your plan includes:

Metric: API_CALL
Included Units: 10,000
Overage Price: $0.01

If your application sends 12,000 usage events:

Included: 10,000
Overage: 2,000 × $0.01
Total Overage: $20

This amount is added to the invoice.

Next Steps

Continue exploring the documentation: