Error Handling

Understanding API errors and how to handle them

Error Handling

The Servicebay API uses standard HTTP status codes and returns detailed error messages.

Error Response Format

{
  "success": false,
  "error": "Description of what went wrong"
}

HTTP Status Codes

Status CodeMeaningDescription
200OKRequest succeeded
201CreatedResource created successfully
400Bad RequestInvalid request parameters or body
401UnauthorizedMissing or invalid API key
403ForbiddenValid key but no access to resource
404Not FoundResource doesn't exist
429Too Many RequestsRate limit exceeded
500Internal Server ErrorSomething went wrong on our end

Common Errors

401 Unauthorized

{
  "success": false,
  "error": "Missing API key. Include X-API-Key header in your request."
}

Solution: Include a valid API key in the X-API-Key header.

403 Forbidden

{
  "success": false,
  "error": "You do not have access to this organisation"
}

Solution: Your API key is scoped to a different organisation. Use the correct key for the organisation you're trying to access.

404 Not Found

{
  "success": false,
  "error": "Customer not found"
}

Solution: Check that the resource ID is correct.

429 Rate Limited

{
  "success": false,
  "error": "Rate limit exceeded. Please wait before making more requests."
}

Solution: Wait until the rate limit window resets (see X-RateLimit-Reset header).

Handling Errors in Code

async function makeApiRequest(url, options) {
  const response = await fetch(url, {
    ...options,
    headers: {
      "X-API-Key": API_KEY,
      "Content-Type": "application/json",
      ...options.headers,
    },
  });

  const data = await response.json();

  if (!data.success) {
    switch (response.status) {
      case 401:
        throw new Error("Invalid API key");
      case 403:
        throw new Error("Access denied to this resource");
      case 404:
        throw new Error("Resource not found");
      case 429:
        const resetTime = response.headers.get("X-RateLimit-Reset");
        throw new Error(`Rate limited. Retry after ${resetTime}`);
      default:
        throw new Error(data.error || "Unknown error");
    }
  }

  return data;
}

On this page