Use API Keys with Distance Matrix API Stay organized with collections Save and categorize content based on your preferences.
AI-generated Key Takeaways
Google Maps Platform secures its products by requiring API keys for authentication and billing purposes.
You can create and restrict API keys through the Google Cloud Console or the Cloud SDK.
Restricting API keys enhances security by limiting their usage to specific applications and IP addresses.
API keys must be included in every Distance Matrix API request using HTTPS.
When constructing URLs for web services, special characters need to be URL encoded and the total length should not exceed 2048 characters.
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 alphanumeric 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 Distance Matrix API, you need a project with a billing account and the Distance Matrix 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 > Credentials page.
- On theCredentials page, clickCreate credentials > API key.
TheAPI key created dialog displays your newly created API key. - ClickClose.
The new API key is listed on theCredentials page underAPI keys.
(Remember torestrict the API key before using it in production.)
Cloud SDK
gcloudservicesapi-keyscreate\--project"PROJECT"\--display-name"DISPLAY_NAME"
Read more about theGoogle 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 onlyAPIs needed for your application. Restricting API keys adds security to your application byprotecting it from unwarranted requests.You are financially responsible for charges caused by abuse of unrestricted API keys. For more information, seeAPI security best practices.When restricting an API key in the Cloud console,Application restrictions override any APIs enabled underAPI restrictions.Follow best practices by creating a separate API key for each app, and for each platformon which that app is available. To restrict an API key: Go to theGoogle Maps Platform > Credentials page. List existing keys. Clear existing restrictions on existing key. Set new restrictions on existing key. Read more about theGoogle Cloud SDK ,Cloud SDK installation , and the following commands: You must include an API key with every Distance Matrix API request. In the following example, replace HTTPS is required for requests that use an API key.Console
Cloud SDK
Warning: Running thegcloud alpha services api-keys update command replaces any existing restrictions on the API key. It does not append the new restrictions passed to the command to the existing restrictions.gcloudservicesapi-keyslist--project="PROJECT"
gcloudservicesapi-keysupdate"projects/PROJECT/keys/KEY_ID"\--clear-restrictions
gcloudservicesapi-keysupdateprojects/PROJECT/locations/global/keys/KEY_ID\--api-target=service=distance-matrix-backend.googleapis.com--allowed-ips="IP_ADDRESS"
Adding the API key to your request
YOUR_API_KEY with your API key.https://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&key=YOUR_API_KEY
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-11-21 UTC.