Set up route responses for WebSocket APIs in API Gateway - Amazon API Gateway

Set up route responses for WebSocket APIs in API Gateway

WebSocket routes can be configured for two-way or one-way communication. API Gateway will not pass the backend response through to the route response, unless you set up a route response.

Note

You can only define the $default route response for WebSocket APIs. You can use an integration response to manipulate the response from a backend service. For more information, see Overview of integration responses.

You can configure route responses and response selection expressions by using the API Gateway console or the AWS CLI or an AWS SDK.

For more information about route response selection expressions, see Route response selection expressions.

Set up a route response using the API Gateway console

After you have created a WebSocket API and attached a proxy Lambda function to the default route, you can set up route response using the API Gateway console:

  1. Sign in to the API Gateway console, choose a WebSocket API with a proxy Lambda function integration on the $default route.

  2. Under Routes, choose the $default route.

  3. Choose Enable two-way communication.

  4. Choose Deploy API.

  5. Deploy your API to a stage.

Use the following wscat command to connect to your API. For more information about wscat, see Use wscat to connect to a WebSocket API and send messages to it.

wscat -c wss://api-id.execute-api.us-east-2.amazonaws.com/test

Press the enter button to call the default route. The body of your Lambda function should return.

Set up a route response using the AWS CLI

To set up a route response for a WebSocket API using the AWS CLI, call the create-route-response command as shown in the following example. You can identify the API ID and route ID by calling get-apis and get-routes.

aws apigatewayv2 create-route-response \ --api-id aabbccddee \ --route-id 1122334 \ --route-response-key '$default'

Example output:

{ "RouteResponseId": "abcdef", "RouteResponseKey": "$default" }