Use API Keys in API Gateway without Usage Plans Enabled
Since the API Gateway usage plans feature was launched on August 11, 2016, usage plans will be enabled for you in a region where you start using API Gateway the first time. In this case, you must use a usage plan to associate an API key with an API stage and follow the instructions in Use API Gateway Usage Plans.
If, on the other hand, you created an API Gateway API before then and have not enabled usage plans in the region since then, you can follow the instructions given in this section to associate an API key with an API stage to identify API callers, to restrict API access to those users with matching API keys, and to curtail abusive uses with different API keys.
To use an API key without enabling usage plans, you need to perform the following tasks:
Create an API key.
Enabled it on specific API methods.
Deploy the API to a stage.
Associate the key with the API stage.
Distribute the key to your customers and ask them to supply it in calls to the enabled API methods.
Use an API Key when Usage Plans Are Not Enabled
The following instructions apply only if usage plans are not enabled for your account in the chosen region. To associate an API key with an API stage in a usage plan, follow the instructions in Use API Gateway Usage Plans.
To use the API Gateway console to enable an API key without usage plans, follow these instructions:
Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway.
Enable API key on an method:
Choose a method under a resource of your choosing.
Choose the Method Request box
If API Key Required is set to false, choose the pencil icon next to it. Choose true from the drop-down list and then choose the check-mark icon to save the setting.
Note that the steps above configure the API Gateway to enforce using API key on the method. Otherwise, the API key created following the instructions below will not be used for any of such calls.
Deploy or redeploy the API for the requirement to take effect.
Create an API key:
In the API Gateway main navigation pane, choose API Keys.
Choose Create API Key from the Actions drop-down menu.
For Name, type a name for the API key entry.
Choose either the Auto Generate option for API Gateway to generate the key or the Custom option for you specify the key value manually. A key value is an alphanumeric string of a size between 30 and 128 characters long.
Optionally, type a description for the API key in the Description text box.
Choose Save. Make a note of the key displayed in API key. Callers of the enabled method must specify the key value in the
Choose Show next to API key to view the newly created API key. Your customers must provide this key as the
x-api-keyheader value when they call this method.
The generated API key is enabled by default, allowing the API caller to access the API, provided that the supplied API key matches the one configured. To prevent the apps with the specified API key from accessing the API, choose Edit, deselect the Enabled option, and then choose Save.
Associate an API key with an API stage
Under API Stage Association, choose the name of the API from the Select API drop-down list.
Choose the name of a stage of the chosen API from the Select stage drop-down list.
Choose Add to save the setting.
The console UI elements shown above are not available if usage plans have been enabled for your account and region.
Distribute the API key to your customers and ask them to add the key as the
x-api-keyheader to call the key-required method. For example, if the API key of
hzYAVO9Sg98nsNh81M84O2kyXVy6K1xwHD8is required on the
GET /method in the
teststage of an API (
yd4f8dz2vf), the caller must submit the following request to invoke the method.
GET /test HTTP/1.1 Host: yd4f8dz2vf.execute-api.us-east-1.amazonaws.com Content-Type: application/json x-api-key: hzYAVO9Sg98nsNh81M84O2kyXVy6K1xwHD8