1. Switch on `code`, not the message
The API's errors are JSON of the form { "error": { "code", "message", "docs_url" } }. The HTTP status and the code string are stable contract; the message is human-readable and may change.
switch (error.code) {
case 'RATE_LIMITED':
case 'BUSY':
case 'TIMEOUT':
// Wait, then retry
break;
case 'INVALID_INPUT':
case 'NOT_FOUND':
case 'CHECK_CODE_ALREADY_USED':
case 'SHARE_CODE_UNUSABLE':
case 'CHECK_CODE_UNUSABLE':
// Correct the input, with the applicant or driver; don't retry
break;
case 'QUOTA_EXCEEDED':
case 'PAYMENT_REQUIRED':
// Billing: send a person to the link
break;
case 'UNAUTHENTICATED':
case 'INVALID_KEY':
case 'PERMISSION_DENIED':
case 'CHECK_NOT_FOUND':
case 'RESOURCE_NOT_FOUND':
case 'ENDPOINT_NOT_FOUND':
// Fix the request or key; don't retry
break;
case 'INTERNAL':
case 'GOVUK_UNEXPECTED':
// Retry once, then contact us
break;
}Each error also carries docs_url, its code's section of Errors, and an INVALID_INPUT names the request field in field. A driving licence check's TIMEOUT or GOVUK_UNEXPECTED is safe to retry only when check_code_used is false: a check code works once.
2. Honour `Retry-After`
When the server says 503 BUSY or 429 RATE_LIMITED it includes a Retry-After header (seconds). Sleep that long and retry. Don't hammer.
async function checkWithBackoff(body: object, key: string, maxAttempts = 3) {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const res = await fetch('https://api.checksharecode.co.uk/v1/check/right-to-work', {
method: 'POST',
headers: { 'content-type': 'application/json', authorization: `Bearer ${key}` },
body: JSON.stringify(body),
});
if (res.ok) return res.json();
if (res.status === 503 || res.status === 429) {
const retry = Number(res.headers.get('retry-after') ?? '5');
await new Promise((r) => setTimeout(r, retry * 1000));
continue;
}
throw new Error(`${res.status}: ${(await res.json()).error.code}`);
}
throw new Error('Exhausted retries');
}3. Log `X-Request-Id`
Every check's response carries an X-Request-Id header, and its errors repeat it as request_id. Log it alongside your own trace ID so support can cross-reference our audit log if a request misbehaves.
4. When to surface to the end user
Only INVALID_INPUT, NOT_FOUND, CHECK_CODE_ALREADY_USED, SHARE_CODE_UNUSABLE and CHECK_CODE_UNUSABLE mean something the applicant, the driver or your form needs to correct. All other codes are your team's to handle.