# Record merchant business events

Operation / SDK: `recordMerchantEvent` · `record_merchant_event` · `RecordMerchantEvent`

Record merchant-reported billing, coupon use, fulfillment, benefit, authorization, usage and balance results through one entry, idempotent by eventId. Non-financial events create no invoices, revenue or spendable Longplay balance and don't change platform usage.

Last updated: 2026-11-06

[Web version](https://www.longplayai.com/docs/api/record-merchant-event)

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

## Request

Except for `coupon.redeemed`, new events require the current App environment's merchant execution mode to be `record_only`. This is environment configuration, not a request field for this operation.

Save a stable `eventId` for each merchant event. Retries must reuse the same `eventId` and identical event content; reusing an `eventId` with different content returns an idempotency conflict.

- **Credential**: Server Key with `billing:write`. Call from your backend only.
- **SDK**: JavaScript `createLongplayServer().recordMerchantEvent` · Python `record_merchant_event` · Go `RecordMerchantEvent`
- **Success**: HTTP 200

The response shows example data.

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/merchant/events/record" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "type": "purchase.closed",
  "eventId": "merchant-order-123-closed",
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "occurredAt": "2026-10-03T12:00:00.000Z",
  "operationId": "a8b14e0c-6576-49a4-812c-7a1587e55255",
  "status": "failed"
}'
```

**TypeScript: recordMerchantEvent.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 recordMerchantEvent() {
  return longplay.recordMerchantEvent({
    type: "purchase.closed",
    eventId: "merchant-order-123-closed",
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
    occurredAt: "2026-10-03T12:00:00.000Z",
    operationId: "a8b14e0c-6576-49a4-812c-7a1587e55255",
    status: "failed",
  });
}
```

**Request body: request.json**

```json
{
  "type": "purchase.closed",
  "eventId": "merchant-order-123-closed",
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "occurredAt": "2026-10-03T12:00:00.000Z",
  "operationId": "a8b14e0c-6576-49a4-812c-7a1587e55255",
  "status": "failed"
}
```

