

# Simple calculator API in API Gateway
<a name="simple-calc-lambda-api"></a>

Our simple calculator API exposes three methods (GET, POST, GET) to invoke the [Simple calculator Lambda function](simple-calc-nodejs-lambda-function.md). A graphical representation of this API is shown as follows:

![\[Simple calculator API for generated SDK\]](http://docs.aws.amazon.com/apigateway/latest/developerguide/images/simple-calc-api-console-hierarchy-new-console.png)


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](https://docs.aws.amazon.com/apigateway/latest/api/API_Method.html#operationName) property on each API method. You can do so when [creating the API method](https://docs.aws.amazon.com/apigateway/latest/api/API_PutMethod.html) or [updating the API method](https://docs.aws.amazon.com/apigateway/latest/api/API_UpdateMethod.html) 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 [Develop REST APIs in API Gateway](rest-api-develop.md). If you're new to API Gateway, see [Choose an AWS Lambda integration tutorial](getting-started-with-lambda-integration.md) first.

## Create models for input and output
<a name="simple-calc-lambda-api-create-models-for-input-and-output"></a>

To specify strongly typed input in the SDK, we create an `Input` model for the API. To describe the response body data type, we create an `Output` model and a `Result` model.

**To create models for the input, output, and result**

1. In the main navigation pane, choose **Models**.

1. Choose **Create model**.

1. For **Name**, enter **input**.

1. For **Content type**, enter **application/json**. 

   If no matching content type is found, request validation is not performed. To use the same model regardless of the content type, enter **\$1default**.

1. For **Model schema**, enter the following model:

   ```
   {
       "$schema" : "$schema": "http://json-schema.org/draft-04/schema#",
       "type":"object",
       "properties":{
           "a":{"type":"number"},
           "b":{"type":"number"},
           "op":{"type":"string"}
       },
       "title":"Input"
   }
   ```

1. Choose **Create model**.

1. Repeat the following steps to create an `Output` model and a `Result` model.

   For the `Output` model, enter the following for the **Model schema**:

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

   For the `Result` model, enter the following for the **Model schema**. Replace the API ID `abc123` with your API ID.

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

## Set up GET / method query parameters
<a name="simple-calc-lambda-api-set-up-get-method-query-parameters"></a>

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

**To set up GET / URL query string parameters**

1. In the **Method request** section for the `GET` method on the root (`/`) resource, choose **Edit**.

1. Choose **URL query string parameters** and do the following:

   1. Choose **Add query string**.

   1. For **Name**, enter **a**.

   1. Keep **Required** and **Caching** turned off. 

   1. Keep **Caching** turned off.

   Repeat the same steps and create a query string named **b** and a query string named **op**.

1. Choose **Save**.

## Set up data model for the payload as input to the backend
<a name="simple-calc-lambda-api-set-up-post-method-body-data-type"></a>

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

**To set up the data model for the payload as input to the backend**

1. In the **Method request** section, for the `POST` method on the root (`/`) resource choose **Edit**.

1. Choose **Request body**.

1. Choose **Add model**.

1. For **Content type**, enter **application/json**.

1. For **Model**, select **Input**.

1. Choose **Save**.

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. 

## Set up data model for the result output from the backend
<a name="simple-calc-lambda-api-set-up-all-methods-result-data-type"></a>

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.

**To set up the data model for the result output from the backend**

1. Select the **/\$1a\$1/\$1b\$1/\$1op\$1** resource, and then choose the **GET** method.

1. On the **Method response** tab, under **Response 200**, choose **Edit**.

1. Under **Response body**, choose **Add model**.

1. For **Content type**, enter **application/json**.

1. For **Model**, select **Result**.

1. Choose **Save**.

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. 