Errors
When a request fails, Hardcount answers with an HTTP status and a JSON body
that names the problem with a code. This page shows the body, what each
status means, and every code the API sends, with what to do about it.
The error body
Section titled “The error body”Every error has this shape, including an unknown path and a method the path does not take:
{ "error": { "type": "invalid_request_error", "code": "validation_error", "message": "external_id is required", "request_id": "req_<id>", "param": "external_id" }}| Field | Always there | What it is |
|---|---|---|
type |
Yes | The kind of error; see Statuses |
code |
Yes | A stable name for the problem. Branch on it in your code. |
message |
Yes | A sentence for people. Do not parse it: the words can change. |
request_ |
Yes | The request’s ID; see Find a request in your logs |
param |
No | The field or query parameter that failed, when one did |
A few answers add fields beside error; see
Answers in another shape.
Statuses
Section titled “Statuses”| Status | type |
Meaning | Retry? |
|---|---|---|---|
400 |
invalid_ |
The request cannot be read: the body is not JSON, or a field has the wrong JSON type | No. Fix the request. |
401 |
authentication_ |
No key, or a wrong, revoked or expired one | No. Fix the key. |
403 |
authentication_ |
The key or session may not do this | No |
404 |
invalid_ |
The resource or the path does not exist | No |
405 |
invalid_ |
The path does not take this method. The Allow header lists the ones it takes. |
No |
409 |
invalid_ |
It conflicts with what exists: a duplicate, or the resource is in the wrong state for this action | Sometimes; see the code |
413 |
invalid_ |
The body is too large | No. Send less. |
422 |
invalid_ |
A field or a business rule failed | No. Fix the request. |
429 |
rate_ |
Too many requests | Yes, after Retry-After seconds |
500 |
api_ |
Something went wrong on the server | See Retry after an error |
502 |
api_ |
Stripe or the email server refused or did not answer | See the code |
503 |
api_ |
A part of Hardcount was not available for a moment | Yes |
504 |
api_ |
The request ran past its time limit before any answer was written | See Retry after an error |
Every code
Section titled “Every code”The status sets the default code. A more specific code replaces it where the API has one.
400 Bad Request
Section titled “400 Bad Request”code |
When | What to do |
|---|---|---|
invalid_ |
The body is not one JSON value, or a field has the wrong JSON type (invalid JSON body). A usage batch has no events or more than 1,000. |
Fix the request |
401 Unauthorized
Section titled “401 Unauthorized”code |
When | What to do |
|---|---|---|
unauthorized |
No key or session, or a wrong, revoked or expired one. Also a wrong sign-in, and a bad public cost-dashboard link. | Fix the key. The messages: When a key is refused. |
invalid_ |
A payment-update link that is unknown, expired or already used, or whose invoice was removed | Ask for a new link |
403 Forbidden
Section titled “403 Forbidden”code |
When | What to do |
|---|---|---|
forbidden |
The key’s type may not call this route. A route that takes only a dashboard session. A browser request from another site. The other mode’s Stripe connection. | Use the right key; see Authentication |
404 Not Found
Section titled “404 Not Found”code |
When | What to do |
|---|---|---|
not_ |
The resource, or one the body names, does not exist in this account and mode. A bad hosted invoice link gets it too. | Check the ID and the key’s mode |
route_ |
No route has this path | Check the URL |
invoice_ |
The invoice behind a payment-update link was removed while the request ran | Ask for a new link |
customer_ |
The customer behind a payment-update link no longer exists | Ask for a new link |
405 Method Not Allowed
Section titled “405 Method Not Allowed”code |
When | What to do |
|---|---|---|
method_ |
The path does not take this method | Use a method from the Allow header |
409 Conflict
Section titled “409 Conflict”Most of these say the resource is in the wrong state now. Read it again, then decide.
Duplicates and state
code |
When | What to do |
|---|---|---|
already_ |
A value that must be unique is taken, such as a customer’s external_. param names it. |
Use another value, or read the one that exists |
invalid_ |
The resource is in the wrong state for this action | Read it, wait or change it, then try again |
concurrent_ |
Another change to the same records stopped the request: Postgres broke a deadlock or ended the request’s wait for a lock, or, for a cancel, the subscription changed twice while the cancel priced it. Only the routes listed below answer it. | Send the same request again. With an Idempotency-Key, the same key works. Some steps stay done (below). |
already_ |
POST / after the first account exists |
Do not retry |
member_ |
GET / or POST /, which the invited person’s browser calls: the invited email already belongs to another account on this server, and an email can belong to only one. |
Do not retry. The person who sent the invitation invites a different address. |
These routes answer concurrent_:
- Subscriptions: create, cancel, and schedule a plan change.
- Customers: update, and write the billing profile.
- Invoices: create, record payment, and collect when it settles the invoice without a card charge.
- Credit notes: issue, and the issue step of create with
auto_. The create step itself answersissue 500.
Postgres ends a wait for a lock only when a lock_ is set, for
example on the database user Hardcount runs as. On any other route or step,
a deadlock or an ended lock wait answers 500 internal_, which an
Idempotency-Key stores (Idempotency).
A cancel prices its final invoice before it writes anything. When the
subscription changes in that time, for example because its billing period
closes, the cancel writes nothing and prices once more on its own. If the
subscription changes again, the cancel answers concurrent_ and the
subscription is not canceled.
After concurrent_, a step that the request saved on its own before
Postgres stopped it stays done. The routes not listed here save nothing
first:
- Cancel, and scheduling a plan change: the invoice for a billing period that had already ended. It may already be charged. The request sent again does not bill that period twice.
- Collect: the customer’s credit that the request applied to the invoice before the settle that Postgres stopped. Sent again, Collect finds nothing left due, so it applies no credit and marks the invoice paid.
- Create with
auto_: the note it made, which is voided. The request sent again makes and issues a new note.issue
Idempotency (how idempotency keys work)
code |
When | What to do |
|---|---|---|
conflict_ |
A request with this Idempotency-Key is still running or did not finish. Hardcount answers at once, without waiting for it. |
Wait a moment, then retry with the same key. Wait longer before each later try. |
conflict_ |
A request with this key started more than 15 minutes ago and never recorded an answer | Check whether the change happened; retry with a new key only if not |
Pricing
code |
When | What to do |
|---|---|---|
price_ |
The plan already prices this meter with the same dimension_ |
Change that price instead |
price_ |
Another price on the plan and meter would take the same events with the same number of labels | Make one price more specific |
meter_ |
A subscriber of the plan has another subscription with this meter, or has the plan twice | Price the meter on a new plan |
meter_ |
You tried to change the aggregation of a meter that has events | Make a new meter |
Subscriptions
code |
When | What to do |
|---|---|---|
unbilled_ |
A billing period has ended and could not be invoiced, so the change waits | Fix the billing failure (POST /), then try again |
already_ |
Creating a subscription, or scheduling a plan change, when another active subscription of the customer is on the plan or scheduled to move to it. The message names its code. | Cancel that subscription first, or choose another plan |
billing_ |
retry-billing on a subscription whose billing is not failing |
Nothing to do |
Invoices and payments
code |
When | What to do |
|---|---|---|
payment_ |
A card charge for the invoice is running, or its result is not known yet. Every charge takes a second or so. A void, write-off, offline payment, credit note, collection or email would run at the same time as that charge. | Wait a few minutes, then retry. After an unclear answer from Stripe, Hardcount can take about an hour to settle the charge. |
payment_ |
A payment for this invoice is already in progress, on the hosted page for example | Wait, then read the invoice |
not_ |
The invoice cannot be paid now, for example it is voided. Collect answers it for a draft that is not a top-up, a voided invoice and a written-off one; the hosted page answers it for a paid one too. | Do not retry. For a written-off debt, record an offline payment or issue a new invoice. |
invoice_ |
Collect on a paid invoice | Nothing to do |
no_ |
Collect when the customer has no card set up | Send the customer a setup link |
no_ |
Collect on an invoice with nothing due that it did not mark paid: credit covered it while the request ran, or a payment is processing | Send Collect again. If it answers the same, read the invoice. |
amount_ |
The amount is below Stripe’s smallest card charge | Void it, record an offline payment, or bill it later |
card_ |
The bank refused the saved card for good | Ask the customer for a new card |
invoice_ |
You asked to resend a payment link for an invoice that is not unpaid or written off | Do not retry |
invoice_ |
You asked to email a draft or voided invoice | Finalize it first, or do not send it |
no_ |
You asked to resend a payment link to a customer with no email | Add an email to the customer |
invoice_ |
The invoice changed while the request ran | Read it again, then retry |
invoice_ |
You tried to add a line to a draft invoice that billing made for a subscription. That invoice keeps the lines and tax that billing gave it. | Bill the charge on its own one-off invoice |
credit_, invoice_ |
A credit note no longer fits the invoice, which changed while the request ran | Read the invoice, then retry with the right amount |
customer_ |
A one-off invoice’s currency is not the customer’s billing currency | Use the customer’s currency |
tax_ |
Stripe Tax’s calculation for the invoice is too old to finalize | Retry the tax, then finalize |
stripe_ |
Retrying a credit note’s refund, refunding a top-up’s unused credit (commit relief), or marking a refund as returned another way, while a refund made in Stripe for this invoice is not recorded yet, or is still in progress at Stripe | If it is not recorded: record it, attach it or dismiss it on the invoice. If it is in progress: wait for Stripe to settle it. Then send the request again. |
dispute_ |
Refunding a top-up’s unused credit (commit relief) while a dispute the customer’s bank won on its payment has no credit note yet | Write it with POST /, then refund what is left |
credit_ |
The customer’s credit changed while it was being returned | Retry |
Setup on this server
code |
When | What to do |
|---|---|---|
email_ |
An action that sends email, on a server with no email set up (SMTP_). Nothing was sent. |
The person who runs your server sets up email: Email setup |
stripe_ |
An action that needs Stripe, in a mode with no Stripe keys | Connect Stripe under Settings |
return_ |
A setup link (send-setup-email, or setup-session with no return_) on a server with no CUSTOMER_, so Stripe would have no page to send the customer back to. Nothing was created at Stripe. |
The person who runs your server sets CUSTOMER_: Settings |
online_ |
The hosted invoice or payment-update page cannot take a card: the seller has not connected Stripe for this mode, or the server has no address for Stripe to send the customer back to (HOSTED_, PAYMENT_) |
The customer pays another way. A payment-update link is not used up, so it works once the seller fixes the setup |
Rare: a record that must be unique already exists
These guard against a record being written twice, mostly by Hardcount’s own
billing run. A request rarely gets one. Do not retry; read the invoice or
the credit ledger, and report it if the state looks wrong:
invoice_, invoice_,
invoice_, invoice_,
credit_,
credit_,
credit_, credit_ and
credit_.
413 Payload Too Large
Section titled “413 Payload Too Large”code |
When | What to do |
|---|---|---|
batch_ |
A POST / body is over 1 MB, or a POST / body is over 32 MB. Nothing was saved. |
Send smaller batches |
payload_ |
The body is over the 1 MB limit, with or without an Idempotency-Key. Nothing was changed, and the answer is not kept for the key. A Stripe webhook body over 64 KB gets it too. |
Send a smaller body. For a Stripe webhook, nothing: Stripe’s events are smaller. |
422 Unprocessable Entity
Section titled “422 Unprocessable Entity”code |
When | What to do |
|---|---|---|
validation_ |
A field failed a rule, such as a missing external_, a bad limit, a list sort or dir the list does not take, a field the operation does not take, or a query parameter it does not take. A list filter that takes a fixed set of values got another, such as status on the invoice list. param names it. |
Fix the field |
batch_ |
At least one event of a usage batch failed. Nothing was stored. The body lists each failure in errors. |
Fix the events named, then send the whole batch again |
idempotency_ |
An Idempotency-Key was first used by another caller (another API key or dashboard user) or with a different method, path, query string or body |
Use a new key for a new request |
customer_ |
An invoice preview or a usage summary for a customer with no active subscription | Send subscription_, or a period with from and to |
subscription_ |
An invoice preview for a subscription with no current billing period | Send a period |
missing_ |
You asked to email a payment-method link to a customer with no email | Add an email |
note_ |
That email’s note is over 2,000 bytes | Shorten it |
invalid_ |
You asked to rotate the public link of a draft invoice | Finalize the invoice first |
invalid_ |
The dashboard’s check of a password-reset link (GET /) found it unknown, expired or used. Setting the password, or accepting an invite, with a bad token answers validation_ with param: token instead. |
Ask for a new link |
429 Too Many Requests
Section titled “429 Too Many Requests”Each sends a Retry-After header with the seconds to wait.
code |
When | What to do |
|---|---|---|
rate_ |
Over a rate limit | Wait Retry-After seconds, then retry |
login_ |
Too many failed sign-ins for an email | Wait, then sign in again |
cooldown_ |
Someone asked to email this customer a payment-method link less than a minute ago, even if that request then failed. Counted only on a server with Redis. | Wait a minute, then send it with a new Idempotency-Key: this answer is stored like any other |
500 Internal Server Error
Section titled “500 Internal Server Error”code |
When | What to do |
|---|---|---|
internal_ |
An unexpected fault. The details are in the server’s log, not in the answer. | See Retry after an error. Look up the request_ in the logs. |
502 Bad Gateway
Section titled “502 Bad Gateway”code |
When | What to do |
|---|---|---|
provider_ |
Stripe refused the request, such as a declined card. The message says why. | Read the message; do not retry blindly |
payment_ |
Stripe never answered. The charge or refund may have gone through. | Check the invoice before you retry |
stripe_ |
Stripe refused the account’s key, or no key is connected for this mode. Nothing was charged. If Stripe refused the key on the confirm, the invoice shows unknown until the first scheduled run after the key works again. |
Fix the Stripe key under Settings |
stripe_ |
The hosted page could not start a Stripe Checkout session | Try again later |
smtp_ |
The email server refused the test email. The message says what it answered. | Fix the email settings |
email_ |
An email could not be queued | Retry |
503 Service Unavailable
Section titled “503 Service Unavailable”code |
When | What to do |
|---|---|---|
authentication_ |
Hardcount could not check the key or session | Retry the same request |
idempotency_ |
Hardcount could not record the Idempotency-Key, so it ran nothing |
Retry with the same key after Retry-After (1 second) |
ingest_ |
The usage store did not answer, or could not write the events. | Send the same events, with the same keys, after Retry-After (1 second): each is recorded once |
collect_ |
POST / was stopped before its first request to Stripe, by a database fault, unreadable Stripe credentials or too many recent Stripe failures. No card was charged. Credit it applied stays applied, and is not applied twice. |
Retry the same request after Retry-After (1 second), with the same Idempotency-Key if you sent one. If that answers 409 payment_, wait as that code says. |
currency_ |
POST / could not read the customer’s currency. No invoice was made. |
Retry the same request, with the same Idempotency-Key if you sent one |
settings_ |
POST / could not read the account’s time zone, which sets the due date of a draft created before invoices carried their own zone. The invoice was not finalized. |
Retry the same request after Retry-After (1 second), with the same Idempotency-Key if you sent one |
unbilled_ |
Record-loss, or a credit note with stripe_ on a top-up, could not read the customer’s usage not invoiced yet, which the note counts as credit already used. Nothing was written. |
Retry the same request after Retry-After (1 second), with the same Idempotency-Key if you sent one |
504 Gateway Timeout
Section titled “504 Gateway Timeout”code |
When | What to do |
|---|---|---|
timeout |
The request ran past its 30-second time limit before any answer was written. A request that runs too long can also end with 500 internal_. |
See Retry after an error |
Retry after an error
Section titled “Retry after an error”4xx: do not send the same request again, except after409codes that say so and after429. Fix the request first. If it carried anIdempotency-Key, send the fixed request with a new key: the answer was stored, and the old key with a changed body answers422idempotency_. Aerror 400invalid JSON bodyis not stored: send the fixed body with the same key.429rate_andlimited login_: waitthrottled Retry-Afterseconds, then send the same request, with the same key. Nothing ran and nothing was stored.cooldown_is stored: see its row above.active 503authentication_andunavailable idempotency_: send the same request with the same key. Nothing ran.unavailable 503ingest_: send the same events with the same keys. Each is recorded once, even if the store wrote them before its answer was lost.unavailable 503collect_: no card was charged. Send the same request with the same key. If that answersunavailable 409payment_, wait as that code says.in_ flight 503currency_,unavailable settings_andunavailable unbilled_: the change was not made, and ausage_ unavailable 503is never stored. Send the same request with the same key.500,502and504on a usage route: send the same events with the same keys. A key is never counted twice.500,502and504on another write sent with anIdempotency-Key: the answer is stored, so a retry with the same key gets it again. That includes the500internal_of a crash inside Hardcount. Read the resource to see whether the change happened, and retry with a new key only if it did not. See Idempotency.error
Find a request in your logs
Section titled “Find a request in your logs”Every answer has a Hardcount-Request-Id header, such as
req_. An error has the same value in request_.
Hardcount makes the ID itself; a request cannot set it.
The server writes one JSON log line per request, with the ID in the
request_ field. Every other line the request writes, such as the
details of a 500, carries it too. To find them on a
quick start install:
cd deploy/composedocker compose logs hardcount-api | grep 'req_d3l2a8c5d6q3f0bnsp0g'Under make dev, search the terminal output of the API.
Request bodies
Section titled “Request bodies”A request body may hold only the fields its operation lists in the API reference.
- Any other field is refused with
422andparamnaming it. That includes a misspelled field, a field the operation does not take, a field the server sets (such asid,tenant_orid created_), and a name in another case (such asat Emailforemail). Nothing is changed. - A usage batch names each such event in
errorsinstead. - An operation with no request body, such as
POST /, takes no body,v1/ subscriptions/ {id}/ cancel {}ornull. - A body that is not one JSON value is refused with
400. - The Stripe and Postmark webhooks and the LiteLLM callback are the exceptions: they take their sender’s body as it comes.
Bodies are limited to 1 MB, LiteLLM callback bodies to 32 MB, and Stripe
webhook bodies to 64 KB. A larger body gets 413: batch_ from
the usage batch and the LiteLLM callback, payload_ from the
other routes. The Postmark webhook is the exception below.
Query parameters
Section titled “Query parameters”A query string may hold only the parameters its operation lists in the API reference.
- Any other is refused with
422validation_,error paramnaming it and the messageunknown query parameter "<name>". That includes a misspelled filter, such asstausforstatus. When several are unknown,paramnames the first in sorted order. - A parameter sent with an empty value, such as
status=, is still one the operation takes. - A query string that does not parse (a bad
%escape, or;between parameters) is refused whole, with noparam. - The Stripe and Postmark webhooks and the cost dashboard’s public link take any query string.
Answers in another shape
Section titled “Answers in another shape”- A rejected usage batch (
422batch_) has, besiderejected error,errors(one line per failing event),ingested: 0andtotal. POST /answersv1/ billing/ run 206when some subscriptions could not be billed, withinvoices_andgenerated errors.- A replayed
Idempotency-Keyreturns the first answer’s body byte for byte. - A CSV export that fails partway ends with a row that starts
EXPORT_, or stops with no such row when the connection is cut. Treat a file as complete only if your client got the whole response without an error.INCOMPLETE GET /answershealth/ ready 503with{"status", "checks"}.- The Postmark webhook accepts a body that is too large or not JSON
with
200and{"status": "skipped", "reason": "<reason>"}. - Plain text, not JSON, comes from two places:
- The HTTP server itself, before Hardcount sees the request. A request it
cannot parse gets
400; headers over about 12 KB get431; an unknown transfer encoding gets501; an unsupported HTTP version gets505. These answers have noHardcount-Request-Id. /, the Prometheus route, answers a plain-textmetrics 500if it cannot read the metrics.
- The HTTP server itself, before Hardcount sees the request. A request it
cannot parse gets
Stuck, or found a bug? Ask in Discussions · Open an issue