Handling API Errors
This article covers how to identify and resolve common API errors you might encounter while using our service. You need this guide when you receive unexpected responses from the API, such as error codes or failed requests.
Before you start
- Ensure you have access to the API documentation.
- Have your API credentials ready.
- Familiarize yourself with basic HTTP status codes.
Steps to Handle API Errors
-
Check the HTTP Status Code
Review the HTTP status code returned by the API. Common codes include 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), and 500 (Internal Server Error). -
Review the Error Message
Look at the error message provided in the API response. This message often gives specific details about what went wrong. -
Validate Your Request
Ensure your API request is correctly formatted. Check the endpoint URL, request headers, and body content for any errors. -
Authenticate Properly
Verify that your API credentials (such as tokens or keys) are correct and have not expired. Re-authenticate if necessary. -
Check API Documentation
Refer to the API documentation to ensure you are using the correct endpoints and request methods. -
Retry the Request
If the error is temporary (e.g., a 500 Internal Server Error), wait a few moments and try the request again. -
Log the Error
Record the error details in your logs for further analysis and to help with future troubleshooting. -
Contact Support
If you cannot resolve the error, contact support with the error details, including the status code, error message, and request information.
Example Error Handling
Here is an example of handling a 401 Unauthorized error:
{
"error": "Unauthorized",
"message": "Invalid API key"
}
- Action: Check your API key for typos or expiration. Update the key in your request header and retry.
If Something Goes Wrong
-
Invalid Credentials: If you receive a 401 Unauthorized error, double-check your API key or token. Ensure it is correctly entered and has not expired.
-
Endpoint Not Found: A 404 Not Found error indicates the endpoint URL might be incorrect. Verify the URL against the API documentation.
-
Server Errors: For 500 Internal Server Errors, the issue is typically on the server side. Retry the request after some time. If the problem persists, contact support.
Sources: azanium/gxp/gas-web, azanium/gxp/gas-api.