API conventions
Errors
Every failed request returns an error object with the same shape. Make decisions in code based on the HTTP status and, above all, the machine-readable code field.
The error object
HTTP/1.1 409 Conflict
Content-Type: application/json
X-Request-Id: req_8c1f0b2a9d3e4f5a6b7c8d9e0f1a2b3c
{
"error": {
"code": "not_cancellable",
"message": "only pending invoices with nothing received can be cancelled",
"request_id": "req_8c1f0b2a9d3e4f5a6b7c8d9e0f1a2b3c"
}
}codeis stable; build your logic on it.messageis written for humans and may change. For validation errors it includes the path of the field (for exampleprice_usd: must be a USD amount like "49.90").request_idmatches theX-Request-Idheader. Include it when you contact support.
Error codes
| Code | HTTP | Meaning |
|---|---|---|
validation_error | 400 | A field is missing or invalid; the message names it. |
amount_too_small | 400 | The amount is below the invoice or withdrawal minimum. |
unauthorized | 401 | The key is missing, malformed or revoked. |
forbidden | 403 | The key lacks the scope, the IP is not allowlisted, or the action is closed to this key. |
live_key | 403 | The action needs a test key (for example simulation). |
test_key | 403 | The action needs a live key (for example endpoint management). |
account_not_active | 403 | The account is not approved yet, or it is suspended. |
address_not_trusted | 403 | The withdrawal address is not saved and trusted. |
address_not_saved | 403 | Withdrawals are restricted to saved addresses; this one is not saved or still within its 24-hour wait. |
withdrawals_locked | 403 | Withdrawals are locked for a while after a security change, or frozen for the account. |
limit_exceeded | 403 | The daily withdrawal limit would be exceeded. |
not_found | 404 | No such record in this account and mode. |
conflict | 409 | The record’s current state does not allow the action. |
not_cancellable | 409 | The invoice has received a payment and cannot be cancelled. |
insufficient_balance | 409 | The available balance does not cover the amount and fee. |
idempotency_conflict | 409 | The same idempotency key was used with a different request. |
request_in_progress | 409 | The first request with this idempotency key is still running; wait Retry-After seconds.retryable |
rate_limited | 429 | Rate limit exceeded; wait Retry-After seconds.retryable |
internal_error | 500 | Unexpected error on our side; retry with the same idempotency key.retryable |
service_unavailable | 503 | A short interruption; wait Retry-After seconds and retry with the same idempotency key.retryable |
rate_unavailable | 503 | No fresh exchange rate for a moment; retry shortly.retryable |
When to retry
429, 5xx and request_in_progress are temporary: retry with exponential backoff and jitter, using the same Idempotency-Key. If a Retry-After header is present, wait at least that long. For other 4xx errors, repeating the request does not change the outcome; fix the request first.
The official SDKs retry temporary errors on their own and raise a separate error class for each code:
import { ConflictError, HisarPayError, ValidationError } from '@hisarpay/node';
try {
await hisarpay.invoices.cancel(id);
} catch (err) {
if (err instanceof ConflictError && err.code === 'not_cancellable') {
// already (partly) paid: keep the order open
} else if (err instanceof ValidationError) {
console.error(err.message);
} else if (err instanceof HisarPayError) {
console.error(err.status, err.code, err.requestId);
} else {
throw err;
}
}use HisarPay\Exception\ConflictException;
use HisarPay\Exception\HisarPayException;
try {
$hisarpay->invoices->cancel($id);
} catch (ConflictException $e) {
if ($e->getErrorCode() === 'not_cancellable') {
// already (partly) paid: keep the order open
}
} catch (HisarPayException $e) {
error_log($e->getErrorCode() . ' ' . $e->getRequestId());
}from hisarpay import ConflictError, HisarPayError
try:
client.invoices.cancel(invoice_id)
except ConflictError as err:
if err.code == "not_cancellable":
pass # already (partly) paid: keep the order open
except HisarPayError as err:
print(err.status, err.code, err.request_id)