Menu
Amazon API Gateway
Developer Guide

Use a JavaScript SDK Generated by API Gateway

Note

These instructions assume you have already completed the instructions in Generate SDKs for an API Using the API Gateway Console.

  1. Extract the contents of the API Gateway-generated .zip file you downloaded earlier.

  2. Enable cross-origin resource sharing (CORS) for all of the methods the SDK generated by API Gateway will call. For instructions, see Enable CORS for a Resource.

  3. In your web page, include references to the following scripts.

    Copy
    <script type="text/javascript" src="lib/axios/dist/axios.standalone.js"></script> <script type="text/javascript" src="lib/CryptoJS/rollups/hmac-sha256.js"></script> <script type="text/javascript" src="lib/CryptoJS/rollups/sha256.js"></script> <script type="text/javascript" src="lib/CryptoJS/components/hmac.js"></script> <script type="text/javascript" src="lib/CryptoJS/components/enc-base64.js"></script> <script type="text/javascript" src="lib/url-template/url-template.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/sigV4Client.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/apiGatewayClient.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/simpleHttpClient.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/utils.js"></script> <script type="text/javascript" src="apigClient.js"></script>
  4. In your code, initialize the SDK generated by API Gateway by using code similar to the following.

    Copy
    var apigClient = apigClientFactory.newClient();
  5. Call the API methods in API Gateway by using code similar to the following. Each call returns a promise with a success and failure callbacks.

    Copy
    var params = { // This is where any modeled request parameters should be added. // The key is the parameter name, as it is defined in the API in API Gateway. param0: '', param1: '' }; var body = { // This is where you define the body of the request, }; var additionalParams = { // If there are any unmodeled query parameters or headers that must be // sent with the request, add them here. headers: { param0: '', param1: '' }, queryParams: { param0: '', param1: '' } }; apigClient.methodName(params, body, additionalParams) .then(function(result){ // Add success callback code here. }).catch( function(result){ // Add error callback code here. });

    Here, the methodName is constructed from the method request's resource path and the HTTP. For example, for the GET / request, the methodName is rootGet. For the GET /a/b/op request, the methodName will be aBOpGet.

  6. To initialize the SDK generated by API Gateway with AWS credentials, use code similar to the following. If you use AWS credentials, all requests to the API will be signed. This means you must set the appropriate CORS Accept headers for each request.

    Copy
    var apigClient = apigClientFactory.newClient({ accessKey: 'ACCESS_KEY', secretKey: 'SECRET_KEY', });
  7. To use an API key with the SDK generated by API Gateway, you can pass the API key as a parameter to the Factory object by using code similar to the following. If you use an API key, it is specified as part of the x-api-key header and all requests to the API will be signed. This means you must set the appropriate CORS Accept headers for each request.

    Copy
    var apigClient = apigClientFactory.newClient({ apiKey: 'API_KEY' });