Skip to main content
An ingestion strategy wraps a client you already use, like an S3 client or a stream, and sends a usage event each time it does metered work. You don’t write events.ingest calls yourself. Strategies come from the @polar-sh/ingestion package, for TypeScript only. For Python, or for events no strategy covers, send events with the SDK. See Send events.

Install

The package requires Node.js 24 or later.

Set up a strategy

Every strategy follows the same pattern:
  1. Ingestion(...) connects to Polar.
  2. .strategy(...) picks what to measure.
  3. .ingest("event_name") names the events it sends.
  4. .client({ externalCustomerId: "user_123" }) returns the wrapped client for one customer. Use it like the original.
Pass customerId instead of externalCustomerId to identify the customer by Polar’s ID. Connect with an access token:
accessToken defaults to the POLAR_ACCESS_TOKEN environment variable. Each event is sent in its own request to the Ingest Events API. One trade-off: a strategy never throws when sending fails. It logs the error with console.error and your request carries on. Your user isn’t blocked by a Polar outage, but a failed event is lost. To attach what each event cost you, add .cost(). It receives the strategy’s context and returns an amount in cents:
See Cost insights for how costs are reported.

S3

Wrap an S3Client to send the bytes of every upload.
Each request with a body sends one event. Requests with a Content-Length of zero, like reads, send nothing. The wrapped client is a copy, so the original s3Client stays untracked.
Event
bucket, key and contentType are included when the command sets them. Meter: filter name equals s3_upload, Sum over bytes. To display prices per gigabyte, use a Custom unit labeled GB. See Meters.

Stream

Wrap a Node.js stream to send the bytes that pass through it. The event is sent when the stream ends.
Create one strategy per stream. A stream can only be read once, so it’s tracked for the first customer you pass to .client().
Event
Meter: filter name equals file_streamed, Sum over bytes.

Delta time

Measure how long something takes. You provide the clock, so you choose the unit.
start() reads the clock. stop() reads it again, sends the difference as deltaTime and returns it. With performance.now() or Date.now(), deltaTime is in milliseconds.
Event
Meter: filter name equals execution_time, Sum over deltaTime.

OpenTelemetry

If you already trace your application with OpenTelemetry, PolarTraceExporter turns spans into events. Add a customerId or externalCustomerId attribute to the spans you want to send. Spans without one are skipped.
Each span becomes one event:
  • name is the span name.
  • metadata holds the span’s string, number and boolean attributes.
  • external_id is the span ID, and parent_id is the parent span’s ID.
Parent and child spans become parent and child events, which group a request’s costs into a trace.
Set the customer attribute on the root span too. A child span’s event points at its parent with parent_id. If the parent span is skipped, Polar never receives the parent, and the child event stays unresolved and isn’t processed further.
Attribute names with dots, like gen_ai.usage.input_tokens, are stored as they are. Meter paths read dots as nesting, so set plain attribute names for anything you want to meter on.

Learn more