Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In AWS how do I add an API key to my API?

Hello: I've been following two tutorials in the AWS documentation:

creating the sample pet store API (https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-from-example.html)

...and creating an API key (https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-setup-api-key-with-console.html#api-gateway-usage-plan-create-apikey).

When I test the pet store get method with the provided URL, it returns:

Welcome to your Pet Store API You have successfully deployed your first API. You are seeing this HTML page because the GET method to the root resource of your API returns this content as a Mock integration.

The Pet Store API contains the /pets and /pets/{petId} resources. By making a GET request to /pets you can retrieve a list of Pets in your API. If you are looking for a specific pet, for example the pet with ID 1, you can make a GET request to /pets/1.

You can use a REST client such as Postman to test the POST methods in your API to create a new pet. Use the sample body below to send the POST request:

{ "type" : "cat", "price" : 123.11 }

Now I go to the API Gateway -> API -> Resources -> -> Method Request -> API Key Required and change it to "True" and redeploy.

When I go to the provided URL to test, now the page returns:

{"message":"Forbidden"}

Which makes sense... I told it API required = true, right?

So my question is, how do I pass the API key? So that I don't get the "forbidden" result? I didn't see that in the tutorial links I pasted above and haven't been able to find elsewhere.

like image 786
MGoBlue93 Avatar asked Sep 16 '25 03:09

MGoBlue93


2 Answers

  1. You Create a Usage Plans
  2. Attach this usage plan to your API and Stage
  3. Create an API Key
  4. Now invoke your API with header named x-api-key and value of it is the API Key created in step-3

Sample: curl -i -H "x-api-key: Cd2YiWs8Fv8Lg6njI0wXf1iiNOE94XjM3EQe8567" -X GET https://7r9cvghbf4.execute-api.ap-northeast-2.amazonaws.com/dd/pets

like image 75
Oxi Avatar answered Sep 19 '25 07:09

Oxi


Assuming you've followed all steps for creating the API key you can use this API key by specifying it in the x-api-key header within your request.

You distribute API keys to your customers and require them to pass the API key as the X-API-Key header of each incoming request.

More information for using API keys in API Gateway is available on: Choose an API key source - Amazon API Gateway

like image 31
Chris Williams Avatar answered Sep 19 '25 09:09

Chris Williams