Error Handling

The LocalStitch API uses standard HTTP status codes and returns errors in a consistent JSON format.

Error Response Format

All error responses follow this structure:

json
{
  "error": "error_code",
  "message": "Human readable description of the error"
}

Validation Errors

Validation errors include a details object with field-specific messages:

json
{
  "error": "validation_error",
  "message": "Invalid request parameters",
  "details": {
    "query": "Query string is required",
    "filters.rating_min": "Must be between 1 and 5"
  }
}

HTTP Status Codes

CodeStatusDescription
200OKRequest succeeded
201CreatedResource created successfully
202AcceptedAsync job created (e.g., enrichment)
400Bad RequestInvalid parameters or request body
401UnauthorizedInvalid or missing API key
402Payment RequiredInsufficient credits for operation
403ForbiddenAPI access not available on your plan
404Not FoundResource not found
409ConflictResource already exists (e.g., duplicate)
429Too Many RequestsRate limit exceeded
500Internal Server ErrorUnexpected server error
502Bad GatewaySearch service temporarily unavailable

Error Codes

The error field contains a machine-readable code:

Error CodeHTTPDescription
unauthorized401Invalid or missing API key
api_access_denied403API access requires a paid plan
validation_error400Invalid request parameters
insufficient_credits402Not enough credits for this operation
not_found404Requested resource doesn't exist
duplicate409Resource already exists
rate_limited429Too many requests, slow down
search_error502Search service error, retry later

Handling Errors in Code

Here's a recommended pattern for handling API errors:

javascript
400">"text-purple-400">async 400">"text-purple-400">function localstitchRequest(url, options) {
  400">"text-purple-400">const response = 400">"text-purple-400">await fetch(url, options);
  400">"text-purple-400">const data = 400">"text-purple-400">await response.json();
  
  400">"text-purple-400">if (!response.ok) {
    switch (response.status) {
      case 400:
        console.error(400">'Validation error:', data.details);
        break;
      case 401:
        console.error(400">'Check your API key');
        break;
      case 402:
        console.error(400">'Insufficient credits:', data.message);
        break;
      case 429:
        400">"text-purple-400">const retryAfter = response.headers.get(400">'Retry-After');
        console.error(400">`Rate limited. Retry after ${retryAfter}s`);
        break;
      400">"text-purple-400">default:
        console.error(400">'API error:', data.message);
    }
    throw new Error(data.message);
  }
  
  400">"text-purple-400">return data;
}

Best Practices

Always check status codes

Don't assume success — verify response.ok or status codes.

Log error details

Include the error code and message in your logs for debugging.

Handle specific errors

Differentiate between recoverable errors (429, 502) and permanent ones (401, 403).

Retry transient errors

Use exponential backoff for 429 and 5xx errors, but not for 4xx client errors.