Menu
Amazon API Gateway
Developer Guide

Simple Calculator API in API Gateway

Our simple calculator API exposes three methods (GET, POST, GET) to invoke the (Calc). A graphical representation of this API is shown as follows:

These three methods show different ways to supply the input for the back-end Lambda function to perform the same operation:

  • The GET /?a=...&b=...&op=... method uses the query parameters to specify the input.

  • The POST /? method uses a JSON payload of {"a":"Number", "b":"Number", "op":"string"} to specify the input.

  • The GET /{a}/{b}/{op} method uses the path parameters to specify the input.

Before showing how to call these methods using an SDK generated by API Gateway for this API, let's recall briefly how to set them up. For detailed instructions, see Creating an API. If you're new to API Gateway, see Getting Started first.

Create Models for Input and Output

To specify strongly typed input in the SDK, we create an Input model for the API:

Similarly, to describe the response body data type, we create the following models in the API Gateway:

Copy
{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "c": {"type":"number"} }, "title": "Output" }

and

Copy
{ "$schema": "http://json-schema.org/draft-04/schema#", "type":"object", "properties":{ "input":{ "$ref":"https://apigateway.amazonaws.com/restapis/t7dve4zn36/models/Input" }, "output":{ "$ref":"https://apigateway.amazonaws.com/restapis/t7dve4zn36/models/Output" } }, "title":"Result" }

Set Up GET / Method Query Parameters

For the GET /?a=..&b=..&op=.. method, the query parameters are declared in Method Request:

Set Up Data Model for the Payload as Input to the Back End

For the POST / method, we create the Input model and add it to the method request to define the shape of input data.

With this model, your API customers can parse a successful output by reading properties of a Result object. Without this model, customers would be required to create dictionary object to represent the JSON output.

Set Up Data Model for the Result Output from the Back End

For all three methods, we create the Result model and add it to the method's Method Response to define the shape of output returned by the Lambda function.

With this model, your API customers can call the SDK to specify the input by instantiating an Input object. Without this model, your customers would be required to create dictionary object to represent the JSON input to the Lambda function.

In addition, you can also create and set up the API following the Swagger API definitions.