Send a usage event
const url = 'http://localhost:8080/v1/usage-events';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"}'};
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 \ --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" }'Records one usage event for a customer and a meter. You name the
customer by your external_ for it and the meter by its
event_. The event counts toward the billing period its
timestamp falls in, and that period’s invoice bills it.
Every event needs its own idempotency_. Send the same key when
you retry the event: an event whose key is already stored is not
stored again, and the call answers 200 with the first event and an
Idempotent-Replayed: true header. Retrying is safe this way; a
new key on each retry bills the usage twice.
The 201 can carry a billing_: what Hardcount found when the
event arrived, such as a time that is already invoiced. The event
is stored all the same.
A usage-only key (hc_) or a secret key can call it.
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, is refused with
422 and param: quantity.
Scalar labels of the event (e.g. {“model”, “token_type”}). 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 are refused with 422, and so are 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 event. It may
be at most 5 minutes ahead of that time. Any past time is
accepted, but an event in a period that is already
invoiced is stored and not billed (billing_). A value that is not an RFC 3339 string is
refused with 422 and param: timestamp.
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"}Responses
Section titled “Responses”Idempotency replay — the original event is returned unchanged
with an Idempotent-Replayed: true header. No duplicate row
is written. The new body is not compared with the original.
One usage event: a quantity of one meter’s usage, for one customer,
at one time. Returned by POST / and
POST /, and listed by
GET /. An event counts toward the billing period
its timestamp falls in.
object
The event’s ID (hc_).
The account the event belongs to.
The customer the event was recorded for (hc_): the
customer whose external_ you sent as
external_.
The meter the event counts toward (hc_): the meter whose
event_ you sent.
Exact quantity as a decimal string (e.g. “1.5”).
The dimension values sent with the event. Absent when none were sent.
object
The key the event was sent with. Hardcount keeps one event per key in each account and mode (live or test), with no expiry: an event sent later with the same key is not stored again, however long after. Absent on an event stored without a key.
When the usage happened. It decides which billing period the
event counts toward. An event sent without timestamp is
stamped with the time Hardcount received it. An event that is
not a backfill may be at most 5 minutes ahead of that time. There is no limit on how far
back it goes, but an event in a period whose invoice is already
built is stored and not billed (see billing_).
How the event arrived. api: POST /, the batch
route or the LiteLLM integration. backfill:
POST /, or released held usage.
Absent on the rows of GET /, which does not
return it.
What serving this event cost you at your provider, in US cents as
a decimal string (e.g. “0.45”), set when the event arrived: from
your provider cost rates, or the cost LiteLLM reported for the
call (see provider_). Absent when no rate matched
or the cost was too large to store, and when the source is
not_.
Where provider_ came from: table (your provider
cost rates), observed (the cost LiteLLM reported for the
call), or not_: the event lacks a provider or
token_ dimension, or has neither model nor model_,
so no rate can apply. Absent when the event has those
dimensions but no rate matched.
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
{ "id": "hc_evt_db6eh6qq3gpgqfoq5c3g", "tenant_id": "hc_ten_dajqj02q3gpgqfoq5c00", "customer_id": "hc_cus_dakhnk2q3gpgqfoq5c0g", "meter_id": "hc_mtr_dajr122q3gpgqfoq5c10", "quantity": "1200", "dimensions": { "model": "gpt-4o", "provider": "openai", "token_type": "input" }, "idempotency_key": "req-7f3a9c", "timestamp": "2026-10-12T14:03:07Z", "origin": "api", "provider_cost_cents": "0.3", "provider_cost_source": "table"}Event ingested. A billing_ is what Hardcount found when the
event arrived (see the billing_ field); the event is stored
all the same.
One usage event: a quantity of one meter’s usage, for one customer,
at one time. Returned by POST / and
POST /, and listed by
GET /. An event counts toward the billing period
its timestamp falls in.
object
The event’s ID (hc_).
The account the event belongs to.
The customer the event was recorded for (hc_): the
customer whose external_ you sent as
external_.
The meter the event counts toward (hc_): the meter whose
event_ you sent.
Exact quantity as a decimal string (e.g. “1.5”).
The dimension values sent with the event. Absent when none were sent.
object
The key the event was sent with. Hardcount keeps one event per key in each account and mode (live or test), with no expiry: an event sent later with the same key is not stored again, however long after. Absent on an event stored without a key.
When the usage happened. It decides which billing period the
event counts toward. An event sent without timestamp is
stamped with the time Hardcount received it. An event that is
not a backfill may be at most 5 minutes ahead of that time. There is no limit on how far
back it goes, but an event in a period whose invoice is already
built is stored and not billed (see billing_).
How the event arrived. api: POST /, the batch
route or the LiteLLM integration. backfill:
POST /, or released held usage.
Absent on the rows of GET /, which does not
return it.
What serving this event cost you at your provider, in US cents as
a decimal string (e.g. “0.45”), set when the event arrived: from
your provider cost rates, or the cost LiteLLM reported for the
call (see provider_). Absent when no rate matched
or the cost was too large to store, and when the source is
not_.
Where provider_ came from: table (your provider
cost rates), observed (the cost LiteLLM reported for the
call), or not_: the event lacks a provider or
token_ dimension, or has neither model nor model_,
so no rate can apply. Absent when the event has those
dimensions but no rate matched.
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
{ "id": "hc_evt_db6eh6qq3gpgqfoq5c3g", "tenant_id": "hc_ten_dajqj02q3gpgqfoq5c00", "customer_id": "hc_cus_dakhnk2q3gpgqfoq5c0g", "meter_id": "hc_mtr_dajr122q3gpgqfoq5c10", "quantity": "1200", "dimensions": { "model": "gpt-4o", "provider": "openai", "token_type": "input" }, "idempotency_key": "req-7f3a9c", "timestamp": "2026-10-12T14:03:07Z", "origin": "api", "provider_cost_cents": "0.3", "provider_cost_source": "table"}The body is not one valid JSON value, it is not a JSON object, it
is empty where the operation needs one, or a field has the wrong
JSON type (such as a string where a number belongs). This answer
is not kept for an Idempotency-Key: send the corrected request
with the same 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": "invalid_request_error", "code": "invalid_request", "message": "invalid JSON body", "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" }}The body is over the 1 MB limit. Send a smaller body. The answer is
the same with or without an Idempotency-Key, and it is not kept
for the key: the same key works for the smaller request.
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": "payload_too_large", "message": "The request body is larger than the 1 MB limit. Send a smaller body.", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}Validation failed and nothing was stored. param names the
field: idempotency_ when it is missing, blank or longer
than 255 characters, quantity when it is missing or not as
described above, external_ or event_ when
missing or for an unknown customer or meter, timestamp for a
malformed one or one more than 5 minutes ahead, dimensions
for more than 16 keys or a value that is an object or an
array, and properties when it has a key (it was renamed
dimensions; null or {} is ignored). An unknown field is
refused the same way.
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": "validation_error", "message": "idempotency_key is required — give each event its own key and send the same key when you retry that event", "request_id": "req_dbjg652q3gpgqfoq5cd0", "param": "idempotency_key" }}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
the customer’s billing periods, the read of the meter’s
aggregation, or the write could not be answered. Retry with the
same key after Retry-After (1 second): the 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