Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Set up route responses for WebSocket APIs in API Gateway

Focus mode
Set up route responses for WebSocket APIs in API Gateway - Amazon 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

The following create-route-response command creates a route response for the $default route. You can identify the API ID and route ID by using the get-apis and get-routes commands.

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

The output will look like the following:

{ "RouteResponseId": "abcdef", "RouteResponseKey": "$default" }
PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.