Enforcing the API Key Requirement
To secure your API endpoint, begin by navigating to the specific method request where you want to require an API key. Click Edit and enable the API Key Required option. Once you save the changes and deploy your API, any request without a valid API key will be rejected with a forbidden error. For example, if you send a request without the required header, you might receive one of the following responses:Creating a Usage Plan
Before generating an API key, it is essential to define a usage plan. A usage plan specifies the maximum number of requests a user can make over a predetermined period and can include throttling settings to prevent abuse. In the API Gateway console, follow these steps:- Navigate to the Usage Plan section.
- Create a new usage plan (e.g., name it “premium”).
- Define different models if required—for example, offering a free plan for general access and a premium plan for increased rate limits.

- Rate: Total number of requests allowed per second (e.g., 2 requests per second).
- Burst: Maximum number of concurrent requests a client can submit at one time (e.g., 10 requests).
- Quota: Total number of requests permitted per time period (e.g., 20 requests per day).

Creating and Associating an API Key
Next, create an API key for your client by following these steps:- In the AWS console, create a new API key and give it a descriptive name (e.g., “user1”).
- Choose to auto-generate the key or customize it as per your requirements.
- Once the key is generated, view or copy its value.

- In the API Gateway console, select the newly created API key.
- Click Add to Usage Plan.
- Select the “premium” usage plan previously created and save the changes.

x-api-key followed by the API key value.
Testing API Access with Quota Enforcement
With the API key in place, sending a properly authenticated request to the API Gateway should return a successful response, such as:Remember that the usage plan enforces a daily quota (in this example, 20 requests). Repeated requests beyond this limit will trigger a quota restriction.