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.
- 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 DFP network
- Go to your DoubleClick for Publishers network.
- Click the Admin tab.
- Ensure that API access is enabled.
- Click the Add a service account user button.
- Fill in the form using the service account email you created in the previous step.
- 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.
- Setup your credentials
Run the following command in a shell:
curl https://raw.githubusercontent.com/googleads/googleads-java-lib/master/examples/dfp_axis/src/main/resources/ads.properties -o ~/ads.properties
Open the~/ads.properties
file and populate the following 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 [...]
-
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.dfp.axis.factory.DfpServices; import com.google.api.ads.dfp.axis.v201705.Network; import com.google.api.ads.dfp.axis.v201705.NetworkServiceInterface; import com.google.api.ads.dfp.lib.client.DfpSession; 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.DFP) .fromFile() .build() .generateCredential(); // Construct a DfpSession. DfpSession session = new DfpSession.Builder() .fromFile() .withOAuth2Credential(oAuth2Credential) .build(); // 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(); // Retrieve the appropriate service NetworkServiceInterface networkService = dfpServices.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()); } }
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.
- Install the library and setup your credentials.
Run the following commands in a shell:
pip install googleads curl https://raw.githubusercontent.com/googleads/googleads-python-lib/master/googleads.yaml -o ~/googleads.yaml
- Setup your
~/googleads.yaml
file.Fill in the following fields:
dfp: 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 dfp # Initialize a client object, by default uses the credentials in ~/googleads.yaml. dfp_client = dfp.DfpClient.LoadFromStorage() # Initialize a service. network_service = dfp_client.GetService('NetworkService', version='v201705') # Make a request. current_network = network_service.getCurrentNetwork() print 'Found network %s (%s)!' % (current_network['displayName'], current_network['networkCode'])
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
- Create a new project
Open Visual Studio and create a new project (i.e. Console Application).
- 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.
- 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
- 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
DfpUser user = new DfpUser(); InventoryService inventoryService = (InventoryService) dfpUser.GetService(DfpService.v201705.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.
- 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
- 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, :v201705)
- 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.