Creating Private API in Amazon API Gateway

   Go back to the Task List

  « 6: Create Cloud9 Environment    8: Clean up »

7: Create API Client

In this task, you will write code to call API using the Python based code.

  1. The API has to be used along with VPC endpoint for the private access. The API URL looks like the following https://9v41ie7ihg.execute-api.eu-west-1.amazonaws.com/dev where 9v41ie7ihg is the API Id. The VPC endpoint id looks like vpce-0db4550239140a852. The values will be different in your AWS Account. You need to join these two to make the API URL which will look like the following.

    API URL - https://[API-ID]-[VPC-ENDPOINT-ID].execute-api.eu-west-1.amazonaws.com/dev

    API URL - https://9v41ie7ihg-vpce-0db4550239140a852.execute-api.eu-west-1.amazonaws.com/dev

  2. In the AWS Cloud9 console, click on the New File option under the File menu to add a new file. Copy-paste the following code in the new file and save it as dojoclient.py. The {API-URL} should be replaced by API URL you have in your environment as described in the first step.

    Cloud9

    import requests
    
    r = requests.get("{API-URL}")
    
    print(r.json())
    

    `

  3. The code is simple. You are making http call to API with Get method.

  4. In the console window, execute python dojoclient.py command to run the dojoclient.py code. The code execution finishes in no time and will print the response. You can see the response code value 200 for success.

    Cloud9

  5. It was an example to see how to call a private API. The workshop finishes here. Follow the next step to clean-up the resources so that you don’t incur any cost post the workshop.