# Check and track

Source: https://docs.orbytelabs.com/sdk/events

Authorize billable work and record completed usage.

The shortcuts read `ORBYTE_API_KEY` from your server environment:

```typescript title="TypeScript"
import { check, track, usd } from "orbytelabs";

const identity = "customer_123";
const access = await check("requests", identity);
if (!access.allowed) throw new Error(access.reason);

// Perform the billable work here.
await track("requests", identity, undefined, {
  idempotencyKey: "request_123",
  costUsdInNanos: usd("0.0000001"),
});
```

An allowed check reserves no credits. Handle tracking failure even after the
work succeeds. Reuse the same key and unchanged input to retry an event; use a
new key for each new event.

## Explicit client [#explicit-client]

For a feature using `sum("tokens")`, pass the measured quantity as payload:

```typescript title="TypeScript"
import { Orbyte, credits, usd } from "orbytelabs";

const client = new Orbyte({ accessToken: process.env.ORBYTE_API_KEY });
const access = await client.events.check({
  featureKey: "tokens",
  identity: "customer_123",
  minimumBalance: credits("0.01"),
});

if (access.allowed) {
  await client.events.track({
    featureKey: "tokens",
    identity: "customer_123",
    payload: { tokens: 1200 },
    idempotencyKey: "generation_123",
  });
}
```

Tracking returns the event ID. Checks return `allowed`, an optional denial
`reason`, and `availableCredits`. See [checking and tracking](/checking-and-tracking)
for distinct payloads and [pricing](/pricing) for usage tracking rules.

## Event costs and margins [#event-costs-and-margins]

Pass optional `costUsdInNanos` as a nonnegative `bigint` in nano dollars.
Use `usd()` to convert dollars with up to nine decimal places. This records your
application's expense separately from customer pricing. Omit it when unknown or
send `0n` for a known zero expense. Replays must keep the same cost.

```typescript title="TypeScript"
const id = await client.events.track({
  featureKey: "tokens",
  identity: "customer_123",
  payload: { tokens: 1200 },
  costUsdInNanos: usd("0.000123456"),
  idempotencyKey: "generation_123",
});

const event = await client.events.get(id);
// event.value and event.credits are bigint values.
// Financial fields are optional bigint values in nano dollars.
// JSON encodes bigint values as integer strings.
```

Event reads require `events:read` and accept `{ environmentId }` as a second
argument. See [checking and tracking](/checking-and-tracking#event-costs-and-margins)
for grant valuation and historical data limits.