# Read usage analytics

Operation / SDK: `readUsageAnalytics` · `read_usage_analytics` · `ReadUsageAnalytics`

Read scoped usage aggregates.

Last updated: 2026-11-06

[Web version](https://www.longplayai.com/docs/api/read-usage-analytics)

<a id="request"></a>

## Request

- **Credential**: Server Key with `credits:read`. Call from your backend only.
- **SDK**: JavaScript `createLongplayServer().readUsageAnalytics` · Python `read_usage_analytics` · Go `ReadUsageAnalytics`
- **Success**: HTTP 200

The response shows example data.

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/usage/analytics" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": null,
  "query": {
    "range": {
      "kind": "last_days",
      "days": 7
    },
    "timezone": "UTC",
    "filterBy": [],
    "interval": "day",
    "internal_feature_id": null,
    "groupBy": "customer",
    "entityId": null,
    "topN": null,
    "after": null,
    "limit": 50
  }
}'
```

**TypeScript: readUsageAnalytics.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function readUsageAnalytics() {
  return longplay.readUsageAnalytics({
    customerId: null,
    query: {
      range: {
        kind: "last_days",
        days: 7,
      },
      timezone: "UTC",
      filterBy: [],
      interval: "day",
      internal_feature_id: null,
      groupBy: "customer",
      entityId: null,
      topN: null,
      after: null,
      limit: 50,
    },
  });
}
```

**Request body: request.json**

```json
{
  "customerId": null,
  "query": {
    "range": {
      "kind": "last_days",
      "days": 7
    },
    "timezone": "UTC",
    "filterBy": [],
    "interval": "day",
    "internal_feature_id": null,
    "groupBy": "customer",
    "entityId": null,
    "topN": null,
    "after": null,
    "limit": 50
  }
}
```

