Get Started

Developers can use the DFP API to build applications that manage inventory, create orders, pull reports, and more.

The DFP API uses SOAP; to help you get started, we offer client libraries in Java, .NET, Python, PHP, and Ruby. To make your first API request, follow the steps below.

Get access to a DFP network

If you don't already have one, sign up for a DFP account. You can also create a test network if you want to test the API in a separate environment.

Make a note of your network code. You'll find this in the URL when you are logged into your network. For example, in the URL https://www.google.com/dfp/2032576#delivery, 2032576 is your network code.

Create authentication credentials

You must authenticate all DFP API requests using OAuth2. The steps below cover the simple use case of accessing your own DFP data. For more details and other options, see Authentication.

  1. Open the Google API Console Credentials page.
  2. From the project menu, choose Create project, enter a name for the project, and optionally, edit the provided Project ID. Click Create.
  3. On the Credentials page, select Create credentials, then select Service account key.
  4. Select New service account, and select JSON as the key type.
  5. Click Create to download a file containing a private key.

Configure your DFP network

  1. Go to your DoubleClick for Publishers network.
  2. Click the Admin tab.
  3. Ensure that API access is enabled.
  4. Click the Add a service account user button.
  5. Fill in the form using the service account email you created in the previous step.
  6. Click on the Save button. A message should appear, confirming the addition of your service account.

Set up your client

Download one of the DFP client libraries. The libraries offer wrapper functions and features that make it easier and faster to develop applications.

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.

  1. Create an Eclipse project

    Open Eclipse and follow these instructions to set up Eclipse with Maven.

  2. Set up your credentials

    Copy the ads.properties file to your home directory and populate the required fields.

    [...]
    api.dfp.applicationName=INSERT_APPLICATION_NAME_HERE
    api.dfp.jsonKeyPairPath=INSERT_PATH_TO_JSON_KEY_FILE_HERE
    api.dfp.networkCode=INSERT_NETWORK_CODE_HERE
    [...]

    Credentials and properties in fromFile() are pulled from the ads.properties file. See the README for more info.

    // Generate a refreshable OAuth2 credential.
    Credential oAuth2Credential = new OfflineCredentials.Builder()
        .forApi(Api.DFP)
        .fromFile()
        .build()
        .generateCredential();

    You can then create your DfpSession using the Builder pattern:

    // Construct a DfpSession.
    DfpSession session = new DfpSession.Builder()
        .fromFile()
        .withOAuth2Credential(oAuth2Credential)
        .build();
  3. Construct a client for the InventoryService

    You will need a SOAP Client Stub to make calls to the API. The DfpServices class handles this logic. This object is expensive to construct - instantiate it once and reuse as much as possible within a thread.

    // Construct a DFP service factory, which can only be used once per thread,
    // but should be reused as much as possible.
    DfpServices dfpServices = new DfpServices();
    You can then obtain a reference to the service class like so:
    InventoryServiceInterface inventoryService =
        dfpServices.get(session, InventoryServiceInterface.class);
  4. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code returns all ad units present within the account into an AdUnitPage object.

    // Create a statement to select all ad units.
    StatementBuilder statementBuilder = new StatementBuilder()
        .orderBy("id ASC")
        .limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    
    // Default for total result set size.
    int totalResultSetSize = 0;
    
    do {
      // Get ad units by statement.
      AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.toStatement());
    
      if (page.getResults() != null) {
        totalResultSetSize = page.getTotalResultSetSize();
        int i = page.getStartIndex();
        for (AdUnit adUnit : page.getResults()) {
          System.out.printf(
              "%d) Ad unit with ID '%s' and name '%s' was found.%n", i++,
              adUnit.getId(), adUnit.getName());
        }
      }
    
      statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    
    System.out.printf("Number of results found: %d%n", totalResultSetSize);

For more detailed information about using the Java Client Library, refer to the README file in the client library distribution.

Python

Here is a basic example that shows how to use the Python client library.

The Python Client Library supports Python v2.7 or Python v3.0+ via the 2to3 converter.

