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 Simple Calculator Lambda Function (Calc). A graphical representation of this API is shown as follows:


            Simple
                Calculator API for Generated SDK

These three methods show different ways to supply the input for the backend 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.

If not defined, API Gateway generates the corresponding SDK method name by combining the HTTP method and path parts. The root path part (/) is referred to as Api Root. For example, the default Java SDK method name for the API method of GET /?a=...&b=...&op=... is getABOp, the default SDK method name for POST / is postApiRoot, and the default SDK method name for GET /{a}/{b}/{op} is getABOp. Individual SDKs may customize the convention. Consult the documentation in the generated SDK source for SDK specific method names.

You can, and should, override the default SDK method names by specifying the operationName property on each API method. You can do so when creating the API method or updating the API method using the API Gateway REST API. In the API Swagger definition, you can set the operationId to achieve the same result.

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 in Amazon API Gateway. If you're new to API Gateway, see Build an API Gateway API with Lambda Integration 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 Backend

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 Backend

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.