API Quickstart using the command line

This page shows you how to get started with the Google Analytics Admin API using the command line.

Step 1. Enable the API

Click this button to create a new Cloud Platform project, automatically enable the Google Analytics Admin API and create the OAuth2 credentials needed for this tutorial:

Enable the Google Analytics Admin API

You will be automatically redirected to the OAuth2 credentials configuration dialog. Choose "Desktop" when asked "Where are you calling from?"

In resulting dialog click DOWNLOAD CLIENT CONFIGURATION and save the file credentials.json to your working directory.

Alternatively, you can follow these steps to create an OAuth2 Credentials in an existing project manually.

Step 2. Install and initialize the Cloud SDK

You need the gcloud command line tool installed on your development machine.

Step 3: Make an API call

Now you can use the Google Analytics Admin API to query a Google Analytics 4 property.

Authenticate

To obtain user credentials in this example, run the following command and use the path to the credentials JSON file downloaded at Step 1:

gcloud auth application-default login \
    --scopes=https://www.googleapis.com/auth/analytics.readonly \
    --client-id-file=[PATH/TO/credentials.json]

An OAuth user consent dialog will open in a new browser window.

Send the request

Use curl to send the request to the Google Analytics Admin API. Enter the following on your command line:

  curl \
  -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
  -H "Content-Type: application/json; charset=utf-8" \
  https://analyticsadmin.googleapis.com/v1beta/accounts

The curl command uses the gcloud auth application-default print-access-token command to get an authentication token.

Congratulations! You've sent your first request to the Google Analytics Admin API.