Errors
What a failed SwapWallet API request returns: the error object, HTTP status codes, common error codes, and how to handle each in your code.
When a request fails, the HTTP status says what kind of failure it is and the body says exactly what went wrong, in a form your code can branch on and a person can read.
The error object
{
"status": "ERROR",
"error": {
"codeString": "VALIDATION_FAILED",
"message": "Request validation failed",
"localizedMessage": "اطلاعات درخواست معتبر نیست"
}
}| Field | What it is |
|---|---|
codeString | A stable, machine-readable code. Branch on this. |
message | The explanation in English, for logs and developers. |
localizedMessage | The same explanation in the account’s language (Persian unless the account chose another), ready to show to the person using your app. |
Messages can be reworded at any time; codes don’t change. Never match on message text.
HTTP status codes
| Status | Meaning | Typical codeString |
|---|---|---|
200 | It worked. | |
400 | The request is wrong: malformed JSON, a missing or invalid field, a bad parameter, or a rule the action breaks (not enough balance, amount under the minimum). | INVALID_REQUEST, VALIDATION_FAILED, INVALID_REQUEST_PARAMETER |
401 | No API key, a wrong one, one that was replaced, or the wrong kind of key for this API. | API_KEY_AUTHENTICATION_FAILED, UN_AUTHORIZATION |
403 | The service is closed for now, such as the shop. | |
404 | The thing you asked for doesn’t exist, the path is wrong, or a market is paused. | ENDPOINT_NOT_FOUND, TRANSACTION_NOT_FOUND, MARKET_IS_NOT_ACTIVE |
405 | Right path, wrong HTTP method. | METHOD_NOT_ALLOWED |
429 | Too many requests in a short time. | TOO_MANY_REQUESTS |
500 | Something failed on SwapWallet’s side. | INTERNAL_SERVER_ERROR |
Endpoint pages list the specific codes worth handling for that endpoint, such as an expired swap quote or an unsupported network. Errors raised while checking the key come back in English in both message fields, and a request with no Authorization header at all may get a bare 401 without this body.
Withdrawal errors suggest an amount
When a withdrawal is refused because of its amount, the error also carries suggestionAmount: an amount that would go through, such as the most you can withdraw right now. Offer it to the user instead of a dead end. These codes carry it:
INSUFFICIENT_USER_BALANCE: the balance doesn’t cover the amount and its fee.LIMIT_DAILY_WITHDRAW_EXCEEDED,LIMIT_MONTHLY_WITHDRAW_EXCEEDED: the account’s withdrawal limit for the day or month.USER_BALANCE_LOCKED: part of the balance can’t be withdrawn yet. See crypto withdrawals.USER_DO_NOT_CONSIDER_NETWORK_FEE: the amount leaves no room for the withdrawal fee.
{
"status": "ERROR",
"error": {
"codeString": "LIMIT_DAILY_WITHDRAW_EXCEEDED",
"message": "…",
"localizedMessage": "…",
"suggestionAmount": { "number": "182.4", "unit": "USDT" }
}
}Handling errors in code
- 4xx: fix the request; sending it again unchanged gets the same answer. The one exception is
429: wait, then retry with a growing delay. - 5xx: safe to retry for reads. For anything that moves money, first check whether the first attempt went through (see Retrying safely).
- Log
codeStringandmessage; showlocalizedMessageto people.
const res = await fetch(url, options);
const body = await res.json();
if (body.status === 'OK') return body.result;
const { codeString, message, localizedMessage } = body.error;
switch (res.status) {
case 401: throw new Error('API key missing, wrong or replaced');
case 429: await sleep(60_000); return retry(); // slow down
case 500: case 502: case 503: return retryLater(); // reads only
default: throw new ApiError(codeString, message, localizedMessage);
}Seeing an error you can’t explain? Send support the codeString, the time of the request and the endpoint. Never include your API key.