Google Maps Platform products are secured from unauthorized use by restricting API calls to those that provide proper authentication credentials. These credentials are in the form of an API key - a unique Alpha numeric string that associates your Google billing account with your project, and with the specific API or SDK.
This guide shows how to create, restrict, and use your API key for Google Maps Platform.
Before you begin
Before you start using the Roads API, you need a project with a billing account and the Roads API enabled. To learn more, seeSet up in Cloud Console.
Creating API keys
The API key is a unique identifier that authenticates requests associated with your project for usage and billing purposes. You must have at least one API key associated with your project.
To create an API key:
Console
-
Go to theGoogle Maps Platform > Credentialspage.
-
On theCredentialspage, clickCreate credentials > API key.
TheAPI key createddialog displays your newly created API key. -
ClickClose.
The new API key is listed on theCredentialspage underAPI keys.
(Remember torestrict the API key before using it in production.)
Cloud SDK
gcloudAlphaservicesapi-keyscreate\ --project"PROJECT"\ --display-name"DISPLAY_NAME"
Read more about the Google Cloud SDK , Cloud SDK installation ,and the following commands:
Restricting API keys
Google strongly recommends that you restrict your API keys by limiting their usage to those only APIs needed for your application. Restricting API keys adds security to your application by protecting it from unwarranted requests. For more information, see API security best practices.
To restrict an API key:
Console
-
Go to theGoogle Maps Platform > Credentialspage.
- Select the API key that you want to set a restriction on. The API key property page appears.
- UnderKey restrictions,set the following restrictions:
- Application restrictions:
- To accept requests from the list of web server IP addresses that you supply, select IP addresses (web servers, cron jobs, etc.)from the list of Application restrictions.Specify one or more IPv4 or IPv6 address, or subnet using CIDR notation. The IP addresses must match the source address the Google Maps Platform servers observe. If you use network address translation (NAT), this would typically correspond to your machine'spublicIP address.
- ClickRestrict key.
- SelectRoads APIfromSelect APIsdropdown. If the Roads API is not listed, you need to enableit.
Cloud SDK
List existing keys.
gcloudservicesapi-keyslist--project="PROJECT"
Clear existing restrictions on existing key.
gcloudAlphaservicesapi-keysupdate"projects/PROJECT/keys/KEY_ID"\ --clear-restrictions
Set new restrictions on existing key.
gcloudAlphaservicesapi-keysupdate"projects/PROJECT/keys/KEY_ID"\ --api-target="roads.googleapis" --allowed-ips="IP_ADDRESS"
Read more about the Google Cloud SDK , Cloud SDK installation ,and the following commands:
Adding the API key to your request
You must include an API key with every Roads API request. In the following example,
replaceYOUR_API_KEY
with your API key.
https://roads.googleapis /v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836
&interpolate=true
&key=YOUR_API_KEY
HTTPS is required for requests that use an API key.