This document explains how to get started writing apps that use the
Google Bid Manager API. The API lets you manage
[Queries](https://developers.google.com/bid-manager/reference/rest/v2/queries) and retrieve
[Report](https://developers.google.com/bid-manager/reference/rest/v2/queries.reports) metadata.

Bid Manager API v2 is the latest available and recommended
version.

## 1. Before you start

If you're unfamiliar with Google Display \& Video 360 concepts, read the
[Display \& Video 360 Help Center](https://support.google.com/displayvideo/) and
experiment with the [UI](https://displayvideo.google.com/).

## 2. Prepare for authentication

To get started using Bid Manager API, you need to first
[use
the setup tool](https://console.cloud.google.com/start/api?id=doubleclickbidmanager&credential=client_key), which guides you through creating a project in the
Google API Console and enabling the API.
If you haven't done so already, create your OAuth 2.0 credentials by clicking **Create credentials \> OAuth client ID** . After you've created the credentials, you can see your client ID on the **Credentials** page. Click the client ID for details, such as client secret, redirect URIs, JavaScript origins address, and email address.

For more information, see
[Authorize Requests](https://developers.google.com/bid-manager/how-tos/authorizing).

## 3. Call the Bid Manager API

The tabs below provide quickstarts for coding in various languages. Similar
sample code can also be found in the
[Bid Manager API Examples](https://github.com/googleads/googleads-bidmanager-examples)
repo.

### Java

1. **Import the necessary libraries.**

   ```java
   import static java.nio.charset.StandardCharsets.UTF_8;
   import com.google.api.client.auth.oauth2.Credential;
   import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
   import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
   import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
   import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
   import com.google.api.client.googleapis.util.Utils;
   import com.google.api.services.doubleclickbidmanager.DoubleClickBidManager;
   import com.google.api.services.doubleclickbidmanager.model.ListQueriesResponse;
   import com.google.api.services.doubleclickbidmanager.model.Query;
   import java.io.Reader;
   import java.nio.file.Files;
   import java.nio.file.Paths;
   ```
2. **Load the client secrets file and generate authorization credentials.**


   The first time you perform this step, you are asked to accept an authorization
   prompt in your browser. Before accepting, make sure you're signed in with a
   Google Account that has access to Display \& Video 360. Your app will be authorized
   to access data on behalf of whichever account is currently signed in.

   ```java
   // Read client secrets file.
   GoogleClientSecrets clientSecrets;
   try (Reader reader = Files.newBufferedReader(Paths.get(path-to-client-secrets-file), UTF_8)) {
     clientSecrets = GoogleClientSecrets.load(Utils.getDefaultJsonFactory(), reader);
   }

   // Generate authorization credentials.
   // Set up the authorization code flow.
   GoogleAuthorizationCodeFlow flow =
       new GoogleAuthorizationCodeFlow.Builder(
           Utils.getDefaultTransport(),
           Utils.getDefaultJsonFactory(),
           clientSecrets,
           oauth-scopes)
       .build();

   Credential credential =
       new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
   ```
3. **Create an authorized API client.**

   ```java
   // Create authorized API client.
   DoubleClickBidManager service =
       new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
           .setApplicationName("bidmanager-java-installed-app-sample")
           .build();
   ```
4. **Perform an operation.**

   ```java
   // Perform an operation.
   // Call the API, getting a list of 10 queries.
   ListQueriesResponse queriesResponse = service.queries().list().setPageSize(10).execute();

   // Print them out.
   System.out.println("Id\t\tName");
   if (queriesResponse.getQueries().size() > 0) {
     for (int i = 0; i < queriesResponse.getQueries().size(); i++) {
       Query currentQuery = queriesResponse.getQueries().get(i);
       System.out.printf(
           "%s\t%s%n",
           currentQuery.getQueryId(),
           currentQuery.getMetadata().getTitle());
     }
   } else {
     System.out.println("No queries exist.");
   }
   ```

For more detailed information about using the Bid Manager API with Java,
refer to the
[README](https://github.com/googleads/googleads-bidmanager-examples/blob/HEAD/java/README.md)
file in the
[Bid Manager API examples](https://github.com/googleads/googleads-bidmanager-examples).

### Python

1. **Import the necessary libraries.**

   ```python
   from google_auth_oauthlib.flow import InstalledAppFlow
   from googleapiclient import discovery
   ```
2. **Load the client secrets file and generate authorization credentials.**


   The first time you perform this step, you are asked to accept an authorization
   prompt in your browser. Before accepting, make sure you're signed in with a
   Google Account that has access to Display \& Video 360. Your app will be authorized
   to access data on behalf of whichever account is currently signed in.

   ```python
   # Set up a flow object to create the credentials using the
   # client secrets file and OAuth scopes.
   credentials = InstalledAppFlow.from_client_secrets_file(
       path-to-client-secrets-file, oauth-scopes).run_local_server()
   ```
3. **Create an authorized API client.**

   ```python
   # Build the discovery document URL.
   discovery_url = f'https://doubleclickbidmanager.googleapis.com/$discovery/rest?version=v2'

   # Build the API service.
   service = discovery.build(
       'doubleclickbidmanager',
       'v2',
       discoveryServiceUrl=discovery_url,
       credentials=credentials)
   ```
4. **Perform an operation.**

   ```python
   # Build and execute queries.listqueries request.
   response = service.queries().list(pageSize='10').execute()

   # Print queries out.
   if 'queries' in response:
     print('Id\t\tName')
     for query in response['queries']:
       print('%s\t%s' % (query['queryId'], query['metadata']['title']))
   else:
     print('No queries exist.')
   ```

For more detailed information about using the Bid Manager API with Python,
refer to the
[README](https://github.com/googleads/googleads-bidmanager-examples/blob/HEAD/python/README.md)
file in the
[Bid Manager API examples](https://github.com/googleads/googleads-bidmanager-examples).

### PHP


This sample assumes that you are running the PHP with a built-in web server and
have configured your credentials to redirect to the relevant web page. For
example, this code, in an `index.php` file, can be run using the following
command and credentials configured to redirect to `http://localhost:8000` after
authentication:

```php
php -S localhost:8000 -t ./
```

<br />

1. **Download and install the Google API PHP Client.**

   The preferred method is via [Composer](https://getcomposer.org/):

   ```php
   composer require google/apiclient:^2.12.1
   ```

   <br />

   Once installed, be sure to include the autoloader

   ```php
   require_once '/path/to/your-project/vendor/autoload.php';
   ```

   <br />

2. **Create a Google_Client object.**

   ```php
   $client = new Google_Client();
   ```
3. **Set up the client, redirect to authentication URL if needed, and retrieve an access token.**


   The first time you perform this step, you are asked to accept an authorization
   prompt in your browser. Before accepting, make sure you're signed in with a
   Google Account that has access to Display \& Video 360. Your app will be authorized
   to access data on behalf of whichever account is currently signed in.

   ```php
   // Set up the client.
   $client->setApplicationName('DBM API PHP Samples');
   $client->addScope(oauth-scope);
   $client->setAccessType('offline');
   $client->setAuthConfigFile(path-to-client-secrets-file);

   // If the code is passed, authenticate. If not, redirect to authentication page.
   if (isset($_GET['code'])) {
     $client->authenticate($_GET['code']);
   } else {
     $authUrl = $client->createAuthUrl();
     header('Location: ' . $authUrl);
   }

   // Exchange authorization code for an access token.
   $accessToken = $client->getAccessToken();
   $client->setAccessToken($accessToken);
   ```
4. **Construct a client for the Display \& Video 360 API service.**

   ```php
   $service = new Google_Service_DoubleClickBidManager($client);
   ```
5. **Perform an operation.**

   ```php
   // Configure params for the Queries.listqueries request.
   $optParams = array('pageSize' => 10);

   // Execute the request.
   $result = $service->queries->listQueries($optParams);

   // Print the retrieved queries.
   if (!empty($result->getQueries())) {
     print('<pre><p>Id Name</p>');
     foreach ($result->getQueries() as $query) {
       printf('<p>%s %s</p>', $query->queryId, $query->metadata->title);
     }
     print('</pre>');
   } else {
     print '<p>No queries exist.</p>';
   }
   ```

For more detailed information about using the Bid Manager API with PHP,
refer to the
[README](https://github.com/googleads/googleads-bidmanager-examples/blob/HEAD/php/README.md)
file in the
[Bid Manager API examples](https://github.com/googleads/googleads-bidmanager-examples).

## 4. Next steps

Now that you have a client library up and running, explore the reference
documentation and begin building out your implementation.

Additional guides are also available on
[utilizing scheduled reports](https://developers.google.com/bid-manager/guides/scheduled-reports/overview)
and
[following reporting best practices](https://developers.google.com/bid-manager/guides/general/best-practices).