Allow unauthenticated guest access to your application using API keys
When you call Amazon Location Service APIs in your applications, you typically make this call as an authenticated user who is authorized to make the API calls. However, there are some cases where you do not want to authenticate every user of your application. For example, you might want a web application that shows your business location to be available to anyone using the website, whether they are logged in or not. In this case, one alternative is to use API keys to make the API calls.
API keys are a key value that is associated with
specific Amazon Location Service resources in your AWS account, and specific actions that you can
perform on those resources. You can use an API key in your application to make
unauthenticated calls to the Amazon Location APIs for those resources. For example, if you
associate an API key with the map resource myMap, and
the GetMap*
actions, then an application that uses that API key will be able
to view maps created with that resource, and your account will be charged as any other usage
from your account. That same API key would not give permissions to change or update the map
resource—only using the resource is allowed.
Note
API keys are available to use only with map, place, and route resources, and you cannot modify or create those resources. If your application needs access to other resources or actions for unauthenticated users, you can use Amazon Cognito to provide access along with, or instead of, API keys. For more information, see Allow unauthenticated guest access to your application using Amazon Cognito.
API keys include a plain text value that gives access to one or more resources in your AWS account. If someone copies your API key, they can access those same resources. To avoid this, you can specify the domains where the API key can be used when you create the key. These domains are called referers. If needed, you can also create short term API Keys by setting expiration times for your API Keys.
Topics
API keys compared to Amazon Cognito
API keys and Amazon Cognito are used in similar ways for similar scenarios, so why would you use one over the other? The following list highlights some of the differences between the two.
-
API keys are available only for map, place, and route resources, and only for certain actions. Amazon Cognito can be used to authenticate access to most Amazon Location Service APIs.
-
The performance of map requests with API keys is typically faster than similar scenarios with Amazon Cognito. Simpler authentication means fewer round trips to the service and cached requests when getting the same map tile again in a short time period.
-
With Amazon Cognito, you can use your own authentication process or combine multiple authentication methods, using Amazon Cognito Federated Identities. For more information, see Getting Started with Federated Identities in the Amazon Cognito Developer Guide.
Create API keys
You can create an API key, and associate it with one or more resources in your AWS account.
You can create an API key using the Amazon Location Service console, the AWS CLI, or the Amazon Location APIs.
Use an API key to call an Amazon Location API
After you create an API key, you can use the key value to make calls to Amazon Location APIs in your application.
The APIs that support API keys have an additional parameter that takes the API key
value. For example, if you call the GetPlace
API, you can fill in the
key parameter, as follows
GET /places/v0/indexes/
IndexName
/places/PlaceId
?key=KeyValue
If you fill in this value, you do not need to authenticate the API call with AWS Sig v4 as you normally would.
For JavaScript developers, you can use the Amazon Location JavaScript Authentication helper to help with authenticating API operations with API keys.
For mobile developers, you can use the following Amazon Location mobile authentication SDKs:
For AWS CLI users, when you use the --key
parameter, you should also use
the --no-sign-request
parameter, to avoid signing with Sig v4.
Note
If you include both a key
and an AWS Sig v4 signature in a call to
Amazon Location Service, only the API key is used.
Use an API key to render a map
You can use the API key value to render a map in your application using MapLibre. This is a little bit different than using the API keys in other Amazon Location APIs that you are calling directly, because MapLibre makes those calls for you.
The following sample code shows using the API key to render a map in a simple webpage
by using the MapLibre GL JS map control. For this code to work properly, replace the
v1.public.your-api-key-value
,
us-east-1
, and ExampleMap
strings with values that match your AWS account.
<!-- index.html --> <html> <head> <link href="https://unpkg.com/maplibre-gl@1.14.0/dist/maplibre-gl.css" rel="stylesheet" /> <style> body { margin: 0; } #map { height: 100vh; } </style> </head> <body> <!-- Map container --> <div id="map" /> <!-- JavaScript dependencies --> <script src="https://unpkg.com/maplibre-gl@1.14.0/dist/maplibre-gl.js"></script> <script> const apiKey = "
v1.public.your-api-key-value
"; // API key const region = "us-east-1
"; // Region const mapName = "ExampleMap
"; // Map name // URL for style descriptor const styleUrl = `https://maps.geo.${region}.amazonaws.com/maps/v0/maps/${mapName}/style-descriptor?key=${apiKey}`; // Initialize the map const map = new maplibregl.Map({ container: "map", style: styleUrl, center: [-123.1187, 49.2819], zoom: 11, }); map.addControl(new maplibregl.NavigationControl(), "top-left"); </script> </body> </html>
Manage API key lifetimes
You can create API keys that work indefinitely. However, if you want to create a temporary API key, rotate API keys on a regular basis, or revoke an existing API key, you can use API key expiration.
When creating a new API key, or updating an existing one, you can set the expiration time for that API key.
-
When an API key reaches its expiration time, the key is automatically deactivated. Inactive keys can no longer be used to make maps requests.
-
You can delete an API key 90 days after deactivating it.
-
If you have an inactive key that you haven't yet deleted, you can restore it by updating the expiration time to a future time.
-
To create a permanent key, you can remove the expiration time.
-
If you attempt to deactivate an API key that has been used within the last 7 days, you'll be prompted to confirm that you want to make the change. If you are using the Amazon Location Service API, or the AWS CLI, you will receive an error, unless you set
ForceUpdate
parameter to true.