Get started with the AdMob API

This document is for developers who want to use the AdMob API to get information about their AdMob account. To use this document, you must be familiar with web programming concepts and web data.

Before we start, make sure you already have:

Also, get familiar with how to authorize requests in the "Authorize request" section.

Get an authorization token

Use oauth2l to automate the process of getting an access token. For your application, you can use libraries listed in Set up your application.

oauth2l fetch --json <path_to_credentials_json> https://www.googleapis.com/auth/admob.report

Replace <path_to_credentials_json> with a path to a credentials.json file that you download when you register your cloud application. The first time, the command walks you through the OAuth 2.0 authorization flow. Subsequent runs refresh the token automatically.

Get account info

You can request account info by calling get or list accounts methods. See the following examples:

# Replace the <access_token> with one received in the previous section.
curl -X GET \
      https://admob.googleapis.com/v1/accounts \
      -H "Authorization: Bearer <access_token>"
You can use oauth2l to generate the "Authorization" header, as shown here:
# The approach wouldn't work without initial steps described in the Get authorization token.
curl -X GET \
     https://admob.googleapis.com/v1/accounts \
     -H "$(oauth2l header --json <path_to_credentials_json> https://www.googleapis.com/auth/admob.report)"

The response would look like:

{
  "account": [
    {
      "publisherId": "pub-1234567890123456",
      "reportingTimeZone": "Europe/Paris",
      "currencyCode": "EUR"
    }
  ]
}

Generate a network report

You can generate a network report by calling the networkReport:generate method. Use the publisherId received in the Get account info step as part of the request URI (for example, v1/accounts/pub-1234567890123456/networkReport:generate). Use the following code to generate the network report:

curl -X POST \
     https://admob.googleapis.com/v1/accounts/pub-1234567890123456/networkReport:generate \
     -H "Authorization: Bearer <access_token>" \
     -H "Content-Type: application/json" \
     --data @- << EOF
{
  "report_spec": {
    "date_range": {
      "start_date": {"year": 2019, "month": 4, "day": 1},
      "end_date": {"year": 2019, "month": 4, "day": 2}
    },
    "dimensions": ["DATE"],
    "metrics": ["CLICKS", "AD_REQUESTS", "IMPRESSIONS", "ESTIMATED_EARNINGS"],
    "dimension_filters": [{"dimension": "COUNTRY", "matches_any": {"values": ["US"]}}],
    "sort_conditions": [{"metric":"CLICKS", order: "DESCENDING"}],
    "localization_settings": {"currency_code": "USD", "language_code": "en-US"}
  }
}
EOF

This request returns the following response:

[{
  "header": {
    "dateRange": {
      "startDate": {"year": 2019, "month": 4, "day": 1},
      "endDate": {"year": 2019, "month": 4, "day": 2}
    }
  }
},
{
  "row": {
    "dimensionValues": {"DATE": {"value": "20190401"}},
    "metricValues": {
      "CLICKS": {"integerValue": "31"},
      "AD_REQUESTS": {"integerValue": "7409"},
      "IMPRESSIONS": {"integerValue": "3440"},
      "ESTIMATED_EARNINGS": {"microsValue": "6381903"}
    }
  }
},
{
  "row": {
    "dimensionValues": {"DATE": {"value": "20190402"}},
    "metricValues": {
      "CLICKS": {"integerValue": "30"},
      "AD_REQUESTS": {"integerValue": "8828"},
      "IMPRESSIONS": {"integerValue": "3724"},
      "ESTIMATED_EARNINGS": {"microsValue": "9010390"}
    }
  }
},
{
  "footer": {
    "matchingRowCount": "2"
  }
}]

Now you're ready to play with different dimensions, metrics, and other network report properties.