Using API Keys

Select platform: Android iOS JavaScript Web Service

Before you begin

Before you start using the Places SDK for Android, you need a project with a billing account and the Places SDK for Android 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

  1. Go to theGoogle Maps Platform > Credentialspage.

    Go to the Credentials page

  2. On theCredentialspage, clickCreate credentials > API key.
    TheAPI key createddialog displays your newly created API key.
  3. 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.

When you restrict your API key, you must provide the SHA-1 certificate fingerprint of the signing key that was used to sign the application. There are two certificate types:

  • Debug certificate:Only use this certificate type with apps you're testing and other non-production code. Do not attempt to publish an app that's signed with a debug certificate. The Android SDK tools generate this certificate automatically when you run a debug build.
  • Release certificate:Use this certificate when you're ready to release your app to an app store. The Android SDK tools generate this certificate when you run a release build.

For more information about Android application signing and certificates, see the Sign your app guide.

For instructions on how to find your signing certificate fingerprint, see the references below:

To restrict an API key:

Console

  1. Go to theGoogle Maps Platform > Credentialspage.

    Go to the Credentials page

  2. Select the API key that you want to set a restriction on. The API key property page appears.
  3. UnderKey restrictions,set the following restrictions:
  • Application restrictions:
  1. SelectAndroid apps.
  2. Click+ Add package name and fingerprint.
  3. Enter your package name and SHA-1 certificate fingerprint. For example:
    com.example.android.mapexample
    BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75
  • API restrictions:
    1. ClickRestrict key.
    2. Select eitherPlaces APIorPlaces API (New) fromSelect APIsdropdown, but not both. If the Places API is not listed, you need to enableit.
  • To finalize your changes, clickSave.
  • Cloud SDK

    Places SDK for Android (New)

    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="places.googleapis"
    --allowed-application="sha1_fingerprint=SHA1_FINGERPRINT,package_name=ANDROID_PACKAGE_NAME"

    Read more about the Google Cloud SDK , Cloud SDK installation ,and the following commands:

    Places SDK for Android

    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="places-backend.googleapis"
    --allowed-application="sha1_fingerprint=SHA1_FINGERPRINT,package_name=ANDROID_PACKAGE_NAME"

    Read more about the Google Cloud SDK , Cloud SDK installation ,and the following commands:

    What's next

    Now that you have an API key, create and configure a project as described in Set Up an Android Studio Project.