# Track behavior and experiment events

Operation / SDK: `trackEvents` · `track` · `flush` · `track_events` · `TrackEvents`

Report behavior, experiment exposure and feature-flag usage events.

Last updated: 2026-11-06

[Web version](https://www.longplayai.com/docs/api/track-events)

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

## Request

- **Credential**: Server Key (with `customerId`) or Customer Token, scope `events:write`.
- **SDK**: JavaScript `createLongplayServer().trackEvents` · JavaScript `createLongplay().track` · Python `track_events` · Go `TrackEvents`
- **Success**: HTTP 200

> Event recording with an official SDK requires at least `0.10.20`. `POST /api/v1/events` accepts only typed events with `kind` (`custom`, `experiment_exposure` or `feature_usage`); earlier SDKs cannot record events. Direct HTTP clients must implement the current typed request and per-event result contract and do not need an SDK.

- [Event recording version requirement](https://www.longplayai.com/docs/guides/version-support.md#event-recording)

The response shows example data.

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/events" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "events": [
    {
      "eventUuid": "b4eaa1be-aa5e-42c4-85d4-f83fd95d74e9",
      "timestamp": "2026-10-03T12:00:00.000Z",
      "kind": "custom",
      "name": "Pro"
    }
  ],
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}'
```

**TypeScript: trackEvents.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 trackEvents() {
  return longplay.trackEvents({
    events: [
      {
        eventUuid: "b4eaa1be-aa5e-42c4-85d4-f83fd95d74e9",
        timestamp: "2026-10-03T12:00:00.000Z",
        kind: "custom",
        name: "Pro",
      },
    ],
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
  });
}
```

**Request body: request.json**

```json
{
  "events": [
    {
      "eventUuid": "b4eaa1be-aa5e-42c4-85d4-f83fd95d74e9",
      "timestamp": "2026-10-03T12:00:00.000Z",
      "kind": "custom",
      "name": "Pro"
    }
  ],
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}
```

**Response: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "inserted": 1,
  "duplicates": 0,
  "rejected": 0,
  "skipped": 0,
  "rejections": [],
  "analyticsConfig": {
    "eventsEnabled": true,
    "experimentViewsEnabled": true,
    "featureUsageEnabled": true
  }
}
```

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

## Body parameters

**application/json**

- `customerId` (uuid, optional): The Customer's UUID.
- `events` (array, required): Events. minItems: 1 · maxItems: 100
- `events.eventUuid` (uuid | uuid | uuid, required): Unique ID of this event: generate it once and reuse it on retries. An event whose ID was already recorded is counted in `duplicates` and not stored again.
- `events.timestamp` (date-time | date-time | date-time, required): Timestamp.
- `events.merchandise` (object | object | object | object | object | object, optional): Reference to merchandise associated with this event in the current App and environment.
- `events.merchandise.id` (string | string | string, optional): Stable merchant-defined merchandise ID, resolved by the server within the current scope.
- `events.merchandise.internalId` (uuid | uuid | uuid, optional): internal ID.
- `events.deviceId` (string | string | string, optional): Stable identifier of the device or browser that produced the event, such as an anonymous cookie value; stored with the event for analytics.
- `events.sessionId` (string | string | string, optional): session ID.
- `events.kind` ("custom" | "experiment_exposure" | "feature_usage", required): Event type: `custom` is an event you record yourself; `experiment_exposure` and `feature_usage` are records of Local evaluation, which the SDK sends for you.
- `events.name` (string, optional): Name of a custom event. `Experiment Viewed` and `Feature Evaluated` are reserved; an event using either is refused. minLength: 1 · maxLength: 255
- `events.properties` (object, optional): Validated pricing properties used to match dimensions and multipliers. default: {}
- `events.generation` (string | string, optional): Feature flag configuration generation that produced this record, taken from `generation` in the delivered configuration.
- `events.experimentId` (string, optional): Key of the experiment the user saw. minLength: 1 · maxLength: 255
- `events.variationId` (string | string, optional): Key of the variation the user was assigned.
- `events.feature` (string, optional): Key of the feature flag that was read. minLength: 1 · maxLength: 255
- `events.value` (object, optional): Value this evaluation returned.
- `events.source` (enum, optional): Where the value came from, such as the default, a rule or an experiment. Values: `"unknownFeature"`, `"defaultValue"`, `"force"`, `"override"`, `"experiment"`, `"prerequisite"`, `"cyclicPrerequisite"`.
- `events.ruleId` (string, optional): ID of the rule that decided the value; empty when no rule applied. maxLength: 255

<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"`.
- `inserted` (integer, required): Number of events written. min: 0 · max: 9007199254740991
- `duplicates` (integer, required): Number of duplicate events. min: 0 · max: 9007199254740991
- `rejected` (integer, required): Number of rejected events. min: 0 · max: 9007199254740991
- `skipped` (integer, required): Number of events skipped because collection is turned off. min: 0 · max: 9007199254740991
- `rejections` (array, required): Each refused event, one entry per event. Other events in the batch are unaffected. maxItems: 100
- `rejections.eventUuid` (uuid, required): `eventUuid` of the refused event.
- `rejections.reason` (enum, required): Why the event was refused: `invalid_event`, `invalid_merchandise`, `not_delivered` or `reserved_name`. Do not retry a refused event; the answer will not change. Values: `"invalid_event"`, `"invalid_merchandise"`, `"not_delivered"`, `"reserved_name"`.
- `analyticsConfig` (object, required): Event collection settings of the environment.
- `analyticsConfig.eventsEnabled` (boolean, required): Whether behavior events are collected.
- `analyticsConfig.experimentViewsEnabled` (boolean, required): Whether experiment exposures are collected.
- `analyticsConfig.featureUsageEnabled` (boolean, required): Whether feature flag usage is collected.

<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 `events:write`. | `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#features)
