Skip to Content
Orbyte Labs

Check and track

Check before work; record usage once it completes.

TypeScript
import { check, credits, track } from "orbytelabs";

const access = await check("requests", customerId, {
  minimumBalance: credits("0.01"),
});
if (!access.allowed) throw new Error(access.reason);

await performWork();
await track("requests", customerId, undefined, {
  idempotencyKey: requestId,
});

Use your authenticated customer identity, never an untrusted request field. Shortcuts read ORBYTE_API_KEY when called.

Checks

check returns allowed, availableCredits as a bigint, asOf, and optional nextChangeAt. Denial reasons are feature_unavailable and insufficient_credits. Authentication and API errors reject the promise.

Without minimumBalance, access needs a positive matching grant balance. Count and distinct checks also verify the deployed per-event cost. Distinct checks take the same grouping payload as tracking. Recheck at nextChangeAt for time-based changes.

A check reserves no funds. Concurrent work can spend the balance before tracking. Your server must handle a rejected final charge.

Tracking and retries

TypeScript
const eventId = await track(
  "tokens",
  customerId,
  { tokens: 1200 },
  {
    idempotencyKey: "generation_123",
  },
);

Tracking prices the current feature, checks eligible grants, and records the event and debit atomically. It returns an event ID. Insufficient credits reject the request.

Supply a stable key for a retryable operation. The same key and unchanged terms return the original event without charging again. Different terms conflict. The shortcut generates a UUID when omitted; that is useful only for calls you will not retry. The SDK does not retry requests automatically.

After an uncertain response, retry with the same key and payload. Never invent a new key for the same work just because the response was lost.

Event costs and margins

Pass optional costUsdInNanos as a nonnegative bigint to record your application's expense in nano dollars. Use usd("0.004") for 4_000_000n. One dollar is 1,000,000,000 nano dollars, matching the credit scale. Omitted means unknown; 0n means a known zero expense. JSON accepts integer strings. Recording expense does not change customer pricing.

Read the event with client.events.get(eventId) or GET /v1/events/{id} using events:read. It returns resolved value, the original credits deducted from the ledger, costUsdInNanos, revenueUsdInNanos, and marginUsdInNanos. Native Convex callers use events.index.get; product event lists include the same financial fields.

All event financial fields use nano dollars. Revenue allocates each consumed grant's paid amount across its original credits, then adds the value of the portions used by this event. A $20 grant of 100 credits contributes 20 cents per credit consumed. An event consuming credits from several grants uses each grant's own rate. Subscription grants snapshot the paid invoice amount, including discounts. Top-ups snapshot the checkout payment. Manual and welcome grants have $0 revenue. Unspent or expired credits are not reallocated to other events.

marginUsdInNanos is revenueUsdInNanos - costUsdInNanos. Financial values are bigint in the SDK and Convex, and integer strings in JSON. Each grant allocation uses integer division, truncating below one nano dollar. The entire paid invoice is allocated to its plan grant, including tax and any other invoice items. Payment fees, later refunds, and accounting adjustments are not deducted. This is a usage valuation policy, not accounting revenue recognition. Old events retain their original credit charge but receive no inferred USD valuation. Zero-charge events have $0 revenue.

Costs, grant consumption, and event revenue are recorded atomically. Retry with the same cost and decimal representation as the original input. Omitted and zero costs are different requests. Redeploying prices, grant expiry, or subscription cancellation does not change a recorded event's amounts.

On this page