Before you can run this example, you must follow the steps in the README file that is distributed with the Python Client Library. If you need help authenticating against our client library, you must also set up your OAuth2 credentials as outlined in our OAuth2 wiki.

  1. Set up your credentials

    Copy the googleads.yaml file to your home directory and populate the required fields.

    dfp:
      application_name: INSERT_APPLICATION_NAME_HERE
      network_code: INSERT_NETWORK_CODE_HERE
      path_to_private_key_file: INSERT_PATH_TO_FILE_HERE
    

    To initialize the Client object, call DfpClient.LoadFromStorage() to read the fields in the googleads.yaml file.

    from googleads import dfp
    
    # Initialize client object.
    dfp_client = dfp.DfpClient.LoadFromStorage()
    
  2. Construct a client for the InventoryService

    Each service can be retrieved through its appropriate getService() method. The getService() method returns a SOAP client to that service.

    # Initialize appropriate service.
    ad_unit_service = client.GetService('InventoryService', version='v201611')
    
  3. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code returns and prints all of the ad units present within a network.

    statement = dfp.FilterStatement()
    
    # Retrieve a small amount of ad units at a time, paging
    # through until all ad units have been retrieved.
    while True:
      response = ad_unit_service.getAdUnitsByStatement(statement.ToStatement())
      if 'results' in response:
        for ad_unit in response['results']:
          # Print out some information for each ad unit.
          print('Ad unit with ID "%s" and name "%s" was found.\n' %
                (ad_unit['id'], ad_unit['name']))
        statement.offset += dfp.SUGGESTED_PAGE_LIMIT
      else:
        break
    
    print '\nNumber of results found: %s' % response['totalResultSetSize']

PHP

Follow the steps in the ads PHP client library README to get started.

.NET

Here is a basic example that shows how to use the .NET client library

  1. Create a new project

    Open Visual Studio and create a new project (i.e. Console Application).

  2. Add required library references to your project

    If you're using NuGet, simply add a dependency for Google.Dfp, or Google.Dfp.Examples.CSharp if you want to run code examples.

    Otherwise, download the binary distribution from GitHub and copy all dlls from the \lib folder into your project. Add references to these dlls and to System.Web.Services in your project.

  3. 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/DfpApi
    • configuration/system.web
    • configuration/configSections/section[name="DfpApi"]
    • configuration/system.net
  4. 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" />
    
  5. Make a call to the library

    You can call the library as shown in the following C# code snippet

    DfpUser user = new DfpUser();
    InventoryService inventoryService =
        (InventoryService) dfpUser.GetService(DfpService.v201608.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 DfpUser 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 2.2 as well as the Google Ads Savon SOAP toolkit of version 1.0.0 or later.

Before you can run the example, you must follow the steps in the README file that is distributed with the Ruby Client Library. You will need to edit dfp_api.yml to include your authentication credentials.

  1. Set up your credentials

    Copy the dfp_api.yml file to your home directory and populate the required fields.

    [...]
    :oauth2_keyfile: INSERT_PATH_TO_JSON_KEY_FILE_HERE
    :application_name: INSERT_APPLICATION_NAME_HERE
    :network_code: INSERT_NETWORK_CODE_HERE
    [...]
    

    First, create a new DfpApi instance. This loads the credentials from the ~/dfp_api.yml file. Refer to the README file for alternate ways to specify credentials.

    # Get DfpApi instance and load configuration from ~/dfp_api.yml.
    dfp = DfpApi::Api.new
    
  2. Construct an InventoryService instance

    Each service can be retrieved through the service() method. This method returns a service stub object that can be used to call its methods. Specify the service name as the first parameter and required API version as the second parameter.

    inventory_service =
        dfp.service(:InventoryService, :v201608)
    
  3. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code returns all ad units present within the account into a page object, 500 at a time.

    # Create a statement to select ad units.
    statement = DfpApi::FilterStatement.new()
    
    # Retrieve a small amount of ad units at a time, paging
    # through until all ad units have been retrieved.
    total_result_set_size = 0;
    begin
      page = inventory_service.get_ad_units_by_statement(
          statement.toStatement())
    
      # Print out some information for each ad unit.
      if page[:results]
        total_result_set_size = page[:total_result_set_size]
        page[:results].each_with_index do |ad_unit, index|
          puts "%d) Ad unit with ID '%s' and name '%s' was found." % [
              index + statement.offset,
              ad_unit[:id],
              ad_unit[:name]
          ]
        end
      end
      statement.offset += DfpApi::SUGGESTED_PAGE_LIMIT
    end while statement.offset < page[:total_result_set_size]
    
    puts 'Total number of ad units: %d' %
        total_result_set_size

If you want to develop in Ruby without the client library, please refer to the wiki article.

Next Steps

When you have got a client library up and running, go ahead and play with the examples provided to extend them for your needs.

Visit the reference documentation to learn more about the API.

If you need help, read the advanced topics listed under the Guides section or visit the Forum.

Send feedback about...

DoubleClick for Publishers
DoubleClick for Publishers
Need help? Visit our support page.