# How do you add a request validator to api gateway?

**URL:** https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923
**Category:** Serverless Framework
**Tags:** aws, lambda, api-gateway
**Created:** [October 22, 2020, 8:09pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923 "2020-10-22T20:09:32Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![jack1902](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jack1902/32/4646_2.png) [@jack1902](https://forum.serverless.com/u/jack1902)
#### Post date: [October 22, 2020, 8:09pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/1 "2020-10-22T20:09:32Z")

</div>

How do you natively within serverless framework add request validation for an endpoint within API Gateway?

I have already achieved this with these two plugins:

- serverless-aws-documentation
- serverless-reqvalidator-plugin

and:

Function has:

```auto
my_func:
  request:
    parameters:
      headers:
        X-Timestamp: true
        X-Signature: true
  reqValidatorName: bodyAndParams

```

req\_validator:

```auto
Resources:
  bodyAndParams:
    Type: "AWS::ApiGateway::RequestValidator"
    Properties:
      Name: 'body-and-params'
      RestApiId:
        Ref: ApiGatewayRestApi
      ValidateRequestBody: true
      ValidateRequestParameters: true

```

But i want to drop these two plugins if possible since the majority of this is available in the framework already but i feel like i am missing the final piece to turn on the validation

---

<div class="post-metadata">

### Author: ![shadowdogg](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@shadowdogg](https://forum.serverless.com/u/shadowdogg)
#### Post date: [October 31, 2020, 8:38pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/2 "2020-10-31T20:38:12Z")

</div>

I too would like to know about this…

Googling shows this but I can’t make sense of it

If you set params as required via serverless syntax you then can just also activate the validator for parameters like so

```auto
Resources:
  ParameterRequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      Name: ParameterRequestValidator
      RestApiId:
        Ref: ApiGatewayRestApi
      ValidateRequestBody: false
      ValidateRequestParameters: true

  ApiGatewayMethodNameOfYourApiLookItUpInYourTemplate:
    Properties:
      RequestValidatorId:
        Ref: ParameterRequestValidator

```

---

<div class="post-metadata">

### Author: ![jack1902](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jack1902/32/4646_2.png) [@jack1902](https://forum.serverless.com/u/jack1902)
#### Post date: [November 2, 2020, 9:15am UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/3 "2020-11-02T09:15:12Z")

</div>

so to do this with the following plugins:

```auto
"serverless-aws-documentation": "github:deliveryhero/serverless-aws-documentation",
"serverless-reqvalidator-plugin": "^1.0.3",

```

on a function:

```auto
request:
  parameters:
    headers:
      X-Token: true
      X-Event: true
reqValidatorName: bodyAndParams

```

The resource `bodyAndParams`:

```auto
bodyAndParams:
    Type: "AWS::ApiGateway::RequestValidator"
    Properties:
      Name: 'body-and-params'
      RestApiId:
        Ref: ApiGatewayRestApi
      ValidateRequestBody: true
      ValidateRequestParameters: true

```

This does work, but i would ideally like to remove the plugins as its an additional dependancy in deploying my application. The header validation being the main thing i care about because it removes the requirement for the code to validate the headers

---

<div class="post-metadata">

### Author: ![shadowdogg](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@shadowdogg](https://forum.serverless.com/u/shadowdogg)
#### Post date: [November 2, 2020, 5:31pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/4 "2020-11-02T17:31:57Z")

</div>

agreed, I am in a similar scenario though I also get some warning each time about it

---

<div class="post-metadata">

### Author: ![garethmcc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/garethmcc/32/2276_2.png) [@garethmcc](https://forum.serverless.com/u/garethmcc)
#### Post date: [November 4, 2020, 10:36am UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/5 "2020-11-04T10:36:41Z")

</div>

There is no need for a plugin for request validation. It is built into the framework: [https://www.serverless.com/framework/docs/providers/aws/events/apigateway#request-parameters](https://www.serverless.com/framework/docs/providers/aws/events/apigateway#request-parameters)

---

<div class="post-metadata">

### Author: ![shadowdogg](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@shadowdogg](https://forum.serverless.com/u/shadowdogg)
#### Post date: [November 4, 2020, 11:52pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/6 "2020-11-04T23:52:02Z")

</div>

That makes them ‘required’ but doesn’t set the validation in the api gateway, so therefore it doesn’t actually validate

---

<div class="post-metadata">

### Author: ![bisoldi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bisoldi/32/4736_2.png) [@bisoldi](https://forum.serverless.com/u/bisoldi)
#### Post date: [November 19, 2020, 2:34pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/7 "2020-11-19T14:34:52Z")

</div>

I don’t understand why you need those two plugins. You’re adding raw CloudFormation, so you should be able to enable validation directly in it. I got it to work, but with an OpenAPI (`x-amazon-apigateway-request-validator`) script embedded into the Cloudformation, which was deployed via Serverless. That said I don’t know why your `AWS::ApiGateway::RequestValidator` resource wouldn’t work.

Anyways, have you figured this out?

I’m actually trying to somewhat the opposite, I want to disable validation. When you create the gateway in Serverless’ native way (i.e. below), adding a schema automatically turns on validation and I can’t find any way to NOT enable it.

```auto
events:
      - http:
          method: post
          path: '/serverless-full'
          integration: lambda
          request:
            schema:
              application/json: ${file(../models/UserModel.json)}
            template:
              application/json: '$util.escapeJavaScript($input.body).replaceAll("\\","")'
            passThrough: WHEN_NO_TEMPLATES
            contentHandling: CONVERT_TO_TEXT
          response:
            headers:
              Content-Type: "'application/json'"
            contentHandling: CONVERT_TO_TEXT

```

---

<div class="post-metadata">

### Author: ![shadowdogg](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@shadowdogg](https://forum.serverless.com/u/shadowdogg)
#### Post date: [November 21, 2020, 1:45pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/8 "2020-11-21T13:45:09Z")

</div>

For those of you failing to see this, like I also did.

This is what you need to do in plain english.

Turn

ApiGatewayMethodNameOfYourApiLookItUpInYourTemplate

to

APIGatewayMethod\<1\>\<2\>

[![API Gateway](https://i.stack.imgur.com/Hnrcr.png)](https://i.stack.imgur.com/Hnrcr.png)

In my case, it was APIGatewayDealsGet

The thing I was looking at was my handler name in serverless

```
   list:
    # Defines an HTTP API endpoint that calls the main function in list.js
    # - path: url path is /deals
    # - method: GET request
    handler: list.main
    events:
      - http:
          path: deals
          method: get
          cors: true
          authorizer: aws_iam
          request:
            parameters:
              querystrings:
                country: true
                type: true

```

Alternatively, if this does not work, check the s3 bucket, mine was called xxxxxxx-ap-serverlessdeploymentbuck-1epdp60eqveqr and go to serverless \> yyyyyyyyyyy \> aaaa \>  
timestamp \> compiled-cloudformation-template.json

And look for the name of your method in there, example mine was:

```
	"ApiGatewayMethodDealsGet": {
		"Type": "AWS::ApiGateway::Method",
		"Properties": {
			"HttpMethod": "GET",
			"RequestParameters": {
```

---

<div class="post-metadata">

### Author: ![pelcore](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/pelcore/32/4825_2.png) [@pelcore](https://forum.serverless.com/u/pelcore)
#### Post date: [December 3, 2020, 6:07pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/9 "2020-12-03T18:07:34Z")

</div>

I still can’t get this to work.  
Have both plugins installed

```
 plugins:
    - serverless-reqvalidator-plugin
    - serverless-aws-documentation

```

The events in my function properly declared

```
events: 
    - http: 
        path: permission/users
        method: get
        reqValidatorName: 'ParameterRequestValidator'
        request:
            parameters:
                querystrings:
                    permissions: true

```

and my resources

```
 Resources:
     ParameterRequestValidator:
       Type: "AWS::ApiGateway::RequestValidator"
       Properties:
         Name: 'ParameterRequestValidator'
         RestApiId:
         Ref: ApiGatewayRestApi
         ValidateRequestBody: false
         ValidateRequestParameters: true

```

First of all, serverless doesn’t recognize property ‘ **reqValidatorName** ’, I guess there’s something I missed.

After adding

```
ApiGatewayMethodPermissionUsersGet:
  Properties:
    RequestValidatorId:
      Ref: ParameterRequestValidator

```

It says **‘resources.Resources.ApiGatewayMethodPermissionUsersGet’: should have required property 'Type’**  
So I guess there’s a lot more that I missed here.  
I’m trying to do as you said, set the request validation configuration in aws.

---

<div class="post-metadata">

### Author: ![shadowdogg](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@shadowdogg](https://forum.serverless.com/u/shadowdogg)
#### Post date: [December 3, 2020, 7:19pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/10 "2020-12-03T19:19:36Z")

</div>

Check my reply… …

---

<div class="post-metadata">

### Author: ![pelcore](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/pelcore/32/4825_2.png) [@pelcore](https://forum.serverless.com/u/pelcore)
#### Post date: [December 10, 2020, 7:02pm UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/11 "2020-12-10T19:02:56Z")

</div>

Ok, there we go. Had to read it a few time to see it.  
Thanks for your time!

---

<div class="post-metadata">

### Author: ![dobeerman](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dobeerman/32/4710_2.png) [@dobeerman](https://forum.serverless.com/u/dobeerman)
#### Post date: [February 16, 2021, 8:26am UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/12 "2021-02-16T08:26:18Z")

</div>

Heave you solved this issue?

‘resources.Resources.ApiGatewayMethodPermissionUsersGet’: should have required property 'Type’

---

<div class="post-metadata">

### Author: ![mscannjr](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@mscannjr](https://forum.serverless.com/u/mscannjr)
#### Post date: [April 1, 2021, 1:33am UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/13 "2021-04-01T01:33:12Z")

</div>

This is not working for me. Pretty much 99+% of the problems I have with AWS are with the API Gateway.

Here is the error I am getting:

`Error: The CloudFormation template is invalid: Invalid template resource property 'APIGatewayUserprofilePost'`

And here is my code:

```auto
  Resources:
    ParameterRequestValidator:
      Type: AWS::ApiGateway::RequestValidator
      Properties:
        Name: ParameterRequestValidator
        RestApiId:
          Ref: ApiGatewayRestApi
        ValidateRequestBody: false
        ValidateRequestParameters: true

      APIGatewayUserprofilePost:
        Properties:
          RequestValidatorId:
            Ref: ParameterRequestValidator

```

I have a function named `userprofile` with a `POST` method. What am I doing wrong here?

---

<div class="post-metadata">

### Author: ![onkar](https://avatars.discourse-cdn.com/v4/letter/o/50afbb/32.png) [@onkar](https://forum.serverless.com/u/onkar)
#### Post date: [May 17, 2022, 6:59am UTC](https://forum.serverless.com/t/how-do-you-add-a-request-validator-to-api-gateway/12923/14 "2022-05-17T06:59:09Z")

</div>

try this “ApiGatewayMethodUserprofilePost”
