Skip to content
Hardcount
Run it locally
Pre-1.0 · one maintainer · Stripe and cards only · no SDKs (client libraries) yet · no production users yet. Docs describe v0.12.0 (30 Nov 2026) · What’s new →

Send a batch of usage events

POST
/v1/usage-events/batch
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 /v1/usage-events.

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_key; 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_ingest_…) 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.

Media typeapplication/json
Array<object>
object
external_customer_id
required

The customer’s external_id (your identifier), not the internal hc_cus_ id.

string
event_name
required

The meter’s event_name (e.g. “tokens”).

string
quantity
required

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.

One of:
number
dimensions

Scalar labels of the event, at most 16. A price takes an event whose labels include the price’s dimension_match.

object
key
additional properties
any
idempotency_key
required

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.

string
<= 255 characters
timestamp

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 /v1/usage-events apply.

string format: date-time

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"
}
]

Batch committed

Media typeapplication/json

What POST /v1/usage-events/batch wrote: how many events were new, how many were already stored, and one result per event in the order sent.

object
ingested
required

Newly written events.

integer
deduplicated
required

Idempotency replays — already ingested, counted as success.

integer
total
required

Events in the request.

integer
after_close
required

Of ingested, the events answered billing_status after_close.

integer
no_price
required

Of ingested, the events answered billing_status no_price.

integer
not_on_plan
required

Of ingested, the events answered billing_status not_on_plan.

integer
outside_subscription
required

Of ingested, the events answered billing_status outside_subscription.

integer
results
required

One entry per event, in the order sent: results[i] is the request’s event i.

Array<object>

What the batch did with one of its events.

object
status
required

ingested: the batch wrote the event. deduplicated: an event with its idempotency_key was already stored, earlier or in this batch, so it was not stored again.

string
Allowed values: ingested deduplicated
billing_status

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_close: the event’s time is already invoiced. Bill the event with a one-off invoice if it should be charged.
  • not_on_plan: a billing period not invoiced yet holds the event’s time, but its plan has no price for this meter.
  • outside_subscription: 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.
  • no_price: the plan that bills the event’s time prices this meter, but no price of that plan takes the event’s dimensions. A price takes an event whose dimensions include all of the price’s dimension_match labels. A price’s labels are text, so a number or a true/false value in the event never matches: the number 1 does not match the string "1".

No billing_status 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.

string
Allowed values: after_close no_price not_on_plan outside_subscription

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_request: 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.

Media typeapplication/json

The body of an error answer. error.type is the broad class, error.code is the exact reason for your code to branch on, and error.message is a sentence for a person. Quote error.request_id when you ask for help.

object
error

What went wrong.

object
type
required

One of invalid_request_error, authentication_error, rate_limit_error or api_error. Every error the API answers has this shape, including an unknown path (404, code route_not_found) and a method the path does not take (405, code method_not_allowed, with an Allow header). The few exceptions are listed in Errors.

string
code
required

The exact reason, such as validation_error, not_found, already_exists, invalid_state or a more specific code the operation lists. Branch on this, not on message.

string
message
required

What went wrong, written for a person to read.

string
request_id
required

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.

string
param

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.

string

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.

Media typeapplication/json

The body of an error answer. error.type is the broad class, error.code is the exact reason for your code to branch on, and error.message is a sentence for a person. Quote error.request_id when you ask for help.

object
error

What went wrong.

object
type
required

One of invalid_request_error, authentication_error, rate_limit_error or api_error. Every error the API answers has this shape, including an unknown path (404, code route_not_found) and a method the path does not take (405, code method_not_allowed, with an Allow header). The few exceptions are listed in Errors.

string
code
required

The exact reason, such as validation_error, not_found, already_exists, invalid_state or a more specific code the operation lists. Branch on this, not on message.

string
message
required

What went wrong, written for a person to read.

string
request_id
required

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.

string
param

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.

string

Example

{
"error": {
"type": "authentication_error",
"code": "unauthorized",
"message": "invalid or expired API key",
"request_id": "req_dbjg652q3gpgqfoq5cd0"
}
}

Code batch_too_large: the request body is over the 1 MB cap (1,048,576 bytes). Nothing was ingested; send smaller batches.

