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
Set up a strategy
Every strategy follows the same pattern:Ingestion(...)connects to Polar..strategy(...)picks what to measure..ingest("event_name")names the events it sends..client({ externalCustomerId: "user_123" })returns the wrapped client for one customer. Use it like the original.
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:
S3
Wrap anS3Client to send the bytes of every upload.
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..client().
Event
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
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.
nameis the span name.metadataholds the span’s string, number and boolean attributes.external_idis the span ID, andparent_idis the parent span’s ID.
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.

