Get Started

This document explains how to get started writing applications that use the Ad Exchange Seller REST API to interact with DoubleClick Ad Exchange. The Ad Exchange Seller REST API enables developers to obtain reports on Ad Exchange earnings and manage Ad Exchange ad clients , URL channels and custom channels .

Ad Exchange Seller REST API v2.0 is the latest available and recommended version.

1. Before you start

  1. If you're unfamiliar with Ad Exchange Seller concepts, read the DoubleClick Ad Exchange Help and experiment with the user interface.

2. Prepare for Authentication

Complete the following steps to prepare for authentication using OAuth 2.0; the API supports many types of credentials, for this example we will use an installed application client secrets file.

  • Go to the Google Developers Console.
  • Select a project.
  • In the sidebar on the left, select APIs and make sure the status is ON for Ad Exchange Seller REST API.
  • In the sidebar on the left, select Credentials.
  • If you do not already have a Client ID for native application
    • Select Create new Client ID.
    • Select Installed application and other and create the new ID.
  • Select Download JSON and keep the client secrets file in a safe place.

See Authentication for more information.

3. Call the Seller REST API

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 Ad Exchange Seller REST API with Java.

  1. Create an Eclipse maven project

    Open the pom.xml and add these dependencies:

      <dependencies>
        <dependency>
          <groupId>com.google.apis</groupId>
          <artifactId>google-api-services-adexchangeseller</artifactId>
          <version>RELEASE</version>
        </dependency>
        <dependency>
          <groupId>com.google.http-client</groupId>
          <artifactId>google-http-client-jackson2</artifactId>
          <version>RELEASE</version>
        </dependency>
         <dependency>
          <groupId>com.google.oauth-client</groupId>
          <artifactId>google-oauth-client-jetty</artifactId>
          <version>RELEASE</version>
        </dependency>
      </dependencies>
      

  2. Load the client secrets file

    All calls to the API require authentication; load up the client secrets file you downloaded to create a GoogleClientSecrets object.

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
            jsonFactory, new InputStreamReader(
                FirstApiRequest.class.getResourceAsStream("/client_secrets.json")));
  3. Set up your credentials

    Create a Credential object using the GoogleClientSecrets.

    // set up authorization code flow
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, jsonFactory, clientSecrets,
            Collections.singleton(AdExchangeSellerScopes.ADEXCHANGE_SELLER_READONLY))
            .setDataStoreFactory(dataStoreFactory).build();
        // authorize and get credentials
        Credential credential =
            new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
              .authorize("user");
  4. Construct a client for the AdExchangeSeller

    Use the Credential object to create an authenticated AdExchangeSeller using the Builder pattern:

    AdExchangeSeller adExchangeSeller = new AdExchangeSeller.Builder(httpTransport,
            jsonFactory, credential).setApplicationName(APPLICATION_NAME).build();

  5. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code runs a Report and displays the output.

    // Set up a report object for the last 7 days’ performance
        Generate request = adExchangeSeller.accounts().reports().generate("myaccount",
            "today-6d", "today");
    
        // Add report dimensions
        request.setDimension(Arrays.asList("DATE", "WINNING_BID_RULE_NAME"));
        // Add report metrics
        request.setMetric(Arrays.asList("AD_REQUESTS", "CLICKS"));
    
        // Run the report.
        // If this was a bigger report we would use paging, see GenerateReportWithPaging.java
        Report response = request.execute();
    
        if (response.getRows() == null || response.getRows().isEmpty()) {
          System.out.println("No rows returned.");
          return;
        }
    
        // Display the returned data
        displayHeaders(response.getHeaders());
        displayRows(response.getRows());
        displayTotals(response.getTotals());

For more detailed information about using the Ad Exchange Seller REST API with Java, refer to the README file in the Ad Exchange Seller REST API examples.

Python

Here is a basic example that shows how to use the Ad Exchange Seller REST API with Python.

  1. Download and install the Google API Python Client

    Example using pip:

    $ pip --upgrade google-api-python-client

  2. Construct a client for the AdExchangeSeller

    Create a client for the AdExchangeSeller API passing in the latest version of the API.

    Note: this approach expects a client_secrets.json file in the same directory as the .py file; this is the file you downloaded in Step 2. above

    service, _ = sample_tools.init(
          argv, 'adexchangeseller', 'v2.0', __doc__, __file__,
          scope='https://www.googleapis.com/auth/adexchange.seller.readonly')
  3. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code runs a Report and displays the output.

    result = service.accounts().reports().generate(
            accountId='myaccount',
            startDate='today-6d', endDate='today',
            metric=['AD_REQUESTS', 'CLICKS'],
            dimension=['DATE', 'WINNING_BID_RULE_NAME'],
            sort=['+DATE']).execute()
    
        # Display headers.
        for header in result['headers']:
          print '%25s' % header['name'],
        print
    
        # Display results.
        for row in result['rows']:
          for column in row:
            print '%25s' % column,
          print
    
        # Display totals
        for total in result['totals']:
          print '%25s' % total,
        print