Media typeapplication/json

The body of an error answer. error.type is the broad class, error.code is the exact reason for your code to branch on, and error.message is a sentence for a person. Quote error.request_id when you ask for help.

object
error

What went wrong.

object
type
required

One of invalid_request_error, authentication_error, rate_limit_error or api_error. Every error the API answers has this shape, including an unknown path (404, code route_not_found) and a method the path does not take (405, code method_not_allowed, with an Allow header). The few exceptions are listed in Errors.

string
code
required

The exact reason, such as validation_error, not_found, already_exists, invalid_state or a more specific code the operation lists. Branch on this, not on message.

string
message
required

What went wrong, written for a person to read.

string
request_id
required

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.

string
param

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.

string

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_rejected) 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_key, 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.

Media typeapplication/json
object
error

What went wrong.

object
type
required

One of invalid_request_error, authentication_error, rate_limit_error or api_error. Every error the API answers has this shape, including an unknown path (404, code route_not_found) and a method the path does not take (405, code method_not_allowed, with an Allow header). The few exceptions are listed in Errors.

string
code
required

The exact reason, such as validation_error, not_found, already_exists, invalid_state or a more specific code the operation lists. Branch on this, not on message.

string
message
required

What went wrong, written for a person to read.

string
request_id
required

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.

string
param

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.

string
errors
required

One entry per event that failed the round that refused the batch, event[i]: reason, where i is its index in the request.

Array<string>
ingested
required

Always 0. A rejected batch writes nothing.

integer
total
required

Events in the request.

integer

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_RATE_LIMIT_GENERAL_PER_MIN), and 1,000 a second on the usage routes (HARDCOUNT_RATE_LIMIT_INGEST_PER_SEC). The dashboard sessions of one account share one limit per mode. The routes under /v1/auth and /v1/bootstrap (sign-in, password reset, invite accept, setup) count per client address, by default 100 a minute (HARDCOUNT_RATE_LIMIT_AUTH_PER_MIN). The public invoice, payment-update and cost-dashboard pages allow 60 a minute per client address.

Media typeapplication/json

The body of an error answer. error.type is the broad class, error.code is the exact reason for your code to branch on, and error.message is a sentence for a person. Quote error.request_id when you ask for help.

object
error

What went wrong.

object
type
required

One of invalid_request_error, authentication_error, rate_limit_error or api_error. Every error the API answers has this shape, including an unknown path (404, code route_not_found) and a method the path does not take (405, code method_not_allowed, with an Allow header). The few exceptions are listed in Errors.

string
code
required

The exact reason, such as validation_error, not_found, already_exists, invalid_state or a more specific code the operation lists. Branch on this, not on message.

string
message
required

What went wrong, written for a person to read.

string
request_id
required

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.

string
param

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.

string

Example

{
"error": {
"type": "rate_limit_error",
"code": "rate_limited",
"message": "too many requests — please retry after the rate limit resets",
"request_id": "req_dbjg652q3gpgqfoq5cd0"
}
}
Retry-After
integer
Example
12

Seconds to wait before the next request can pass, rounded up to a whole second and at least 1.

X-RateLimit-Limit
integer
Example
1000

How many requests the limit allows in its period (a minute, or a second on the usage routes).

X-RateLimit-Remaining
integer
Example
0

How many requests are left in the current period.

X-RateLimit-Reset
integer format: int64
Example
1793870460

When the whole limit is available again, as Unix seconds. One request may pass sooner; Retry-After says when.

ingest_unavailable: 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.

Media typeapplication/json

The body of an error answer. error.type is the broad class, error.code is the exact reason for your code to branch on, and error.message is a sentence for a person. Quote error.request_id when you ask for help.

object
error

What went wrong.

object
type
required

One of invalid_request_error, authentication_error, rate_limit_error or api_error. Every error the API answers has this shape, including an unknown path (404, code route_not_found) and a method the path does not take (405, code method_not_allowed, with an Allow header). The few exceptions are listed in Errors.

string
code
required

The exact reason, such as validation_error, not_found, already_exists, invalid_state or a more specific code the operation lists. Branch on this, not on message.

string
message
required

What went wrong, written for a person to read.

string
request_id
required

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.

string
param

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.

string

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