Grant credit to a customer
const url = 'http://localhost:8080/v1/credits/grant';const options = { method: 'POST', headers: { 'Idempotency-Key': '5f0c8a2e-7b1d-4c3e-9a6f-2d8b4e1c7a90', Authorization: 'Bearer <token>', 'Content-Type': 'application/json' }, body: '{"customer_id":"hc_cus_dakhnk2q3gpgqfoq5c0g","currency":"USD","amount_cents":5000,"description":"Welcome credit","expires_at":"2027-01-01T00:00:00Z","grant_kind":"promotional"}'};
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/credits/grant \ --header 'Authorization: Bearer <token>' \ --header 'Content-Type: application/json' \ --header 'Idempotency-Key: 5f0c8a2e-7b1d-4c3e-9a6f-2d8b4e1c7a90' \ --data '{ "customer_id": "hc_cus_dakhnk2q3gpgqfoq5c0g", "currency": "USD", "amount_cents": 5000, "description": "Welcome credit", "expires_at": "2027-01-01T00:00:00Z", "grant_kind": "promotional" }'Adds a credit grant in currency. The credit pays only the
customer’s invoices in that currency; it is never converted. A
grant in a currency the customer is not billed in is accepted and
shows as its own balance. Invoices draw the credit when they are
billed. The audit log records the grant.
Authorizations
Section titled “Authorizations”Parameters
Section titled “Parameters”Header Parameters
Section titled “Header Parameters”Makes the call safe to retry. Send a random UUID, and the same value
on every retry. Keys are kept per account and mode, and every API key
and dashboard user of the account shares them, so make a key
unpredictable. A key over 255 characters is refused with 422
validation_, param: Idempotency-Key. A request sent with a
key runs to its end even if the client disconnects: retry with the
same key for its answer.
For 24 hours, a repeat from the same caller (the same API key or the
same dashboard user) with the same key, method, path, query string
and body gets the first response again, byte for byte, with an
Idempotent-Replayed: true header, instead of running again. A
first answer of 401, 403, 409, 422 or 503, or a 400 invalid JSON body, is not kept, so a retry with the same key runs again. A
request to a path or method no route serves holds no key.
A repeat sent while the first request is still running gets 409
with code conflict_ at once: retry it in a moment with
the same key. The same key from another caller,
or with a different method, path, query string or body, is a 422
with code idempotency_ at once, even while the first request
runs. The query string must match byte for byte. After
24 hours the key is free, and a request with it runs as new. The
other error cases are in
Idempotency. The usage routes do
not take it: each usage event carries its own idempotency_.
The requests whose answer carries a secret shown only once
(creating or rotating an API key, creating a webhook endpoint or
rotating its signing secret, rotating a customer’s cost dashboard
token, creating a card setup session) do not take it either:
Hardcount does not keep that answer.
Example
5f0c8a2e-7b1d-4c3e-9a6f-2d8b4e1c7a90Request Bodyrequired
Section titled “Request Bodyrequired”object
The customer to grant credit to (hc_).
ISO 4217 code of the credit. Required. Case-insensitive; stored uppercase. Zero-decimal currencies (such as JPY) are not supported yet.
The credit to add, in the currency’s minor unit (cents for USD). From 1 to 100000000 (1,000,000.00).
What the credit is for, shown on the ledger entry. Required: a grant without one, or with a blank one, is refused with 422. At most 500 bytes after surrounding spaces are trimmed (fewer characters when it has non-ASCII letters).
Optional expiry; must be in the future. Omitted = never expires. The credit can be drawn up to and including this instant; a period invoice draws it when its period ends at or before it. The dashboard sends the local midnight after the picked day.
Optional cost-basis class. promotional marks free or
marketing credit, which invoices draw before any other
credit. Omitted means unclassified credit. commit is
refused here: commit credit comes only from paying a
top-up.
Example
{ "customer_id": "hc_cus_dakhnk2q3gpgqfoq5c0g", "currency": "USD", "amount_cents": 5000, "description": "Welcome credit", "expires_at": "2027-01-01T00:00:00Z", "grant_kind": "promotional"}Responses
Section titled “Responses”Credits granted
One entry in a customer’s credit ledger: credit added (grant, or a
positive adjustment), drawn by an invoice (usage), removed (a
negative adjustment) or written off (expiry). Returned by
POST / and POST /, and listed
by GET /.
object
The entry’s ID (hc_).
The account the entry belongs to.
The customer whose credit the entry changes (hc_).
grant: credit added, by you withPOST /or by Hardcount (a top-up when it is paid, a credit note, or a void or credit note that gave drawn credit back).v1/ credits/ grant usage: credit an invoice drew.expiry: the unused rest of a grant, written off once itsexpires_passed.at adjustment: credit added or removed withPOST /, or commit credit taken back when its invoice was refunded by a credit note.v1/ credits/ adjust
The entry’s currency, uppercase. Credit pays only invoices in its own currency.
Positive adds credit, negative removes it, in the currency’s minor unit (cents for USD).
The customer’s credit balance in this entry’s currency after it, in the currency’s minor unit (cents for USD).
What the entry is for: the description you gave a grant or
adjustment, “Applied to invoice” and the invoice number on a
usage entry, or “Expired grant” and the grant’s ID on an
expiry entry.
The invoice the entry comes from: the invoice a usage entry
was drawn by, the voided or credited invoice that gave credit
back, or the top-up that funded a grant. Absent when no invoice
caused the entry, and on the adjustment that takes commit
credit back after its invoice was refunded: that invoice is in
metadata..
On a grant, its expiry. The credit can be drawn up to and including this instant; a period invoice draws it when its period ends at or before it. The dashboard sends the local midnight after the picked day. Absent when the credit does not expire.
Facts Hardcount records on the entry, such as which grants a
draw or a deduction took its credit from (drained_).
Absent when empty, and on the answer of
POST /, which does not return it; the ledger
list does.
object
When the entry was written.
For entries that add credit, how much of it is gone, in the currency’s minor unit: drawn by invoices, expired, removed by a negative adjustment, or, for commit credit, taken back when its invoice was refunded by a credit note. Absent when nothing is.
On a grant, what the credit cost the customer. commit:
prepaid credit granted when a top-up was paid; that invoice is
source_. promotional:
free credit you marked so; invoices draw it before any other
credit. Absent for other credit.
For a commit grant, the top-up whose payment made the grant.
Absent on other entries.
Recorded on credit that a credit note gave back, but not sent by
any route in this version: the ledger list does not return it,
and the grant and adjust routes never write it. Such an entry
names the invoice in invoice_.
Recorded on credit that a voided invoice gave back, but not sent
by any route in this version: the ledger list does not return
it, and the grant and adjust routes never write it. Such an
entry names the invoice in invoice_.
Example
{ "id": "hc_ccl_dav2242q3gpgqfoq5c50", "tenant_id": "hc_ten_dajqj02q3gpgqfoq5c00", "customer_id": "hc_cus_dakhnk2q3gpgqfoq5c0g", "entry_type": "grant", "currency": "USD", "amount_cents": 5000, "balance_after": 5000, "description": "Welcome credit", "expires_at": "2027-01-01T00:00:00Z", "created_at": "2026-10-01T09:00:00Z", "grant_kind": "promotional"}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 credential is valid but may not call this route. A usage-only
key (hc_) can only send usage, read a customer’s live
credit balance and call GET /; use a secret key. The message names the permission the key
lacks. A browser request that would change data (POST, PUT, PATCH
or DELETE) from a page on another site gets this 403 too, with the
message cross-site request blocked.
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": "forbidden", "message": "insufficient permissions: this key type does not have customer:read access", "request_id": "req_dbjg652q3gpgqfoq5cd0" }}No customer with this customer_ in this account and mode.
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": "not_found", "message": "customer not found", "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" }}A field failed validation (param names it): customer_ or
description missing; a missing or unsupported currency;
amount_ not above 0 or above 100000000; a description
over 500 bytes; an expires_ not in the future; a
grant_ other than promotional. Or the body has an unknown
field (param names it; the body must use only the fields
listed here).
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": "must be greater than 0", "request_id": "req_dbjg652q3gpgqfoq5cd0", "param": "amount_cents" }}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.
Stuck, or found a bug? Ask in Discussions · Open an issue