Understand error response formats and handle API errors in your application
When a request fails, the API returns a JSON response with success: false and an error object containing a machine-readable code, a human-readable message, and the HTTP status code.
When using fetch directly, check the success field in the response body:
Copy
const response = await fetch( "https://<project-ref>.supabase.co/functions/v1/api", { method: "POST", headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", }, body: JSON.stringify({ resource: "genies", action: "get", id: "abc-123", }), });const result = await response.json();if (!result.success) { const { code, message, status } = result.error; switch (code) { case "UNAUTHORIZED": case "INVALID_TOKEN": // Refresh the auth session and retry break; case "NOT_FOUND": case "AGENT_NOT_FOUND": // Resource does not exist or is not accessible break; case "VALIDATION_ERROR": // Fix the request body and retry break; case "RATE_LIMIT_EXCEEDED": // Wait and retry with exponential backoff break; default: console.error(`API error [${code}]: ${message}`); }}
Use exponential backoff for 429 and 500 errors. These are the only error codes worth retrying automatically. Authentication errors (401) require a token refresh, and validation errors (400, 403, 404) indicate a problem with the request itself.A recommended approach:
Start with a 1-second delay after the first failure.
Double the delay after each subsequent failure (1s, 2s, 4s, 8s…).
Add random jitter (0-500ms) to avoid thundering herd problems.
Give up after 3-4 retries and surface the error to the user.
The most common cause is an expired access token. Supabase tokens typically expire after 1 hour. Use supabase.auth.refreshSession() to get a new token.
Each resource supports a specific set of actions. Sending an unsupported action (e.g., action: "archive" to a resource that does not support it) returns INVALID_ACTION. Check the resource’s documentation in the API reference for supported actions.
403 — Insufficient permissions
The user’s role does not allow the requested operation. For example, a consumer user cannot create genies. See Introduction for the role permission matrix.
404 — Resource not found
The record either does not exist or belongs to another user. Non-admin users can only access their own resources. Verify the id value and confirm the user has access.
429 — Rate limit exceeded
Back off and retry with exponential delay. A simple implementation: