Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS WebSocket API Gateway Template Selection Expressions examples

AWS API Gateway service page says that a Template Selection Expression can be used to implement a way to transform the request body. However the documentation for these selection expressions is very light and I haven't been able to find any examples.

  1. Where can I find examples of what these expressions look like?
  2. Where can I find what variables and options are available in these expressions?
like image 832
McShaman Avatar asked Jun 21 '26 22:06

McShaman


1 Answers

To add content to the integration request, you'll need to use a Request Template. The Request Template is the piece that actually generates the new request body, while the Request Selection Template is used in the process of determining which Request Template to use.

  1. Turn off HTTP Proxy Integration for your route. (You can't modify the request otherwise.)
  2. Save your changes. (the Request Templates section won't appear until you do so.)
  3. Set your Template Selection Expression. This is used to look up a value from the incoming request object. (If you want to match all incoming requests, enter \$default. Note the slash. Full documentation here.)
  4. Set your Template Key. This is compared against the value selected by the Template Selection Expression. If it's a match, the template is used. (If you want to match all incoming requests, enter $default. Note the absence of a slash.)
  5. Click your Template Key to open the template editor. This is where you enter your template that will be sent to your integration endpoint as the body of the request. For example, if you want to forward the connection ID and the incoming query parameters to the integration endpoint, you can use the following:
{
    "myConnectionIdProperty": "$context.connectionId",
    "myQueryParams": $input.params()
}

Documentation for variables available to you in the template expression can be found here.

(Note that while $request is a valid variable in the Template Selection Expression, it is not a valid variable in the template itself. Use $input instead there.)

like image 79
Ben Grabow Avatar answered Jun 25 '26 19:06

Ben Grabow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!