Menu
Amazon API Gateway
Developer Guide

Create an API Using AWS CLI Commands

To create an API using the AWS CLI, you work with the create-rest-api, create-resource or get-resources, put-method, put-method-response, put-integration, and put-integration-response commands. The following procedures show how to work with these AWS CLI commands to create the simple PetStore API.

To create a simple PetStore API using AWS CLI

  1. Call the create-rest-api command to create a RestApi.

    Copy
    aws apigateway create-rest-api --name 'Simple PetStore (AWS CLI)' --region us-west-2

    The following is the output of this command:

    Copy
    { "name": "Simple PetStore (AWS CLI)", "id": "vaz7da96z6", "createdDate": 1494572809 }
  2. Call the get-resources command to retrieve the root resource identifier of the RestApi.

    Copy
    aws apigateway get-resources --rest-api-id vaz7da96z6 --region us-west-2

    The following is the output of this command:

    Copy
    { "items": [ { "path": "/", "id": "begaltmsm8" } ] }
  3. Call the put-method command to create a Method request of GET / with open access.

    Copy
    aws apigateway put-method --rest-api-id vaz7da96z6 --resource-id begaltmsm8 --http-method GET --authorization-type "NONE" --region us-west-2

    The following is the output of this command:

    Copy
    { "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE" }
  4. Call the put-method-response command to create a MethodResponse of the GET / method.

    Copy
    aws apigateway put-method-response --rest-api-id vaz7da96z6 --resource-id begaltmsm8 --http-method GET --status-code 200 --region us-west-2

    The following is the output of this command:

    Copy
    { "statusCode": "200" }
  5. Call the put-integration command to create an Integration with a specified HTTP endpoint for the GET / method.

    Copy
    aws apigateway put-integration --rest-api-id vaz7da96z6 --resource-id begaltmsm8 --http-method GET --type HTTP --integration-http-method GET --uri 'http://petstore-demo-endpoint.execute-api.com/petstore/pets' --region us-west-2

    The following is the output of this command:

    Copy
    { "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "cacheNamespace": "begaltmsm8" }
  6. Call the put-integration-response command to create an IntegrationResponse of the GET / method integrated with an HTTP backend.

    Copy
    aws apigateway put-integration-response --rest-api-id vaz7da96z6 --resource-id begaltmsm8 --http-method GET --status-code 200 --selection-pattern "" --region us-west-2

    The following is the output of this command:

    Copy
    { "selectionPattern": "", "statusCode": "200" }

Deploy the API to a stage stage, for example, by calling create-deployment:

Copy
aws apigateway create-deployment --rest-api-id vaz7da96z6 --stage-name test --stage-description 'Test stage' --description 'First deployment'

You can test this API by typing the https://vaz7da96z6.execute-api.us-west-2.amazonaws.com/test URL in a browser, and substituting vaz7da96z6 with the identifier of your API. The expected output should be as follows:

Copy
[ { "id": 1, "type": "dog", "price": 249.99 }, { "id": 2, "type": "cat", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 } ]