For more detailed information about using the Ad Exchange Seller REST API with Python, refer to the README file in the Ad Exchange Seller REST API examples.

PHP

Here is a basic example that shows how to use the Ad Exchange Seller REST API with PHP.

  1. Setup dependencies

    Download and install google-api-php-client.

    Add this to your include path

    set_include_path('/path/to/clientlib' . PATH_SEPARATOR . get_include_path());
  2. Create a Google_Client object

    $client = new Google_Client();
  3. Set up your credentials

    All calls to the API require authentication; load up the client secrets file you downloaded and add a scope for your requests.

    $client->setAuthConfigFile('client_secrets.json');
    $client->addScope('https://www.googleapis.com/auth/adexchange.seller');
  4. Construct a client for the AdExchangeSeller

    You can then create your AdExchange Seller client using the authorized Google_Client object:

    $service = new Google_Service_AdExchangeSeller($client);

  5. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code runs a Report and displays the output.

    // Set up a report
      $startDate = 'today-6d';
      $endDate = 'today';
    
      // Set metrics, dimensions and sort order
      $optParams = array(
          'metric' => array('AD_REQUESTS', 'CLICKS'),
          'dimension' => array('DATE', 'WINNING_BID_RULE_NAME'),
          'sort' => '+DATE'
      );
    
      // Run report.
      // If this was a bigger report we would use paging, see
      // GenerateReportWithPaging.php for more information.
      $report = $service->accounts_reports->generate("myaccount", $startDate,
            $endDate, $optParams);
    
      // Display the returned data.
      if (isset($report) && isset($report['rows'])) {
        // Display headers.
        foreach($report['headers'] as $header) {
          printf('%25s', $header['name']);
        }
        print "\n";
    
        // Display results.
        foreach($report['rows'] as $row) {
          foreach($row as $column) {
            printf('%25s', $column);
          }
          print "\n";
        }
    
        // Display totals
        foreach($report['totals'] as $total) {
          printf('%25s', $total);
        }
        print "\n";
      } else {
        print "No rows returned.\n";
      }

For more detailed information about using the Ad Exchange Seller REST API with PHP, refer to the README file in the Ad Exchange Seller REST API examples.

Ruby

Here is a basic example that shows how to use the Ad Exchange Seller REST API with Ruby.

  1. Download and install the Google API Ruby Client

    Example using gem:

    $ gem install google-api-client
    $ gem update -y google-api-client

  2. Load the client secrets file

    All calls to the API require authentication; start by loading up the client secrets file you downloaded.

    client_secrets = Google::APIClient::ClientSecrets.load
  3. Set up your credentials

    Create credentials using the loaded client secrets info.

    flow = Google::APIClient::InstalledAppFlow.new(
            :client_id => client_secrets.client_id,
            :client_secret => client_secrets.client_secret,
            :scope => [API_SCOPE]
        )
        authorization = flow.authorize(file_storage)
  4. Construct a client for the AdExchangeSeller

    Create an authorized client for the AdExchangeSeller API by passing in the credentials.

    service = Google::APIClient::Service.new(
          API_NAME, API_VERSION, {
              :application_name => "Ruby #{API_NAME} samples: #{$0}",
              :application_version => '1.0.0',
              :authorization => authorization})
  5. Perform an operation

    After you've instantiated a client to connect to the API, you can perform an operation. The following code runs a Report and displays the output.

    result = service.accounts.reports.generate(
          :accountId => 'myaccount',
          :startDate => 'today-6d',
          :endDate => 'today',
          :metric => ['AD_REQUESTS', 'CLICKS'],
          :dimension => ['DATE', 'WINNING_BID_RULE_NAME'],
          :sort => ['+DATE']).execute
    
      # Display headers.
      result.data.headers.each do |header|
        print '%25s' % header['name']
      end
      puts
    
      # Display results.
      result.data.rows.each do |row|
        row.each do |column|
          print '%25s' % column
        end
        puts
      end
    
      # Display totals.
      result.data.totals.each do |total|
        print '%25s' % total
      end
      puts
    
    

For more detailed information about using the Ad Exchange Seller REST API with Python, refer to the README file in the Ad Exchange Seller REST API examples.

4. Next Steps

Read the background information to more fully understand what is happening in the samples and the options available to you in writing your solution.

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

Visit the reference documentation for the version you are working with to learn more about the API.

For more need help, read the advanced topics listed under the Guides section.

5. Survey

Help us improve this documentation; tell us what worked and what we missed by filling out a quick survey.

Send feedback about...

Seller REST API
Seller REST API