What error handling covers and the problem it solves
Any program that calls an API has to decide what to do when the answer is not the one it wanted. With SimilarIQ the decision has a money dimension, because a search costs a credit. Retrying the wrong error wastes time. Failing to retry the right one throws away a result you could have had for free.
The API keeps this manageable by using a short list of HTTP status codes with distinct meanings: 200, 400, 401, 402, 429 and 502. Each one points to a different party who needs to act. Your code, your credentials, your balance, your pacing, or nobody at all.
This page is the map. It says what each code means, whether a credit was spent, and what a script or an AI agent should do next. It applies to the REST API directly and, through them, to the SDKs and the CLI that sit on top of it.
How it works: the six status codes
The codes fall into three groups. One is success. Three are problems on your side that a retry will not fix: a malformed request, a credential problem and an empty balance. Two are temporary conditions where trying again is the correct response: a rate limit and a failed search.
Only one outcome costs anything. A 200 on the search endpoint is a completed search and spends one credit, $0.25. The four client-side codes mean no search was performed. A 502 means a search was attempted and failed, and the credit is returned automatically.
Some details are not published and you should not code against guesses: the structure of the error body, any machine-readable error codes inside it, whether a 429 carries a header telling you how long to wait, and the numeric rate limits themselves. Branch on the HTTP status, log the body as text, and check /docs for anything more specific.
- 200: success; the response holds the ranked list and
creditsRemaining - 400: bad request; correct the input before sending again
- 401: bad or missing key; fix authentication
- 402: out of credits; add funds or wait for auto-refill
- 429: rate limited; pause, then retry
- 502: search failed; credit refunded, safe to retry
400 and 401: fix the request, do not retry
A 400 says the request itself is wrong. The search call is POST https://similariq.ai/api/v1/search with a JSON body such as {"domain":"stripe.com","limit":100}. The input is one company website, and limit can be 10, 25, 50 or 100. A missing domain, a body that is not valid JSON or a limit outside those four values are the obvious things to check. The published facts do not list every validation rule, so read the response body for the specific complaint.
A 401 says the key is bad or missing. Confirm that the header is X-API-Key: sk-live-... or Authorization: Bearer sk-live-..., that the secret was copied whole, and that the key has not been revoked. Keys are shown once at creation, so a truncated copy cannot be recovered; create a new key in the app.
Neither code improves with repetition. Sending the same bytes again produces the same answer. In a batch job, a 400 should mark that one item as failed and move on, while a 401 should stop the whole job, because every later call will fail the same way.
402 and 429: funds and pacing
A 402 means there are no credits to pay for the search. Nothing ran and nothing was charged. The remedy is outside your code: someone deposits funds, with a $10 minimum, or auto-refill tops the balance up if it is enabled and the monthly cap has room. A program should stop, report clearly and leave the queue intact for later.
You can usually avoid meeting a 402 at all. Every successful search response includes creditsRemaining, and the balance check is a free call. A job that knows it has 40 credits and 60 domains can decide what to do before it starts.
A 429 means the rate limiter stepped in. SimilarIQ describes it as a standard per-key and per-IP limiter that smooths bursts. The response to a 429 is to slow down: wait, then retry, with a longer wait each time. Since a search takes roughly 30 to 60 seconds, moderate concurrency is usually enough. For dedicated throughput, email info@similariq.ai.
502: the failed search that costs nothing
A 502 is the one error that follows real work. A search is a fresh research pass, and a 502 means that pass could not be completed. SimilarIQ refunds the credit automatically, and the documentation describes the call as safe to retry. You pay for results, not attempts. The automatic refunds page goes into the billing side.
A reasonable policy is two or three retries with a pause between them, then give up on that domain and record it. Do not loop forever. A refund makes a retry free, but a domain that fails repeatedly is telling you something, and an endless loop will meet the rate limiter.
One related case has no status code: your own client timing out. If your HTTP library abandons the request after ten seconds, you never learn whether the search succeeded. Set the timeout well above a minute. If a timeout does happen, check GET /api/history, which is free, before you search the same domain again.
A worked example: a batch with every kind of error
A script processes 50 domains with 45 credits in the balance. The numbers are hypothetical. It starts by calling the balance endpoint, sees 45, and logs a warning that the last five domains may not be funded. It proceeds anyway.
Along the way one domain returns 400 because the input row was an email address, not a website. The script records it and continues; no credit is spent. Two searches return 502. Both succeed on the first retry; the failed attempts are refunded. One burst triggers a 429; the script waits and carries on. After 45 successful searches the balance is zero and the next call returns 402. The script stops and writes out the four domains still queued.
The bill is 45 credits, or $11.25. The 400, the two 502s, the 429 and the 402 cost nothing. Someone deposits funds, the script is restarted with the four remaining domains and the corrected fifth, and the job finishes for another $1.25.
Errors in the app, the CLI, the SDKs and the MCP server
The status codes are an API concept. In the web app the same conditions appear as messages on screen, and their exact wording is not published. The underlying rules are the same: a search with no balance cannot run, and a failed search is refunded.
The CLI and the SDKs call the REST API, so the same six outcomes sit underneath them. How each SDK surfaces an error, as an exception type or a returned object, is documented on the SDK and CLI reference pages and is not restated here. Whatever the form, branch on the status.
On the MCP server, an assistant sees tool results, not raw HTTP. The published facts do not describe how each failure is worded inside a tool result. Give the agent plain rules: call get_search_balance first; if find_similar_companies fails, retry at most twice; if it reports no credits, stop and tell the user. A connection that fails to authenticate needs a fresh OAuth sign-in or a valid key header.
Pitfalls to avoid
The first pitfall is one retry rule for everything. A blanket retry on any non-200 response hammers the API with requests that cannot succeed and hides the real cause. Retry 429 and 502 only.
The second is treating an empty or short result list as an error. A 200 is a success even if the list is not what you hoped for, and it is charged. Judge quality with matchScore and confidence, not with status codes.
The third is swallowing 402. A job that logs out of credits and carries on will log it thousands of times and achieve nothing. Stop on the first one. The fourth is forgetting bulk: POST /api/bulk/upload returns a job identifier and the work happens asynchronously, so problems show up when you poll GET /api/bulk/jobs/:id, not in the upload response. The job's status values are not published here; read them from the response.
Pairing error handling with other features, and its limits
Good error handling leans on three other features. The free balance check prevents most 402s. Spend controls, meaning optional auto-refill and a monthly cap, decide whether an empty balance refills itself and how far. Free reads let you recover from uncertainty by checking history without paying.
There are limits to what error handling can do. It cannot raise your rate limit, fund your account or make a failing domain succeed. It does not come with an uptime commitment either; an SLA is available by quote through the form on /pricing. What it can do is make sure that every credit you spend corresponds to a list you received, and that every failure is recorded with the reason.
Frequently asked questions
What does a 502 from the SimilarIQ API mean?
The search failed. The credit for it is refunded automatically and the request is safe to retry. Retry a small number of times with a pause, then record the domain and move on.
What does a 402 mean?
You are out of credits. No search ran and nothing was charged. Add funds, with a $10 minimum deposit, or let auto-refill top up the balance if it is enabled and your monthly cap allows it.
How should I handle a 429 rate limit?
Wait and retry, lengthening the wait each time. The limiter works per key and per IP to smooth bursts. The numeric limits are not published; for dedicated throughput, email info@similariq.ai.
Which errors should I retry?
Retry 429 after a pause and 502 because the credit was refunded. Do not retry 400, 401 or 402 without changing something first: the request, the key or the balance.
Why am I getting a 401?
The key is missing, mistyped or revoked. Send it as X-API-Key: sk-live-... or as a bearer token. If the key was lost or truncated, create a new one in the app, since keys are shown once.
Am I charged for requests that return an error?
No. A 400, 401, 402 or 429 means no search was performed. A 502 means a search failed and the credit was returned. Only a successful search costs a credit.
What timeout should my HTTP client use?
A search takes roughly 30 to 60 seconds because it is a fresh research pass. Set your client timeout well above a minute so you do not abandon calls that are about to return.