Is there a way to programmatically create a Google Maps API key? I googled this but couldn't find a straightforward answer.
As of Nov 2023, you can programmatically create API keys with Google Cloud's CLI (gcloud
).
You can use gcloud
locally or as part of your CI/CD pipelines, e.g. in Github Actions.
Once you've got gcloud
installed, authenticate and set the corresponding account + project:
gcloud config set account my@email.com
gcloud config set project my-project-name
After that, look into the services api-keys create
command. Right now it's only available in beta/alpha
but it gets the job done:
gcloud beta services api-keys create \
--display-name="Frontend API key - Prod" \
--allowed-referrers="https://*.example.com/*" \
--api-target=service=places.googleapis.com,service=maps.googleapis.com
To retrieve the list of available services (Maps and other GCP APIs) to pass in --api-target
, run:
gcloud services list --available --filter="name:googleapis.com"
💡 For each gcloud CLI operation, there's usually a REST call at your disposal. I couldn't find create
but here are the docs for the list
command. From this you can try to derive the create
call - perhaps it's already implemented but not yet documented?
To learn more about API keys, read the Google guide or this chapter of my Google Maps Handbook.