**Response: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "record": {
    "id": "05260476-ab36-45ee-8865-5f736a983341",
    "eventId": "merchant-order-123-closed",
    "source": "merchant",
    "providerVerified": false,
    "outcome": "recorded"
  }
}
```

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

## Body parameters

**application/json**

- `eventId` (string | string | string | string | string | string | string | string | string | string | string | string | string | string | string | string | string, required): Event ID used for deduplication; unchanged on retries.
- `customerId` (uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid, required): The Customer UUID. Required with a Server Key; omit it with a Customer Token.
- `occurredAt` (date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time, required): Timestamp (ISO 8601).
- `type` ("purchase.completed" | "purchase.closed" | "subscription.observed" | "subscription.change.completed" | "subscription.change.closed" | "invoice.paid" | "refund.updated" | "fulfillment.updated" | "benefit.claimed" | "benefit.closed" | "benefit.updated" | "authorization.updated" | "balance.observed" | "coupon.redeemed" | "coupon.redeemed" | "usage.observed" | "usage.observed", required): Event or operation discriminator selecting the matching request branch.
- `operationId` (uuid | uuid | uuid | null | uuid | uuid | uuid | null | uuid, optional): Business operation UUID. Subscription execution references operationId from previewAttach; balance refunds reference the original consumption.
- `merchantOrderId` (string, optional): Stable merchant-native order reference binding the original purchase and its replays. minLength: 1 · maxLength: 200
- `paymentStatus` (enum, optional): Merchant-verified payment outcome: paid or no payment required. Values: `"paid"`, `"trialing"`, `"no_payment_required"`.
- `invoice` (object | null | object | null | object, optional): Invoice.
- `invoice.merchantInvoiceId` (string | string | string, required): Stable merchant-native invoice reference used to verify invoice facts and replays.
- `invoice.reason` (enum | enum | enum, optional): Rejection reason or ledger operation description; interpret with accepted and status.
- `invoice.currency` (string | string | string, required): Currency code supported by the published price.
- `invoice.subtotalMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.discountMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.taxMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.totalMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.paidMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.periodStartsAt` (date-time | null | date-time | null | date-time | null, required): Timestamp (ISO 8601).
- `invoice.periodEndsAt` (date-time | null | date-time | null | date-time | null, required): Timestamp (ISO 8601).
- `invoice.lines` (array | array | array, required): Line items.
- `invoice.lines.merchantLineId` (string | string | string | string | string | string, required): Stable merchant-native invoice line reference, unique within the invoice.
- `invoice.lines.quantity` (integer | integer | integer | integer | integer | integer, required): Quantity (integer).
- `invoice.lines.subtotalMinor` (integer | integer | integer | integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.lines.discountMinor` (integer | integer | integer | integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.lines.taxMinor` (integer | integer | integer | integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.lines.totalMinor` (integer | integer | integer | integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.lines.periodStartsAt` (date-time | null | date-time | null | date-time | null | date-time | null | date-time | null | date-time | null, required): Timestamp (ISO 8601).
- `invoice.lines.periodEndsAt` (date-time | null | date-time | null | date-time | null | date-time | null | date-time | null | date-time | null, required): Timestamp (ISO 8601).
- `invoice.lines.priceId` (uuid | uuid | uuid, optional): Longplay Price UUID for this invoice line, matching the original quote.
- `invoice.lines.nativePrice` (object | object | object, optional): Native price reference on a renewal line, supplied in place of priceId. Confirmed operations use the quoted priceId.
- `invoice.lines.nativePrice.merchantPriceId` (string | string | string, required): Native price identifier in the merchant catalog, resolved within the current App and environment.
- `invoice.lines.nativePrice.productId` (string | string | string, required): Product ID.
- `invoice.lines.nativePrice.kind` (enum | enum | enum, required): Kind; see the allowed values.
- `invoice.lines.nativePrice.currency` (string | string | string, required): Currency code supported by the published price.
- `invoice.lines.nativePrice.unitAmountMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `invoice.lines.nativePrice.interval` (enum | enum | enum, required): Interval; see the allowed values.
- `invoice.lines.nativePrice.intervalCount` (integer | integer | integer, required): Stripe Price billing interval count, at most 52.
- `subscription` (object | null | object | object, optional): Merchant-verified subscription snapshot; null for a purchase without a subscription.
- `subscription.merchantSubscriptionId` (string | string | string, required): Merchant-native subscription reference represented by this snapshot.
- `subscription.revision` (integer | integer | integer, required): Revision of the specific versioned resource; Customer profile updates have no revision.
- `subscription.status` (enum | enum | enum, required): Status; see the allowed values.
- `subscription.periodStartsAt` (date-time | date-time | date-time, required): Timestamp (ISO 8601).
- `subscription.periodEndsAt` (date-time | date-time | date-time, required): Timestamp (ISO 8601).
- `subscription.cancelAtPeriodEnd` (boolean | boolean | boolean, required): Whether it cancels at the end of the current period.
- `subscription.hasDiscounts` (boolean | boolean | boolean, required): Whether the merchant subscription has discounts, used to determine whether subsequent changes can use standard pricing.
- `subscription.items` (array | array | array, required): List items.
- `subscription.items.merchantItemId` (string | string | string | string | string | string, required): Stable merchant-native subscription item reference used to bind later changes to the original item.
- `subscription.items.quantity` (integer | integer | integer | integer | integer | integer, required): Quantity (integer).
- `subscription.items.priceId` (uuid | uuid | uuid, optional): Longplay Price UUID represented by this subscription item.
- `subscription.items.nativePrice` (object | object | object, optional): Native price reference on an autonomous subscription observation, supplied in place of priceId. Confirmed operations use the quoted priceId.
- `subscription.items.nativePrice.merchantPriceId` (string | string | string, required): Native price identifier in the merchant catalog, resolved within the current App and environment.
- `subscription.items.nativePrice.productId` (string | string | string, required): Product ID.
- `subscription.items.nativePrice.kind` (enum | enum | enum, required): Kind; see the allowed values.
- `subscription.items.nativePrice.currency` (string | string | string, required): Currency code supported by the published price.
- `subscription.items.nativePrice.unitAmountMinor` (integer | integer | integer, required): Amount in the currency's minor unit, such as cents.
- `subscription.items.nativePrice.interval` (enum | enum | enum, required): Interval; see the allowed values.
- `subscription.items.nativePrice.intervalCount` (integer | integer | integer, required): Stripe Price billing interval count, at most 52.
- `status` (enum | enum | enum | enum | enum | enum | enum | enum, optional): Status; see the allowed values.
- `scheduledFor` (date-time | null, optional): Effective timestamp of the accepted scheduled subscription change, matching its original instruction.
- `merchantSubscriptionId` (string | string | null, optional): Stable merchant-native subscription reference joining subsequent observations for that subscription.
- `merchantInvoiceId` (string | null | string, optional): Stable merchant-native Invoice reference identifying the refunded Invoice for a native refund without an operation, including dispute funds.
- `merchantRefundId` (string, optional): Stable merchant-native refund reference retained across retries of the same refund. minLength: 1 · maxLength: 200
- `amountMinor` (integer, optional): Amount in the currency's minor unit, such as cents. min: 0 · max: 100000000
- `currency` (string, optional): Currency code supported by the published price. pattern: ^[A-Z]{3}$
- `merchantFulfillmentId` (string, optional): Stable merchant-native fulfilment reference. minLength: 1 · maxLength: 200
- `merchantBenefitId` (string | string | string, optional): Stable merchant-native benefit reference joining later facts for that benefit.
- `resource` (string | string | string | string, optional): Merchant-native resource reference, such as a credit balance or a model.
- `unit` (string | string | string | string, optional): Merchant-native unit of the quantity.
- `quantity` (integer | integer | integer, optional): Quantity (integer).
- `expiresAt` (date-time | null | date-time | null, optional): Expiry time (ISO 8601, UTC).
- `revision` (integer | integer | integer | integer | integer, optional): Revision of the specific versioned resource; Customer profile updates have no revision.
- `remainingQuantity` (integer | null, optional): Quantity left on the benefit, or null when the merchant cannot report it.
- `merchantAuthorizationId` (string, optional): Stable merchant-native authorization reference joining later states of that authorization. minLength: 1 · maxLength: 200
- `kind` ("auto_topup", optional): Kind; see the allowed values.
- `termsReference` (string | null, optional): Merchant-native terms reference bound to this authorization, or null when there is none.
- `availableQuantity` (integer, optional): Quantity currently available in the merchant-native balance. min: 0 · max: 1000000000000
- `reservedQuantity` (integer, optional): Quantity reserved in the merchant-native balance and not yet completed. min: 0 · max: 1000000000000
- `periodStartsAt` (date-time | null, optional): Timestamp (ISO 8601).
- `periodEndsAt` (date-time | null, optional): Timestamp (ISO 8601).
- `grantId` (uuid | uuid, optional): grant ID.
- `productVersionId` (uuid, optional): Product Version ID.
- `subscriptionId` (string, optional): subscription ID. minLength: 1 · maxLength: 255
- `merchandiseId` (uuid, optional): Internal UUID of registered merchandise in the current App and environment.
- `merchantTransactionId` (string | string, optional): Stable merchant-native usage transaction reference used to deduplicate the fact.
- `operation` ("consume" | "reverse", optional): The operation to run; it selects the other fields.
- `reversalOf` (string, optional): Native usage transaction that this reversal undoes. minLength: 1 · maxLength: 200

<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"`.
- `record` (object, required): Receipt for the merchant event durably recorded by Longplay.
- `record.id` (uuid, required): Longplay merchant-event record UUID, retained by idempotent replays.
- `record.eventId` (string, required): Event ID used for deduplication; unchanged on retries. minLength: 1 · maxLength: 200
- `record.source` ("merchant", required): The merchant source marker identifying facts submitted by the merchant.
- `record.providerVerified` (false, required): Always false: this receipt represents merchant-submitted facts without direct provider verification by Longplay.
- `record.outcome` (string, required): Durable event outcome containing the associated business objects and processing state. minLength: 1 · maxLength: 100

<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 `billing:write`. | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` or `X-Indiana-Environment` doesn't match the credential's App and environment. | `check_scope` |
| `CONFLICT` | 409 | The same `eventId` was sent with different input (`reason: IDEMPOTENCY_CONFLICT`). | `resolve_conflict` |
| `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; read the original operation or replay the original ID and input. | `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#commerce)
