Skip to content
GitHubXDiscord

GatewayResponse

The GatewayResponse resource allows you to manage custom responses from the AWS ApiGateway, enabling you to tailor error messages and responses for your API clients. For more details, visit the AWS ApiGateway GatewayResponses documentation.

Create a basic GatewayResponse that customizes the response for unauthorized requests.

import AWS from "alchemy/aws/control";
const unauthorizedResponse = await AWS.ApiGateway.GatewayResponse("UnauthorizedResponse", {
RestApiId: "myApiId123",
ResponseType: "UNAUTHORIZED",
ResponseTemplates: {
"application/json": "{\"message\": \"Access denied.\"}"
},
ResponseParameters: {
"gatewayresponse.header.Access-Control-Allow-Origin": "'*'"
}
});

Configure a GatewayResponse to handle internal server errors with a customized message and specific headers.

const internalServerErrorResponse = await AWS.ApiGateway.GatewayResponse("InternalServerErrorResponse", {
RestApiId: "myApiId123",
ResponseType: "INTERNAL_SERVER_ERROR",
StatusCode: "500",
ResponseTemplates: {
"application/json": "{\"error\": \"Internal server error occurred.\"}"
},
ResponseParameters: {
"gatewayresponse.header.Access-Control-Allow-Origin": "'*'",
"gatewayresponse.header.Content-Type": "'application/json'"
}
});

You can also customize the response for not found errors in a flexible way.

const notFoundResponse = await AWS.ApiGateway.GatewayResponse("NotFoundResponse", {
RestApiId: "myApiId123",
ResponseType: "RESOURCE_NOT_FOUND",
StatusCode: "404",
ResponseTemplates: {
"application/json": "{\"error\": \"Resource not found.\"}"
}
});

If you want to ensure that your resource is created or updated without failing when it already exists, you can use the adopt option.

const adoptResponse = await AWS.ApiGateway.GatewayResponse("AdoptResponse", {
RestApiId: "myApiId123",
ResponseType: "DEFAULT_4XX",
adopt: true,
ResponseTemplates: {
"application/json": "{\"error\": \"An error occurred.\"}"
}
});