You can use the Google Ad Manager API to build apps that manage inventory, create orders, pull reports, and more.
The Ad Manager API uses SOAP. To help you get started, we offer client libraries for Java, .NET, Python, PHP, and Ruby.
To make your first API request, follow these steps:
Get access to an Ad Manager network
If you don't already have one, sign up for an Ad Manager account. You can also create a test network if you want to test the API in a separate environment. Note that you don't need an AdSense account for test purposes.
Make a note of your network code. You can find this in the URL when you sign in
to your network. For example, in the URL
https://admanager.google.com/1234#home
, 1234
is your network code.
Create authentication credentials
You must authenticate all Ad Manager API requests using OAuth 2.0. The steps below cover the use case of accessing your own Ad Manager data. For more details and other options, see Authentication.
Open the Google API Console Credentials page
From the project menu, choose Create project, enter a name for the project, and optionally, edit the provided Project ID. Click Create.
On the Credentials page, select Create credentials, then select Service account key.
Select New service account and select
JSON
as the key type.Click Create to download a file containing a private key.
Configure your Ad Manager network
Sign in to Google Ad Manager.
In the sidebar, click Admin > Global settings.
Under General settings > Api access click the slider to Enabled.
Click the Save button at the bottom of the page.
Set up your client
Download one of the Ad Manager client libraries. The libraries offer wrapper functions and features that make it easier and faster to develop apps.
The tabs below provide quickstarts for coding in each of the languages for which there is a client library.
Java
Here is a basic example that shows how to use the Java client library. For more detailed usage information, refer to the README file in the client library distribution.
- Setup your credentials
Run the following command in a shell:
Open thecurl https://raw.githubusercontent.com/googleads/googleads-java-lib/main/examples/admanager_axis/src/main/resources/ads.properties -o ~/ads.properties
~/ads.properties
file and populate the following fields:[...] api.admanager.applicationName=INSERT_APPLICATION_NAME_HERE api.admanager.jsonKeyFilePath=INSERT_PATH_TO_JSON_KEY_FILE_HERE api.admanager.networkCode=INSERT_NETWORK_CODE_HERE [...]
-
Specify dependencies
Edit your
pom.xml
file and add the following to thedependencies
tag. You can find the latest version number on Github.<dependency> <groupId>com.google.api-ads</groupId> <artifactId>ads-lib</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>com.google.api-ads</groupId> <artifactId>dfp-axis</artifactId> <version>RELEASE</version> </dependency>
-
Write some code and make a request!
import com.google.api.ads.common.lib.auth.OfflineCredentials; import com.google.api.ads.common.lib.auth.OfflineCredentials.Api; import com.google.api.ads.admanager.axis.factory.AdManagerServices; import com.google.api.ads.admanager.axis.v202408.Network; import com.google.api.ads.admanager.axis.v202408.NetworkServiceInterface; import com.google.api.ads.admanager.lib.client.AdManagerSession; import com.google.api.client.auth.oauth2.Credential; public class App { public static void main(String[] args) throws Exception { Credential oAuth2Credential = new OfflineCredentials.Builder() .forApi(Api.AD_MANAGER) .fromFile() .build() .generateCredential(); // Construct an AdManagerSession. AdManagerSession session = new AdManagerSession.Builder() .fromFile() .withOAuth2Credential(oAuth2Credential) .build(); // Construct a Google Ad Manager service factory, which can only be used once per // thread, but should be reused as much as possible. AdManagerServices adManagerServices = new AdManagerServices(); // Retrieve the appropriate service NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class); // Make a request Network network = networkService.getCurrentNetwork(); System.out.printf("Current network has network code '%s' and display" + " name '%s'.%n", network.getNetworkCode(), network.getDisplayName()); } }
Python
Here is a basic example that shows how to use the Python client library. The Python Client Library supports Python v3.6+. For more detailed usage information, refer to the README file in the client library distribution.
- Install the library and set up your credentials.
Run the following commands in a shell:
pip install googleads
curl https://raw.githubusercontent.com/googleads/googleads-python-lib/main/googleads.yaml \ -o ~/googleads.yaml
- Set up your
~/googleads.yaml
file.Fill in the following fields:
ad_manager: application_name: INSERT_APPLICATION_NAME_HERE network_code: INSERT_NETWORK_CODE_HERE path_to_private_key_file: INSERT_PATH_TO_FILE_HERE
-
Run some code and make a request.
# Import the library. from googleads import ad_manager # Initialize a client object, by default uses the credentials in ~/googleads.yaml. client = ad_manager.AdManagerClient.LoadFromStorage() # Initialize a service. network_service = client.GetService('NetworkService', version='v202408') # Make a request. current_network = network_service.getCurrentNetwork() print("Current network has network code '%s' and display name '%s'." % (current_network['networkCode'], current_network['displayName']))
PHP
Here is a basic example that shows how to use the PHP client library.
-
Install the library and setup your credentials.
Run the following commands in a shell to install the client library and download the adsapi_php.ini file to your home directory:
composer require googleads/googleads-php-lib
curl https://raw.githubusercontent.com/googleads/googleads-php-lib/main/examples/AdManager/adsapi_php.ini -o ~/adsapi_php.ini
-
Setup your
~/adsapi_php.ini
file.Fill in the following fields:
[AD_MANAGER] networkCode = "INSERT_NETWORK_CODE_HERE" applicationName = "INSERT_APPLICATION_NAME_HERE" [OAUTH2] jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE" scopes = "https://www.googleapis.com/auth/dfp"
-
Run some code and make a request!
<?php require 'vendor/autoload.php'; use Google\AdsApi\AdManager\AdManagerSession; use Google\AdsApi\AdManager\AdManagerSessionBuilder; use Google\AdsApi\AdManager\v202408\ApiException; use Google\AdsApi\AdManager\v202408\ServiceFactory; use Google\AdsApi\Common\OAuth2TokenBuilder; // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder()) ->fromFile() ->build(); // Construct an API session configured from a properties file and the OAuth2 // credentials above. $session = (new AdManagerSessionBuilder()) ->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); // Get a service. $serviceFactory = new ServiceFactory(); $networkService = $serviceFactory->createNetworkService($session); // Make a request $network = $networkService->getCurrentNetwork(); printf( "Network with code %d and display name '%s' was found.\n", $network->getNetworkCode(), $network->getDisplayName() );
.NET
Here is a basic example that shows how to use the .NET client library
- Create a new project
Open Visual Studio and create a new project (Console Application).
- Add required library references to your project
Add a nuget dependency for Google.Dfp.
- Setup your App.config
Copy src\App.config to your project directory and add it to your project. If your application has its own App.config, then you can copy the following nodes into your App.config:
- configuration/AdManagerApi
- configuration/configSections/section[name="AdManagerApi"]
- configuration/system.net
- Setup credentials
Open App.config and edit the following keys:
<add key="ApplicationName" value="INSERT_YOUR_APPLICATION_NAME_HERE" /> <add key="NetworkCode" value="INSERT_YOUR_NETWORK_CODE_HERE" /> <add key="OAuth2Mode" value="SERVICE_ACCOUNT" /> <add key="OAuth2SecretsJsonPath" value="INSERT_OAUTH2_SECRETS_JSON_FILE_PATH_HERE" />
- Make a call to the library
You can call the library as shown in the following C# code snippet
AdManagerUser user = new AdManagerUser(); using (InventoryService inventoryService = user.GetService<InventoryService>()) { // Create a statement to select ad units. int pageSize = StatementBuilder.SUGGESTED_PAGE_LIMIT; StatementBuilder statementBuilder = new StatementBuilder().OrderBy("id ASC").Limit(pageSize); // Retrieve a small amount of ad units at a time, paging through until all // ad units have been retrieved. int totalResultSetSize = 0; do { AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement()); // Print out some information for each ad unit. if (page.results != null) { totalResultSetSize = page.totalResultSetSize; int i = page.startIndex; foreach (AdUnit adUnit in page.results) { Console.WriteLine( "{0}) Ad unit with ID \"{1}\" and name \"{2}\" was found.", i++, adUnit.id, adUnit.name); } } statementBuilder.IncreaseOffsetBy(pageSize); } while (statementBuilder.GetOffset() < totalResultSetSize); Console.WriteLine("Number of results found: {0}", totalResultSetSize); }
If you don't want to set your credentials in your App.config, then refer to this wiki article for alternate ways of using the AdManagerUser class. For more detailed information about using the .NET Client Library, refer to the README . If you want to develop in .NET without the client library, please refer to the NoClientLibrary wiki article.
Ruby
Here is a basic example that shows how to use the Ruby client library. The Ruby client library requires Ruby 2.1 or later.
-
Install the Ruby gem and get the configuration file.
Run the following commands in a shell:
gem install google-dfp-api
curl https://raw.githubusercontent.com/googleads/google-api-ads-ruby/main/ad_manager_api/ad_manager_api.yml -o ~/ad_manager_api.yml
-
Setup your credentials
Populate the required fields in the
~/ad_manager_api.yml
file. If you don't already have an OAuth2 keyfile, you'll need to follow the steps to create your OAuth2 credentials.:authentication: :oauth2_keyfile: INSERT_PATH_TO_JSON_KEY_FILE_HERE :application_name: INSERT_APPLICATION_NAME_HERE :network_code: INSERT_NETWORK_CODE_HERE
-
Write some code and make a request!
# Import the library. require 'ad_manager_api' # Initialize an Ad Manager client instance (uses credentials in ~/ad_manager_api.yml by default). ad_manager = AdManagerApi::Api.new # Get a service instance. network_service = ad_manager.service(:NetworkService, :v202408) # Make a request. network = network_service.get_current_network() puts "The current network is %s (%d)." % [network[:display_name], network[:network_code]]
More detailed steps for getting started can be found in the README file that is distributed with the Ruby client library. Also, check out our full example library for Ruby.
Next steps
When you have a client library up and running, modify the examples provided to extend them for your needs.
Browse the reference documentation to learn more about the API.
If you need help, visit our Support page.