Is there a way to change the http status codes returned by Amazon API Gateway?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Amazon API Gateway is a fully managed service that allows developers to create, publish, maintain, monitor, and secure APIs at any scale. Among the many functionalities it provides, managing HTTP status codes is an essential aspect, as these codes indicate the result of client-server communication. Occasionally, there might be a requirement to change the default HTTP status codes returned by Amazon API Gateway. This article explores how to achieve this, delving deeper into the technical aspects and providing examples for clarity.
Understanding HTTP Status Codes in API Gateway
HTTP status codes are three-digit numbers issued by a server in response to a client's request made to the server. Each number has a specific meaning and is categorized into five classes:
- 1xx: Informational responses
- 2xx: Successful responses
- 3xx: Redirection messages
- 4xx: Client error responses
- 5xx: Server error responses
Amazon API Gateway, by default, provides a series of HTTP status codes in different scenarios, such as success, client error, and server error. However, these defaults might not always fit the specific needs of your application or integration, requiring customization.
Changing HTTP Status Codes
Modifying HTTP status codes in Amazon API Gateway can be achieved through various methods, including Integration Responses and Mapping Templates within an API Gateway configuration. Below, we discuss some techniques:
Method 1: Using Integration Responses
Integration Responses allow you to capture the backend response and transform it into a format acceptable by the API Gateway. You can also set custom HTTP status codes based on specific conditions. Here’s a basic outline of how to achieve this:
- Setup your Integration Response in API Gateway:
- Navigate to the API Gateway console.
- Select the desired API and Resource.
- Under Method Execution, choose Integration Response.
- Click Add integration response.
- Use Integration Response to define specific patterns that should map backend response to status codes.
- Define Regular Expressions or Conditions:
- Utilize conditions to map backend responses (e.g., Lambda functions, other endpoints) to specific HTTP status codes that you prefer.
- Example:
\.errorCode\.(\d{3})$can be used to dynamically extract a three-digit number from the backend response as the status code.
Example:
Imagine a scenario where handling different types of errors from a Lambda function backend is crucial. Here’s a simple demonstration:
In this case, the API Gateway can use a regex pattern on errorCode to return custom status code 418.
Method 2: Using Mapping Templates
Mapping Templates in Amazon API Gateway allow for powerful data transformations using Apache Velocity Template Language (VTL). You can use these to change or manipulate requests and responses, including HTTP status codes.
To configure a mapping template:
- Navigate to API Gateway console and select your API.
- Go to Method Response and choose the response status code you wish to map.
- Click on Add Response Model and choose application/json as the content-type.
- Add a Mapping Template and write your VTL template to map incoming data to your desired status code.
Example mapping for custom status code:
This template explicitly sets the response status to 400 instead of what was planned.
Key Considerations
When changing HTTP status codes, developers should consider:
- Consistency: Ensure status codes are consistent with the API's documentation and client expectations.
- Meaningful Responses: Provide meaningful error messages and body content associated with custom status codes to facilitate troubleshooting.
- Testing: Test API responses thoroughly for all possible scenarios to confirm custom status codes behave as expected.
Summary Table
| Feature/Aspect | Description or Usage |
| HTTP Status Codes | Codes indicating the result of HTTP client-server communication. |
| Integration Responses | Mapping backend responses to custom status codes using regex or exact match rules. |
| Mapping Templates | Use VTL for data transformation to adjust the status code in the response. |
| Example Regex Pattern | \.errorCode\.(\d{3})$ used to extract custom status code from a response. |
| Example Status Code Change | Setting $context.responseOverride.status = 400 to return HTTP 400. |
| Key Considerations | Consistency, meaningful responses, and thorough testing. |
By understanding these detailed methods and examples, developers can efficiently manipulate HTTP status codes in Amazon API Gateway, leading to more precise API behavior and better client experience. Always remember to document any changes to ensure they are well understood by the entire team.