**Response: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "analytics": {
    "from": "2026-10-03T12:00:00.000Z",
    "to": "2026-10-03T12:00:00.000Z",
    "interval": "hour",
    "timezone": "UTC",
    "rows": [
      {
        "bucket": "2026-10-03T12:00:00.000Z",
        "group": "1Example",
        "isOther": true,
        "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
        "eventCount": "1",
        "units": "1",
        "tokenEventCount": "1",
        "tokens": {
          "input": "1",
          "output": "1",
          "cache_read": "1",
          "cache_write": "1",
          "reasoning": "1",
          "input_audio": "1",
          "output_audio": "1"
        },
        "totalTokens": "1",
        "providerCostEventCount": "1",
        "providerCostUsd": "1",
        "invalidTelemetryEventCount": "1"
      }
    ],
    "nextCursor": {
      "bucket": "2026-10-03T12:00:00.000Z",
      "group": "1Example",
      "isOther": true,
      "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed"
    }
  }
}
```

<a id="parameters"></a>

## Body parameters

**application/json**

- `customerId` (uuid | null, required): The Customer UUID. Required with a Server Key; omit it with a Customer Token.
- `query` (object, required): Query.
- `query.range` (object | object | object, required): Time range.
- `query.range.kind` ("absolute" | "last_days" | "billing_cycles", required): Kind; see the allowed values.
- `query.range.from` (date-time, optional): Range start (ISO 8601).
- `query.range.to` (date-time, optional): Range end (ISO 8601).
- `query.range.days` (1 | 7 | 30 | 90, optional): Number of days.
- `query.range.membershipId` (uuid, optional): membership ID.
- `query.range.count` (integer, optional): For a `billing_cycles` range, how many of the membership's most recent billing cycles to cover (1–12); with `includeCurrent: false`, only completed cycles count. min: 1 · max: 12
- `query.range.includeCurrent` (boolean, optional): Whether to include the current period.
- `query.timezone` (string, required): Time zone. maxLength: 100
- `query.filterBy` (array, required): Filter. maxItems: 5
- `query.filterBy.key` (string, required): Name of an event property to filter on (lowercase letters, digits and underscores); only events whose property equals one of `values` are included. pattern: ^[a-z][a-z0-9_]{0,63}$
- `query.filterBy.values` (array, required): Values. minItems: 1 · maxItems: 20
- `query.interval` (enum, required): Interval; see the allowed values. Values: `"hour"`, `"day"`, `"week"`, `"month"`.
- `query.internal_feature_id` (uuid | null, required): Feature ID.
- `query.entityId` (uuid | null, required): Entity ID within the current customer scope, for configured Entity and license Features.
- `query.topN` (integer | null, required): Number of top items to return.
- `query.groupBy` (enum | string, required): Group-by dimension.
- `query.after` (object | null, required): Pagination cursor: pass the value from the previous page; `null` for the first page.
- `query.after.bucket` (date-time, required): Aggregation bucket.
- `query.after.group` (string, required): Group. maxLength: 240
- `query.after.isOther` (boolean, required): Whether this row aggregates the remaining items.
- `query.after.internal_feature_id` (uuid, required): Feature ID.
- `query.limit` (integer, required): Maximum records per page, subject to the contract limit. min: 1 · max: 100

<a id="response"></a>

## Response fields

Every successful response includes `requestId` (this request's ID, for support) and `scope` (App and environment).

**HTTP 200 · application/json**

- `requestId` (uuid, required): UUID the server generated for this HTTP request; include it when reporting a problem. It is not your business requestId.
- `scope` (object, required): The App and environment of this response. Headers only verify scope; they cannot switch key scope.
- `scope.projectId` (string, required): App ID bound to the credential. minLength: 1
- `scope.environment` (enum, required): Separate data scope: sandbox or live. Values: `"sandbox"`, `"live"`.
- `analytics` (object, required): The report page: the resolved `from`/`to` range, `interval` and `timezone`, the aggregated `rows` and `nextCursor` for the next page.
- `analytics.from` (date-time, required): Range start (ISO 8601).
- `analytics.to` (date-time, required): Range end (ISO 8601).
- `analytics.interval` (enum, required): Interval; see the allowed values. Values: `"hour"`, `"day"`, `"week"`, `"month"`.
- `analytics.timezone` (string, required): Time zone. maxLength: 100
- `analytics.rows` (array, required): Data rows. maxItems: 100
- `analytics.rows.bucket` (date-time, required): Aggregation bucket.
- `analytics.rows.group` (string, required): Group. maxLength: 240
- `analytics.rows.isOther` (boolean, required): Whether this row aggregates the remaining items.
- `analytics.rows.internal_feature_id` (uuid, required): Feature ID.
- `analytics.rows.eventCount` (string, required): Number of accepted usage events in this row (consumptions and confirmed reservations, including zero-quantity ones), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.units` (string, required): Units as a decimal string. pattern: ^(0|[1-9]\d{0,29})(\.\d{1,18})?$
- `analytics.rows.tokenEventCount` (string, required): Number of events in this row with a valid `ai_tokens` report, as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens` (object, required): AI token totals by pool, summed over the events in this row with valid token reports; each value is an integer string.
- `analytics.rows.tokens.input` (string, required): Total of the `input` pool (input tokens not counted in another pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens.output` (string, required): Total of the `output` pool (output tokens not counted in another pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens.cache_read` (string, required): Total tokens read from the prompt cache (`cache_read` pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens.cache_write` (string, required): Total tokens written to the prompt cache (`cache_write` pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens.reasoning` (string, required): Total reasoning tokens (`reasoning` pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens.input_audio` (string, required): Total audio input tokens (`input_audio` pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.tokens.output_audio` (string, required): Total audio output tokens (`output_audio` pool), as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.totalTokens` (string, required): Sum of all seven token pools in this row, as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.providerCostEventCount` (string, required): Number of events in this row with a valid `ai_provider_cost_usd` report, as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.rows.providerCostUsd` (string, required): Total AI provider cost in USD reported by events in this row, as a decimal string; events without a valid report add nothing. pattern: ^(0|[1-9]\d{0,29})(\.\d{1,18})?$
- `analytics.rows.invalidTelemetryEventCount` (string, required): Number of events in this row whose `ai_tokens` or `ai_provider_cost_usd` report is malformed and therefore excluded from the totals, as an integer string. pattern: ^(0|[1-9]\d{0,29})$
- `analytics.nextCursor` (object | null, required): Next-page cursor; null means end. Pass unchanged and clear when filters change.
- `analytics.nextCursor.bucket` (date-time, required): Aggregation bucket.
- `analytics.nextCursor.group` (string, required): Group. maxLength: 240
- `analytics.nextCursor.isOther` (boolean, required): Whether this row aggregates the remaining items.
- `analytics.nextCursor.internal_feature_id` (uuid, required): Feature ID.

<a id="errors"></a>

## Errors

Failures return an error object; handle them by `error.code` and `error.recovery`. The table lists the errors that follow from this operation's credential, scope and idempotency fields; depending on the request it can also return `NOT_FOUND` (a referenced resource doesn't exist), `NOT_CONFIGURED` (a required integration isn't connected) or `PAYLOAD_TOO_LARGE`. Some refusals, such as insufficient balance, are not errors but a `reason` in a successful response.

**Possible errors**

| code | HTTP | When | recovery |
| --- | --- | --- | --- |
| `INVALID_REQUEST` | 400 | The request doesn't match this page's parameters. A missing or malformed protocol header returns `UNSUPPORTED_PROTOCOL_VERSION` or `INVALID_PROTOCOL_VERSION` instead. | `fix_request` |
| `UNAUTHENTICATED` | 401 | The credential is missing, invalid, expired or revoked. | `authenticate` |
| `FORBIDDEN` | 403 | The credential lacks `credits:read`. | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` or `X-Indiana-Environment` doesn't match the credential's App and environment. | `check_scope` |
| `RATE_LIMITED` | 429 | Too many requests; wait for `Retry-After` and retry the same request. | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | The service is temporarily unavailable; retry the same request. | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | The outcome is unknown; retry later. | `recover_operation` |

- [Error codes and recovery](https://www.longplayai.com/docs/api/errors.md)
- [Request conventions](https://www.longplayai.com/docs/api.md)
- [All operations](https://www.longplayai.com/docs/api/operations.md#credits)
