Menu
Amazon API Gateway
Developer Guide

Swagger Definitions of a Sample API with Basic Request Validation

The following Swagger definition defines a sample API with request validation enabled. The API is a subset of the PetStore API. It exposes a POST method to add a pet to the pets collection and a GET method to query pets by a specified type.

There are two request validators declared in the x-amazon-apigateway-request-validators map at the API level. The params-only validator is enabled on the API and inherited by the GET method. This validator allows API Gateway to verify that the required query parameter (q1) is included and not blank in the incoming request. The all validator is enabled on the POST method. This validator verifies that the required header parameter (h1) is set and not blank. It also verifies that the payload format adheres to the specified RequestBodyModel schema. This model requires that the input JSON object contains the name, type, and price properties. The name property can be any string, type must be one of the specified enumeration fields (["dog", "cat", "fish"]), and price must range between 25 and 500. The id parameter is not required.

For more information about the behavior of this API, see Enable Basic Request Validation for an API in API Gateway.

Copy
{ "swagger": "2.0", "info": { "title": "ReqValidators Sample", "version": "1.0.0" }, "schemes": [ "https" ], "basePath": "/v1", "produces": [ "application/json" ], "x-amazon-apigateway-request-validators" : { "all" : { "validateRequestBody" : true, "validateRequestParameters" : true }, "params-only" : { "validateRequestBody" : false, "validateRequestParameters" : true } }, "x-amazon-apigateway-request-validator" : "params-only", "paths": { "/validation": { "post": { "x-amazon-apigateway-request-validator" : "all", "parameters": [ { "in": "header", "name": "h1", "required": true }, { "in": "body", "name": "RequestBodyModel", "required": true, "schema": { "$ref": "#/definitions/RequestBodyModel" } } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "POST", "requestParameters": { "integration.request.header.custom_h1": "method.request.header.h1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } }, "get": { "parameters": [ { "name": "q1", "in": "query", "required": true } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "GET", "requestParameters": { "integration.request.querystring.type": "method.request.querystring.q1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } } } }, "definitions": { "RequestBodyModel": { "type": "object", "properties": { "id": { "type": "integer" }, "type": { "type": "string", "enum": ["dog", "cat", "fish"] }, "name": { "type": "string" }, "price": { "type": "number", "minimum": 25, "maximum": 500 } }, "required": ["type", "name", "price"] }, "Error": { "type": "object", "properties": { } } } }