Send a batch of usage events
const url = 'http://localhost:8080/v1/usage-events/batch';const options = { method: 'POST', headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'}, body: '[{"external_customer_id":"acme-42","event_name":"tokens","quantity":1200,"dimensions":{"model":"gpt-4o","provider":"openai","token_type":"input"},"idempotency_key":"req-7f3a9c","timestamp":"2026-10-12T14:03:07Z"},{"external_customer_id":"acme-42","event_name":"tokens","quantity":350,"dimensions":{"model":"gpt-4o","provider":"openai","token_type":"output"},"idempotency_key":"req-7f3a9c-out","timestamp":"2026-10-12T14:03:07Z"}]'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}curl --request POST \ --url http://localhost:8080/v1/usage-events/batch \ --header 'Authorization: Bearer <token>' \ --header 'Content-Type: application/json' \ --data '[ { "external_customer_id": "acme-42", "event_name": "tokens", "quantity": 1200, "dimensions": { "model": "gpt-4o", "provider": "openai", "token_type": "input" }, "idempotency_key": "req-7f3a9c", "timestamp": "2026-10-12T14:03:07Z" }, { "external_customer_id": "acme-42", "event_name": "tokens", "quantity": 350, "dimensions": { "model": "gpt-4o", "provider": "openai", "token_type": "output" }, "idempotency_key": "req-7f3a9c-out", "timestamp": "2026-10-12T14:03:07Z" } ]'Records up to 1,000 usage events in one call, in a body of at most
1 MB. Each event has the same fields as POST /.
The batch is atomic: every event validates, then all of them
commit in one transaction. On any validation failure nothing is
ingested. Every event must carry its own idempotency_; an
event without one fails validation, so the whole batch is refused.
Retrying the whole batch is safe because of those keys: events
already stored are counted in deduplicated, not stored again,
and are not errors. A deduplicated count above zero on a batch
you never sent before means some keys were already used, by an
earlier event or by another event in the batch. Those events
were not stored. A usage-only key (hc_) or a secret key
can call it.
Events sent with no timestamp share the request’s time: they are
all dated when Hardcount receives the batch. A last meter bills
the event with the latest timestamp, so if the events of one
request must have an order, send each one’s timestamp.
Authorizations
Section titled “Authorizations”Request Bodyrequired
Section titled “Request Bodyrequired”object
The customer’s external_id (your identifier), not the internal hc_cus_ id.
The meter’s event_ (e.g. “tokens”).
A JSON number (5) or a string holding one ("5.5",
for exact decimals). At most 12 decimal places;
trailing zeros do not count. Its size must be under
10^26, and it is at most 100 characters. A negative
value is a correction, which a usage-only key cannot
send. A missing quantity, or any other value, fails
validation.
Scalar labels of the event, at most 16. A price takes an event whose labels include the price’s dimension_.
object
Your unique ID for this event. Pick it once, before the
first send, and send the same key on every retry of the
event. Keys are unique per account and mode (live or
test), forever. An event whose key was already used is
not stored again: the first event wins and the new body
is discarded. So never reuse a key for a different
event, such as a batch index or a timestamp that can
repeat, or that usage is lost. Empty or whitespace-only
keys fail validation, and so do keys longer than
255 characters. This is the only dedup key: the usage
routes ignore the Idempotency-Key header.
When the usage happened, as an RFC 3339 string. Leave
it out to use the time Hardcount receives the batch.
The same rules as for POST / apply.
Example
[ { "external_customer_id": "acme-42", "event_name": "tokens", "quantity": 1200, "dimensions": { "model": "gpt-4o", "provider": "openai", "token_type": "input" }, "idempotency_key": "req-7f3a9c", "timestamp": "2026-10-12T14:03:07Z" }, { "external_customer_id": "acme-42", "event_name": "tokens", "quantity": 350, "dimensions": { "model": "gpt-4o", "provider": "openai", "token_type": "output" }, "idempotency_key": "req-7f3a9c-out", "timestamp": "2026-10-12T14:03:07Z" }]Responses
Section titled “Responses”Batch committed
What POST / wrote: how many events were new,
how many were already stored, and one result per event in the order
sent.
object
Newly written events.
Idempotency replays — already ingested, counted as success.
Events in the request.
Of ingested, the events answered billing_ after_.
Of ingested, the events answered billing_ no_.
Of ingested, the events answered billing_ not_.
Of ingested, the events answered billing_ outside_.
One entry per event, in the order sent: results[i] is the
request’s event i.
What the batch did with one of its events.
object
ingested: the batch wrote the event. deduplicated: an event
with its idempotency_ was already stored, earlier or in
this batch, so it was not stored again.
What Hardcount found when the event arrived. It checked the event against the customer’s subscriptions, the plans and prices in force, and the invoices already built at that moment. Sent only on the answer that wrote the event; the event is stored all the same.
A status that is present was true at that moment:
after_: the event’s time is already invoiced. Bill the event with a one-off invoice if it should be charged.close not_: a billing period not invoiced yet holds the event’s time, but its plan has no price for this meter.on_ plan outside_: no active subscription of the customer bills the event’s time. For example, it has none, or the time is before one starts, or after one ends or its scheduled cancel.subscription no_: the plan that bills the event’s time prices this meter, but no price of that plan takes the event’sprice dimensions. A price takes an event whose dimensions include all of the price’sdimension_labels. A price’s labels are text, so a number or a true/false value in the event never matches: the numbermatch 1does not match the string"1".
No billing_ means Hardcount found no reason then. It is not a
promise that the event will be billed. Hardcount checks only the
customer’s current billing periods: time after a period end whose
invoice is not built yet is not checked. A later change can change
the outcome either way: a subscription created or changed, a
scheduled cancel undone, or a price added. The invoice is the final
answer.
Two cases can make a present status wrong: a price change whose save began before a billing period started and finished after the request read the prices, and a subscription change saved while the request is in flight. Absent on a replay and on a read.
Example
{ "ingested": 2, "deduplicated": 0, "total": 2, "after_close": 0, "no_price": 0, "not_on_plan": 0, "outside_subscription": 0, "results": [ { "status": "ingested" }, { "status": "ingested" } ]}Code invalid_: the body is not a JSON array of event
objects, or an event has a field of the wrong JSON type
(message expected JSON array of events); the array is empty
(at least one event is required); or it holds more than 1,000
events (maximum 1000 events per batch). Nothing was ingested.
The body of an error answer. error. is the broad class,
error. is the exact reason for your code to branch on, and
error. is a sentence for a person. Quote
error. when you ask for help.
object
What went wrong.
object
One of invalid_, authentication_,
rate_ or api_. Every error the API
answers has this shape, including an unknown path (404,
code route_) and a method the path does not take
(405, code method_, with an Allow header).
The few exceptions are listed in
Errors.
The exact reason, such as validation_, not_,
already_, invalid_ or a more specific code
the operation lists. Branch on this, not on message.
What went wrong, written for a person to read.
The id of the request that produced this answer
(req_…). It is normally the same value as the
Hardcount-Request-Id response header. On an answer
replayed for a repeated Idempotency-Key, the body is the
first request’s, so this is the first request’s id, while
the Hardcount-Request-Id header holds the new request’s
id.
The request field or query parameter the error is about. Sent only when the error names one.
A request body may hold only the fields its operation
lists, spelled and cased as listed. Any other field, or one
the server sets (such as id), is refused with 422 and
param naming it. An operation with no request body lists
no fields: send no body, {} or null. The Stripe and
Postmark webhooks and the LiteLLM callback take their
sender’s body as it comes.
Example
{ "error": { "type": "invalid_request_error", "code": "invalid_request", "message": "maximum 1000 events per batch", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}No valid credential: the API key or session is missing, wrong,
expired or revoked. With no API key and no valid session cookie, the
message starts with missing credentials. A wrong, expired and
revoked key all get the same message, invalid or expired API key.
The body of an error answer. error. is the broad class,
error. is the exact reason for your code to branch on, and
error. is a sentence for a person. Quote
error. when you ask for help.
object
What went wrong.
object
One of invalid_, authentication_,
rate_ or api_. Every error the API
answers has this shape, including an unknown path (404,
code route_) and a method the path does not take
(405, code method_, with an Allow header).
The few exceptions are listed in
Errors.
The exact reason, such as validation_, not_,
already_, invalid_ or a more specific code
the operation lists. Branch on this, not on message.
What went wrong, written for a person to read.
The id of the request that produced this answer
(req_…). It is normally the same value as the
Hardcount-Request-Id response header. On an answer
replayed for a repeated Idempotency-Key, the body is the
first request’s, so this is the first request’s id, while
the Hardcount-Request-Id header holds the new request’s
id.
The request field or query parameter the error is about. Sent only when the error names one.
A request body may hold only the fields its operation
lists, spelled and cased as listed. Any other field, or one
the server sets (such as id), is refused with 422 and
param naming it. An operation with no request body lists
no fields: send no body, {} or null. The Stripe and
Postmark webhooks and the LiteLLM callback take their
sender’s body as it comes.
Example
{ "error": { "type": "authentication_error", "code": "unauthorized", "message": "invalid or expired API key", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}Code batch_: the request body is over the 1 MB cap
(1,048,576 bytes). Nothing was ingested; send smaller batches.
The body of an error answer. error. is the broad class,
error. is the exact reason for your code to branch on, and
error. is a sentence for a person. Quote
error. when you ask for help.
object
What went wrong.
object
One of invalid_, authentication_,
rate_ or api_. Every error the API
answers has this shape, including an unknown path (404,
code route_) and a method the path does not take
(405, code method_, with an Allow header).
The few exceptions are listed in
Errors.
The exact reason, such as validation_, not_,
already_, invalid_ or a more specific code
the operation lists. Branch on this, not on message.
What went wrong, written for a person to read.
The id of the request that produced this answer
(req_…). It is normally the same value as the
Hardcount-Request-Id response header. On an answer
replayed for a repeated Idempotency-Key, the body is the
first request’s, so this is the first request’s id, while
the Hardcount-Request-Id header holds the new request’s
id.
The request field or query parameter the error is about. Sent only when the error names one.
A request body may hold only the fields its operation
lists, spelled and cased as listed. Any other field, or one
the server sets (such as id), is refused with 422 and
param naming it. An operation with no request body lists
no fields: send no body, {} or null. The Stripe and
Postmark webhooks and the LiteLLM callback take their
sender’s body as it comes.
Example
{ "error": { "type": "invalid_request_error", "code": "batch_too_large", "message": "request body exceeds 1048576 bytes — send smaller batches", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}Batch rejected — one or more events failed validation and
nothing was ingested. The body is the error envelope (code
batch_) plus errors[] (event[i]: reason),
ingested: 0 and total.
The events are checked in two rounds, and errors[] lists the
failures of one round only. The first round checks every event
for: a missing field, a missing, blank or too long
idempotency_, an unknown field or a properties key, the
quantity format and length, the timestamp format, and an
unknown customer or meter. The second round runs only when no
event failed the first, and checks: more than 16 dimensions or a
value that is not a scalar, a quantity of 10^26 or more or with
more than 12 decimal places, and a timestamp more than 5
minutes ahead. So a fixed batch can be refused again, for its
second-round failures. Fix the listed events and retry the
whole batch.
object
What went wrong.
object
One of invalid_, authentication_,
rate_ or api_. Every error the API
answers has this shape, including an unknown path (404,
code route_) and a method the path does not take
(405, code method_, with an Allow header).
The few exceptions are listed in
Errors.
The exact reason, such as validation_, not_,
already_, invalid_ or a more specific code
the operation lists. Branch on this, not on message.
What went wrong, written for a person to read.
The id of the request that produced this answer
(req_…). It is normally the same value as the
Hardcount-Request-Id response header. On an answer
replayed for a repeated Idempotency-Key, the body is the
first request’s, so this is the first request’s id, while
the Hardcount-Request-Id header holds the new request’s
id.
The request field or query parameter the error is about. Sent only when the error names one.
A request body may hold only the fields its operation
lists, spelled and cased as listed. Any other field, or one
the server sets (such as id), is refused with 422 and
param naming it. An operation with no request body lists
no fields: send no body, {} or null. The Stripe and
Postmark webhooks and the LiteLLM callback take their
sender’s body as it comes.
One entry per event that failed the round that
refused the batch, event[i]: reason, where i
is its index in the request.
Always 0. A rejected batch writes nothing.
Events in the request.
Example
{ "error": { "type": "invalid_request_error", "code": "batch_rejected", "message": "batch rejected — nothing was ingested; fix the listed events and retry the whole batch", "request_id": "req_dbjg652q3gpgqfoq5cd0" }, "errors": [ "event[1]: idempotency_key is required — give each event its own key and send the same key when you retry that event" ], "ingested": 0, "total": 2}Too many requests. Wait for the number of seconds in Retry-After,
then retry. The answers that pass the limit carry the
X-RateLimit-* headers too.
Each API key has its own limit, by default 1,000 requests a minute
(HARDCOUNT_), and 1,000 a second on the
usage routes (HARDCOUNT_). The dashboard
sessions of one account share one limit per mode. The routes under
/ and / (sign-in, password reset, invite
accept, setup) count per client address, by default 100 a minute
(HARDCOUNT_). The public invoice,
payment-update and cost-dashboard pages allow 60 a minute per client
address.
The body of an error answer. error. is the broad class,
error. is the exact reason for your code to branch on, and
error. is a sentence for a person. Quote
error. when you ask for help.
object
What went wrong.
object
One of invalid_, authentication_,
rate_ or api_. Every error the API
answers has this shape, including an unknown path (404,
code route_) and a method the path does not take
(405, code method_, with an Allow header).
The few exceptions are listed in
Errors.
The exact reason, such as validation_, not_,
already_, invalid_ or a more specific code
the operation lists. Branch on this, not on message.
What went wrong, written for a person to read.
The id of the request that produced this answer
(req_…). It is normally the same value as the
Hardcount-Request-Id response header. On an answer
replayed for a repeated Idempotency-Key, the body is the
first request’s, so this is the first request’s id, while
the Hardcount-Request-Id header holds the new request’s
id.
The request field or query parameter the error is about. Sent only when the error names one.
A request body may hold only the fields its operation
lists, spelled and cased as listed. Any other field, or one
the server sets (such as id), is refused with 422 and
param naming it. An operation with no request body lists
no fields: send no body, {} or null. The Stripe and
Postmark webhooks and the LiteLLM callback take their
sender’s body as it comes.
Example
{ "error": { "type": "rate_limit_error", "code": "rate_limited", "message": "too many requests — please retry after the rate limit resets", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}Headers
Section titled “Headers”Example
12Seconds to wait before the next request can pass, rounded up to a whole second and at least 1.
Example
1000How many requests the limit allows in its period (a minute, or a second on the usage routes).
Example
0How many requests are left in the current period.
Example
1793870460When the whole limit is available again, as Unix seconds. One
request may pass sooner; Retry-After says when.
ingest_: a customer or meter lookup, the read of a
customer’s billing periods, the read of a meter’s aggregation,
or the write could not be answered. Retry the whole batch with
the same keys after Retry-After (1 second): each event is
recorded once.
The body of an error answer. error. is the broad class,
error. is the exact reason for your code to branch on, and
error. is a sentence for a person. Quote
error. when you ask for help.
object
What went wrong.
object
One of invalid_, authentication_,
rate_ or api_. Every error the API
answers has this shape, including an unknown path (404,
code route_) and a method the path does not take
(405, code method_, with an Allow header).
The few exceptions are listed in
Errors.
The exact reason, such as validation_, not_,
already_, invalid_ or a more specific code
the operation lists. Branch on this, not on message.
What went wrong, written for a person to read.
The id of the request that produced this answer
(req_…). It is normally the same value as the
Hardcount-Request-Id response header. On an answer
replayed for a repeated Idempotency-Key, the body is the
first request’s, so this is the first request’s id, while
the Hardcount-Request-Id header holds the new request’s
id.
The request field or query parameter the error is about. Sent only when the error names one.
A request body may hold only the fields its operation
lists, spelled and cased as listed. Any other field, or one
the server sets (such as id), is refused with 422 and
param naming it. An operation with no request body lists
no fields: send no body, {} or null. The Stripe and
Postmark webhooks and the LiteLLM callback take their
sender’s body as it comes.
Example
{ "error": { "type": "api_error", "code": "ingest_unavailable", "message": "usage store temporarily unavailable — retry the request with the same idempotency keys; each event is recorded once", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}Stuck, or found a bug? Ask in Discussions · Open an issue