Error Handling
Understand API error responses and how to handle them.
Error Format
All error responses follow the same format:
jsonJSON
1{2 "error": "Human-readable error message"3}
HTTP Status Codes
| Code | Description | Common causes |
|---|---|---|
400 | Bad Request | Missing required fields, invalid parameters |
401 | Unauthorized | Missing or invalid API key |
403 | Forbidden | Insufficient permissions for the operation |
404 | Not Found | Resource doesn't exist or doesn't belong to your company |
500 | Internal Error | Server-side error |
Handling Errors with the SDK
The SDK throws typed errors that include the HTTP status code and error message:
typescriptTypeScript
1import Corebill from '@corebill/sdk';23const corebill = new Corebill({ apiKey: 'sk_live_...' });45try {6 const customer = await corebill.customers.retrieve('cus_nonexistent');7} catch (error) {8 if (error instanceof Error) {9 console.error(error.message); // "Customer not found"10 }11}
Common Errors
Missing company_id
jsonJSON
1// 4002{ "error": "company_id query parameter is required" }
Invalid API key
jsonJSON
1// 4012{ "error": "Invalid API key" }
Insufficient permissions
jsonJSON
1// 4032{ "error": "Insufficient permissions. Write access required." }
Resource not found
jsonJSON
1// 4042{ "error": "Customer not found" }
Missing required fields
jsonJSON
1// 4002{ "error": "name is required" }
Invalid status value
jsonJSON
1// 4002{ "error": "Invalid status. Must be one of: draft, sent, viewed, paid, overdue, cancelled" }
Business rule violations
jsonJSON
1// 4002{ "error": "Cannot delete a paid invoice" }
Best Practices
- Always check the HTTP status code before parsing the response body
- Use the
errormessage for debugging, not for programmatic logic - Implement retry logic with exponential backoff for
500errors - Log error responses for troubleshooting