# Read credit analytics

Operation / SDK: `readCreditAnalytics` · `read_credit_analytics` · `ReadCreditAnalytics`

Read credit analytics derived from balance and financial records.

Last updated: 2026-11-06

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

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

## Request

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

The response shows example data.

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/credits/analytics" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": null,
  "query": {
    "range": {
      "kind": "absolute",
      "from": "2026-10-01T00:00:00.000Z",
      "to": "2026-10-03T12:00:00.000Z"
    },
    "timezone": "UTC",
    "filterBy": [],
    "interval": "day",
    "internal_feature_id": null,
    "groupBy": "customer",
    "entityId": null,
    "topN": null,
    "after": null,
    "limit": 50
  }
}'
```

**TypeScript: readCreditAnalytics.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 readCreditAnalytics() {
  return longplay.readCreditAnalytics({
    customerId: null,
    query: {
      range: {
        kind: "absolute",
        from: "2026-10-01T00:00:00.000Z",
        to: "2026-10-03T12:00:00.000Z",
      },
      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": "absolute",
      "from": "2026-10-01T00:00:00.000Z",
      "to": "2026-10-03T12:00:00.000Z"
    },
    "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",
        "resourceId": "79322b17-a312-485e-8033-b532cdc16fe9",
        "consumedUnits": "-1",
        "refundedUnits": "-1",
        "reservedUnits": "-1",
        "charged": "-1",
        "refunded": "-1",
        "reserved": "-1",
        "netUnits": "-1",
        "netCharged": "-1"
      }
    ],
    "nextCursor": {
      "bucket": "2026-10-03T12:00:00.000Z",
      "group": "1Example",
      "isOther": true,
      "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
      "resourceId": "79322b17-a312-485e-8033-b532cdc16fe9"
    }
  }
}
```

<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.after.resourceId` (uuid, required): Cursor field: the `resourceId` from the previous page's `nextCursor`. Pass that cursor object unchanged.
- `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.resourceId` (uuid, required): UUID of the Feature whose balance funded the usage: the credit Feature when usage is paid with credits, otherwise the same as `internal_feature_id`.
- `analytics.rows.consumedUnits` (string, required): Quantity consumed in this bucket, including confirmed reservations, in units of the tracked Feature (`internal_feature_id`); decimal string. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.refundedUnits` (string, required): Quantity refunded, in units of the tracked Feature; decimal string. Refunds count in the bucket of the original usage. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.reservedUnits` (string, required): Quantity currently held by pending, unexpired reservations, in units of the tracked Feature; decimal string. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.charged` (string, required): Amount deducted from the `resourceId` balance by consumption, in that balance's units (credits for a credit Feature); decimal string. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.refunded` (string, required): Refunded portion. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.reserved` (string, required): Reserved quantity. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.netUnits` (string, required): `consumedUnits` minus `refundedUnits`; decimal string. Reservations are not included. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.netCharged` (string, required): `charged` minus `refunded`: the net amount deducted from the `resourceId` balance; decimal string. pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `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.
- `analytics.nextCursor.resourceId` (uuid, required): `resourceId` of the last row on this page; part of the cursor to pass unchanged as `query.after`.

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