This page describes how to [authenticate against the Conversational Analytics API](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#authenticate). It also explains how to configure connections to your data in [Looker](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#connect-to-looker), [BigQuery](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#connect-to-bigquery), [Data Studio](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#connect-to-data-studio), and [databases](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#connect-to-databases) (AlloyDB, GoogleSQL for Spanner, Cloud SQL for MySQL, and Cloud SQL for PostgreSQL) by using either direct HTTP requests or the SDK. The Conversational Analytics API uses standard [Google Cloud authentication methods](https://docs.cloud.google.com/docs/authentication.md.txt).

## Before you begin

Before you can authenticate against the Conversational Analytics API and configure connections to your data, you must complete the prerequisites and enable the required APIs for your Google Cloud project, as described in [Enable the Conversational Analytics API](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/enable-the-api.md.txt).

If you plan to use [customer-managed encryption keys (CMEK)](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/cmek.md.txt) to protect your Conversational Analytics API resources, you must grant the API's service account permission to use your Cloud Key Management Service key. For details, see [Before you begin](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/cmek.md.txt#before-you-begin) in the CMEK documentation.

## Authenticate against the Conversational Analytics API

This section describes how to authenticate against the Conversational Analytics API by using HTTP and Python methods to obtain the necessary authorization tokens. The following samples use regional endpoint logic based on the resource location. For more information about regional endpoints, see [Data residency](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/data-residency.md.txt).

### HTTP curl

The following sample `curl` command targets an API endpoint method to demonstrate how to send an authenticated request by using the identity token that is generated by the Google Cloud CLI:

    curl  -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
    https://ENDPOINT_URL/v1/projects/PROJECT_ID/locations/LOCATION/METHOD

Replace the sample values as follows:

- <var class="readonly" translate="no">ENDPOINT_URL</var>: The service endpoint URL that corresponds to your location, for example, `geminidataanalytics.googleapis.com` for the `global` default location, or `geminidataanalytics.us.rep.googleapis.com` for the `us` multi-region.
- <var class="readonly" translate="no">PROJECT_ID</var>: The ID of the Google Cloud project where you [enabled the required APIs](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/enable-the-api.md.txt).
- <var class="readonly" translate="no">LOCATION</var>: The location where your resources are stored (for example, `global` or `us-east4`).

  > [!IMPORTANT]
  > **Important:** The location in your request path must match the location of the endpoint that you're calling. For example, if your endpoint URL is `geminidataanalytics.us.rep.googleapis.com`, specify `us` as the `LOCATION` in your request path.

- <var class="readonly" translate="no">METHOD</var>: The target endpoint method or resource path (for example, `dataAgents` to create an agent, or `dataAgents/AGENT_ID` to get a specific agent).

### HTTP using Python

The following sample Python code targets an API endpoint method to demonstrate how to obtain an access token and send an authenticated HTTP request. The code dynamically builds the endpoint URL based on the specified location type.

```python
billing_project = 'PROJECT_ID'
location = 'LOCATION'
access_token = !gcloud auth application-default print-access-token

# Set the base URL based on location.
if not location or location == "global":
  base_url = "https://geminidataanalytics.googleapis.com"
elif "-" in location:
  # Regional endpoints
  base_url = f"https://geminidataanalytics-{location}.googleapis.com"
else:
  # Multi-regional endpoints
  base_url = f"https://geminidataanalytics.{location}.rep.googleapis.com"

url = f"{base_url}/v1/projects/{billing_project}/locations/{location}/METHOD"
headers = {"Authorization": f'Bearer {access_token[0]}'}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">PROJECT_ID</var>: The ID of the Google Cloud project where you [enabled the required APIs](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/enable-the-api.md.txt).
- <var class="readonly" translate="no">LOCATION</var>: The location where your resources are stored (for example, `global` or `us-east4`).

  > [!IMPORTANT]
  > **Important:** The location in your request path must match the location of the endpoint that you're calling. For example, if your endpoint URL is `geminidataanalytics.us.rep.googleapis.com`, specify `us` as the `LOCATION` in your request path.

- <var class="readonly" translate="no">METHOD</var>: The target endpoint method or resource path. For example:

  - To create a data agent, use the `POST` method and specify `dataAgents`.
  - To list existing data agents, use the `GET` method and specify `dataAgents`.

### Python SDK

The following sample Python code demonstrates how to authenticate your Google Account for access to the Conversational Analytics API within Colaboratory:

```python
from google.colab import auth
auth.authenticate_user()

```

## Connect to Looker with the Conversational Analytics API

To connect to Looker with the Conversational Analytics API, you must provide the following information:

- The URL of your Looker instance
- The specific [LookML model](https://docs.cloud.google.com/looker/docs/lookml-terms-and-concepts.md.txt#model) and [Looker Explore](https://docs.cloud.google.com/looker/docs/lookml-terms-and-concepts.md.txt#explore) that you want to use as a data source

Your Looker instance must also meet specific requirements:

- [Looker (original)](https://docs.cloud.google.com/looker/docs/admin-panel-platform-gil.md.txt#before_you_begin)
- [Looker (Google Cloud core)](https://docs.cloud.google.com/looker/docs/looker-core-admin-gemini.md.txt#before_you_begin)

Additionally, the authenticating user or [service account](https://docs.cloud.google.com/looker/docs/looker-core-user-management.md.txt#creating_an_api-only_service_account) must have the [required Looker permissions](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#required-looker-permissions).

### Choose the appropriate connection method

You can then choose to authorize requests using either [Looker API keys (client ID and client secret)](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-api-keys) or an [access token](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-access-token). Customers who use [Looker (Google Cloud core) with only private connections](https://docs.cloud.google.com/looker/docs/looker-core-private-ip-overview.md.txt) must connect using an [access token](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-access-token).

> [!IMPORTANT]
> **Important:** You can connect to a maximum of five Looker Explores at a time with the Conversational Analytics API.

Use the following table to choose how to authorize requests.

|---|---|---|---|---|---|
| **User type** | **Authorization method** | **For Looker (original)** | **For Looker (Google Cloud core)** | **For Looker (Google Cloud core) that uses only private connections** | **Description** |
| Embed users | [Access token](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-access-token) | [`login_user`](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/ApiAuth/login_user.md.txt) | [`login_user`](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/ApiAuth/login_user.md.txt) | [`login_user`](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/ApiAuth/login_user.md.txt) | Respects LookML row and column level permissions (for example, `access_filters`, `access_grants`, `sql_always_where`) of the target user's `access_token`. |
| Standard users | [Access token](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-access-token) | [`login_user`](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/ApiAuth/login_user.md.txt) Or [OAuth client](https://docs.cloud.google.com/looker/docs/api-cors.md.txt) | [OAuth client](https://docs.cloud.google.com/looker/docs/api-cors.md.txt) | [OAuth client](https://docs.cloud.google.com/looker/docs/api-cors.md.txt) | Respects LookML row and column level permissions (for example, `access_filters`, `access_grants`, `sql_always_where`) of the target user's `access_token`. |
| [Looker API-only service account](https://docs.cloud.google.com/looker/docs/looker-core-user-management.md.txt#creating_an_api-only_service_account) | [API key](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-api-keys) | [Client ID and secret](https://docs.cloud.google.com/looker/docs/admin-panel-users-users.md.txt#api_keys) | [Client ID and secret](https://docs.cloud.google.com/looker/docs/admin-panel-users-users.md.txt#api_keys) | N/A | All users share the same level of access to Looker. |

API keys use the permissions and access levels of the user. API keys can be useful if you are building an application where everyone shares the same level of access.

An access token lets you use LookML row and column level permissions (for example,`access_filters`, `access_grants`, `sql_always_where`) of the target user's `access_token`. An access token is useful for a multi-tenant application.

### Required Looker permissions

The user or [service account](https://docs.cloud.google.com/looker/docs/looker-core-user-management.md.txt#creating_an_api-only_service_account) whose credentials are used to authenticate must be granted a Looker role that includes the following permissions for the models that you want to query:

- [`access_data`](https://docs.cloud.google.com/looker/docs/admin-panel-users-roles.md.txt#access_data)
- [`gemini_in_looker`](https://docs.cloud.google.com/looker/docs/admin-panel-users-roles.md.txt#gemini_in_looker)

You can configure these permissions in the **Admin \> Roles** section of your Looker instance.

### Connect with Looker API keys

This section describes how to generate the API keys and configure the Conversational Analytics API to connect to Looker by using either direct HTTP requests or the SDK.

> [!IMPORTANT]
> **Important:** Customers who use the [Looker (Google Cloud core) private connections](https://docs.cloud.google.com/looker/docs/looker-core-private-ip-overview.md.txt) option cannot use this method and should authorize requests with an [access token](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#looker-access-token).

To establish a connection with a Looker instance, you need valid Looker API keys, which are created by Looker and consist of a client ID and a client secret. Looker uses these keys to authenticate users against the Looker API.

To learn more about generating new Looker API keys, see [Admin settings - Users](https://docs.cloud.google.com/looker/docs/admin-panel-users-users.md.txt#api_keys). To learn more about authentication methods and managing Looker API keys, see [Looker API authentication](https://docs.cloud.google.com/looker/docs/api-auth.md.txt).

### HTTP using Python

After you generate the API keys (client ID and secret), you can configure the Conversational Analytics API to connect to Looker by making direct HTTP requests. The following sample code demonstrates how to specify your Looker data source details and your API keys within the body of your HTTP request.

```python

looker_credentials = {
  "oauth": {
      "secret": {
        "client_id": "CLIENT_ID",
        "client_secret": "CLIENT_SECRET",
      }
  }
}

looker_data_source = {
  "looker": {
    "explore_references": {
        "looker_instance_uri": "INSTANCE_URI",
        "lookml_model": "MODEL",
        "explore": "EXPLORE",
    }
  }
}

```

Replace the sample values as follows:

- <var class="readonly" translate="no">CLIENT_ID</var>: The client ID of your generated Looker API key
- <var class="readonly" translate="no">CLIENT_SECRET</var>: The client secret of your generated Looker API key
- <var class="readonly" translate="no">INSTANCE_URI</var>: The complete URL of your Looker instance (for example, `https://mycompany.looker.com`)
- <var class="readonly" translate="no">MODEL</var>: The name of the LookML model that you want to use
- <var class="readonly" translate="no">EXPLORE</var>: The name of the Explore within the specified model that you want to use

### Python SDK

After you generate the API keys (client ID and secret), you can configure the Conversational Analytics API to connect to Looker by using Python. The following sample Python code demonstrates how to specify your Looker data source details and your API keys to the Conversational Analytics API.

> [!TIP]
> **Tip:** Store the Looker client ID (`looker_client_id`) and the Looker client secret (`looker_client_secret`) as environment variables for improved security.

```python
looker_client_id = "CLIENT_ID" # @param {type:"string"}
looker_client_secret = "CLIENT_SECRET" # @param {type:"string"}
looker_instance_uri = "INSTANCE_URI" # @param {type:"string"}
lookml_model = "MODEL" # @param {type:"string"}
explore = "EXPLORE" # @param {type:"string"}

# Looker data source
looker_explore_reference = geminidataanalytics.LookerExploreReference()
looker_explore_reference.looker_instance_uri = looker_instance_uri
looker_explore_reference.lookml_model = lookml_model
looker_explore_reference.explore = explore

credentials = geminidataanalytics.Credentials()
credentials.oauth.secret.client_id = looker_client_id
credentials.oauth.secret.client_secret = looker_client_secret

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.looker.explore_references = [looker_explore_reference]
```

Replace the sample values as follows:

- <var class="readonly" translate="no">CLIENT_ID</var>: The client ID of your generated Looker API key
- <var class="readonly" translate="no">CLIENT_SECRET</var>: The client secret of your generated Looker API key
- <var class="readonly" translate="no">INSTANCE_URI</var>: The complete URL of your Looker instance (for example, `https://mycompany.looker.com`)
- <var class="readonly" translate="no">MODEL</var>: The name of the Looker model that you want to use
- <var class="readonly" translate="no">EXPLORE</var>: The name of the Looker Explore that you want to use

### Connect with an access token

This section describes how to configure the Conversational Analytics API to connect to Looker using an access token.

> [!NOTE]
> **Note:** An administrator user must generate the `access_token` value. The user that the `access_token` is created for must have the permissions for the [`Get LookML Model Explore`](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/LookmlModel/lookml_model_explore.md.txt) and [`Run Inline Query`](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/Query/run_inline_query.md.txt) Looker endpoints. The [`develop`](https://docs.cloud.google.com/looker/docs/admin-panel-users-roles.md.txt#develop) permission is a minimum requirement.

To establish a connection with a Looker instance, you need a valid OAuth2 `access_token` value, which is created by a successful request to the `login` Looker API endpoint.

To learn more about generating an access token, see [Looker API authentication](https://docs.cloud.google.com/looker/docs/api-auth.md.txt) and how to [present client credentials to obtain an access token](https://docs.cloud.google.com/looker/docs/reference/looker-api/latest/methods/ApiAuth/login.md.txt#present-client-credentials-to-obtain-an-authorization-token).
<button value="public" default="">Using a public IP</button> <button value="private">Using a private connection</button>

<br />

### HTTP using Python

The following sample code demonstrates how to specify your Looker data source details and your access token within the body of your HTTP request.

We suggest storing the Looker access token (`access_token`) as an environment variable for improved security.

```python
looker_credentials = {
  "oauth": {
    "token": {
      "access_token": "ACCESS_TOKEN",
    }
  }
}

looker_data_source = {
  "looker": {
    "explore_references": {
        "looker_instance_uri": "INSTANCE_URI",
        "lookml_model": "MODEL",
        "explore": "EXPLORE",
    }
  }
}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">ACCESS_TOKEN</var>: The `access_token` value that you generate to authorize requests to Looker
- <var class="readonly" translate="no">INSTANCE_URI</var>: The complete URL of your Looker instance (for example, `https://mycompany.looker.com`)
- <var class="readonly" translate="no">MODEL</var>: The name of the LookML model that you want to use
- <var class="readonly" translate="no">EXPLORE</var>: The name of the Explore within the specified model that you want to use

### Python SDK

The following sample Python code demonstrates how to define your Looker data source details and your access token to authorize requests using the Python SDK.

We suggest storing the Looker access token (`access_token`) as an environment variable for improved security.

```python
looker_access_token = "ACCESS_TOKEN"
looker_instance_uri = "INSTANCE_URI"
lookml_model = "MODEL"
explore = "EXPLORE"

# Looker data source
looker_explore_reference = geminidataanalytics.LookerExploreReference()
looker_explore_reference.looker_instance_uri = looker_instance_uri
looker_explore_reference.lookml_model = lookml_model
looker_explore_reference.explore = explore

credentials = geminidataanalytics.Credentials()
credentials.oauth.token.access_token = looker_access_token

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.looker.explore_references = [looker_explore_reference]
```

Replace the sample values as follows:

- <var class="readonly" translate="no">ACCESS_TOKEN</var>: The `access_token` value you use to authorize requests to Looker
- <var class="readonly" translate="no">INSTANCE_URI</var>: The complete URL of your Looker instance (for example, `https://mycompany.looker.com`)
- <var class="readonly" translate="no">MODEL</var>: The name of the Looker model that you want to use
- <var class="readonly" translate="no">EXPLORE</var>: The name of the Looker Explore that you want to use

## Connect to BigQuery with the Conversational Analytics API

This section describes how to configure the Conversational Analytics API to connect to BigQuery by using either direct HTTP requests or an SDK. The Conversational Analytics API supports connections to BigQuery tables, and to graphs with [BigQuery Graph](https://docs.cloud.google.com/bigquery/docs/graph-overview.md.txt).

With the Conversational Analytics API, there are no hard limits on the number of
BigQuery tables that you can connect to. However, connecting to a
large number of tables can reduce accuracy or cause you to exceed Gemini's input
token limit. Queries that require complex joins across multiple tables might
also result in less accurate responses.

### Connect to tables

To connect to one or more BigQuery tables, provide the project ID,
the dataset ID, and the table ID for each table.

> [!NOTE]
> **Note:** To query Lakehouse tables that connect to the Apache Iceberg REST catalog or that are federated to an external catalog, use the four part [P.C.N.T
> syntax](https://docs.cloud.google.com/biglake/docs/understand-catalog-types#pcnt). If you are using Lakehouse tables, the dataset ID should be `catalog.namespace` (C.N).

The following examples show how to connect to one or more BigQuery tables by using either HTTP requests or the Python SDK.

### HTTP using Python

The following sample code defines a connection to multiple BigQuery tables:

```python
bigquery_data_sources = {
    "bq": {
      "tableReferences": [
        {
          "projectId": "PROJECT_ID",
          "datasetId": "DATASET_ID",
          "tableId": "TABLE_ID"
        },
        {
          "projectId": "PROJECT_ID_2",
          "datasetId": "DATASET_ID_2",
          "tableId": "TABLE_ID_2"
        },
        {
          "projectId": "PROJECT_ID_3",
          "datasetId": "DATASET_ID_3",
          "tableId": "TABLE_ID_3"
        },
      ]
    }
}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">PROJECT_ID</var>, <var class="readonly" translate="no">PROJECT_ID_2</var>, <var class="readonly" translate="no">PROJECT_ID_3</var>: The ID of the Google Cloud project that contains the BigQuery dataset and table that you want to connect to. To connect to a [public dataset](https://docs.cloud.google.com/bigquery/public-data.md.txt), specify `bigquery-public-data`.
- <var class="readonly" translate="no">DATASET_ID</var>, <var class="readonly" translate="no">DATASET_ID_2</var>, <var class="readonly" translate="no">DATASET_ID_3</var>: The ID of the BigQuery dataset (or `catalog.namespace` for Lakehouse runtime catalog tables).
- <var class="readonly" translate="no">TABLE_ID</var>, <var class="readonly" translate="no">TABLE_ID_2</var>, <var class="readonly" translate="no">TABLE_ID_3</var>: The ID of the BigQuery table (or table name for Lakehouse runtime catalog tables).

### Python SDK

You can use the `auth` SDK from Colaboratory to authenticate using your Google Account credentials and authorize the Colab client to access BigQuery using your credentials.

The following sample Python code defines a connection to multiple BigQuery tables and demonstrates how to authenticate using your Google Account credentials within Colaboratory to authorize requests.

```python
from google.colab import auth
auth.authenticate_user()

# BigQuery data source
bigquery_table_reference = geminidataanalytics.BigQueryTableReference()
bigquery_table_reference.project_id = "PROJECT_ID"
bigquery_table_reference.dataset_id = "DATASET_ID"
bigquery_table_reference.table_id = "TABLE_ID"

bigquery_table_reference_2 = geminidataanalytics.BigQueryTableReference()
bigquery_table_reference_2.project_id = "PROJECT_ID_2"
bigquery_table_reference_2.dataset_id = "DATASET_ID_2"
bigquery_table_reference_2.table_id = "TABLE_ID_2"

bigquery_table_reference_3 = geminidataanalytics.BigQueryTableReference()
bigquery_table_reference_3.project_id = "PROJECT_ID_3"
bigquery_table_reference_3.dataset_id = "DATASET_ID_3"
bigquery_table_reference_3.table_id = "TABLE_ID_3"

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.bq.table_references = [bigquery_table_reference, bigquery_table_reference_2, bigquery_table_reference_3]
```

Replace the sample values as follows:

- <var class="readonly" translate="no">PROJECT_ID</var>, <var class="readonly" translate="no">PROJECT_ID_2</var>, <var class="readonly" translate="no">PROJECT_ID_3</var>: The ID of the Google Cloud project that contains the BigQuery dataset and table that you want to connect to. To connect to a [public dataset](https://docs.cloud.google.com/bigquery/public-data.md.txt), specify `bigquery-public-data`.
- <var class="readonly" translate="no">DATASET_ID</var>, <var class="readonly" translate="no">DATASET_ID_2</var>, <var class="readonly" translate="no">DATASET_ID_3</var>: The ID of the BigQuery dataset (or `catalog.namespace` for Lakehouse runtime catalog tables). For example, `san_francisco`.
- <var class="readonly" translate="no">TABLE_ID</var>, <var class="readonly" translate="no">TABLE_ID_2</var>, <var class="readonly" translate="no">TABLE_ID_3</var>: The ID of the BigQuery table (or table name for Lakehouse runtime catalog tables). For example, `street_trees`.

### Connect to BigQuery Graph

The following examples show how to connect to one or more graphs with [BigQuery Graph](https://docs.cloud.google.com/bigquery/docs/graph-overview.md.txt) by using either HTTP requests or the Python SDK.

For information about BigQuery edition requirements and graph query limitations, see [Connect to BigQuery Graph](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/connect-bigquery.md.txt#connect-to-bq-graph).

### HTTP using Python

The following sample code defines a connection to a BigQuery graph:

```python
bigquery_data_sources = {
    "bq": {
      "propertyGraphReferences": [
        {
          "projectId": "GRAPH_PROJECT_ID",
          "datasetId": "GRAPH_DATASET_ID",
          "propertyGraphId": "GRAPH_ID"
        }
      ]
    }
}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">GRAPH_PROJECT_ID</var>: The ID of the Google Cloud project that contains the BigQuery graph.
- <var class="readonly" translate="no">GRAPH_DATASET_ID</var>: The ID of the BigQuery dataset that contains the graph.
- <var class="readonly" translate="no">GRAPH_ID</var>: The ID of the BigQuery graph.

### Python SDK

You can use the `auth` SDK from Colaboratory to authenticate to BigQuery by using the credentials of your user that is authenticated to Colaboratory.

The following sample Python code defines a connection to a BigQuery graph:

```python
from google.colab import auth
auth.authenticate_user()

# BigQuery graph reference
graph_reference = geminidataanalytics.BigQueryPropertyGraphReference()
graph_reference.project_id = "GRAPH_PROJECT_ID"
graph_reference.dataset_id = "GRAPH_DATASET_ID"
graph_reference.property_graph_id = "GRAPH_ID"

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.bq.property_graph_references = [graph_reference]
```

Replace the sample values as follows:

- <var class="readonly" translate="no">GRAPH_PROJECT_ID</var>: The ID of the Google Cloud project that contains the BigQuery graph.
- <var class="readonly" translate="no">GRAPH_DATASET_ID</var>: The ID of the BigQuery dataset that contains the graph.
- <var class="readonly" translate="no">GRAPH_ID</var>: The ID of the BigQuery graph.

## Connect to Data Studio with the Conversational Analytics API

To connect to Data Studio with the Conversational Analytics API, you must first [enable the Data Studio API](https://docs.cloud.google.com/gemini/data-agents/conversational-analytics-api/authentication.md.txt#data-studio-api). This section describes how to configure the Conversational Analytics API to connect to Data Studio by using either direct HTTP requests or an SDK.

To use the Conversational Analytics API with Data Studio Pro, your Pro subscription must be outside of a VPC-SC perimeter.

### Enable Data Studio API

To enable the Data Studio API, follow the instructions in [Enable the API](https://developers.google.com/data-studio/integrate/api#enable_the_api).

### Authenticate to Data Studio

To connect to Data Studio with the Conversational Analytics API, you must ensure the authenticated user or service account is authorized to access the Data Studio data source, and then provide the data source ID.

### HTTP using Python

After you enable the Data Studio API, you can connect to Data Studio by making HTTP curl requests with Python. The following sample code demonstrates how to specify your Data Studio data source details within the body of your HTTP request.

You can connect to Data Studio by making direct HTTP requests. A sample HTTP call is shown in the following code block.

```json
looker_studio_data_source = {
    "studio":{
        "studio_references":
        [
            {
              "datasource_id": "DATASOURCE_ID"
            }
        ]
    }
}

```

Replace <var class="readonly" translate="no">your_studio_datasource_id</var> with the actual data source ID of the Data Studio data source that you want to use.

### Python SDK

After you enable the Data Studio API, you can connect to Data Studio by using an SDK. The following sample Python code demonstrates how to specify your Data Studio data source details.

```python

datasource_id = "DATASOURCE_ID"

# Looker Studio
studio_references = geminidataanalytics.StudioDatasourceReference()
studio_references.datasource_id = studio_datasource_id

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.studio.studio_references = [studio_references]

```

Replace <var class="readonly" translate="no">STUDIO-DATASOURCE-ID</var> with the actual data source ID of the Data Studio data source that you want to use.

## Connect to databases with the Conversational Analytics API

To connect to databases with the Conversational Analytics API, you must have the necessary IAM permissions for your database instance. Optionally, if you use context sets ([Preview](https://docs.cloud.google.com/products#product-launch-stages)) with your database data source, to create, upload, or manage context sets, you must also enable the [Knowledge Catalog API](https://console.cloud.google.com/apis/library/dataplex.googleapis.com) and grant the Dataplex Catalog Editor (`roles/dataplex.catalogEditor`) or Dataplex Catalog Admin (`roles/dataplex.catalogAdmin`) role on the project. For more information, see [Context sets permissions](https://docs.cloud.google.com/knowledge-catalog/docs/iam-permissions.md.txt#context-set-permissions).

### Connect to AlloyDB for PostgreSQL

The user or service account must have the `alloydb.databaseUser` role. For more information, see [AlloyDB, IAM database authentication](https://docs.cloud.google.com/alloydb/docs/database-users/iam-authentication.md.txt).

### HTTP using Python

The following sample code demonstrates how to specify your
AlloyDB data source details in the body of
your HTTP request.

```python
alloydb_data_sources = {
  "alloydb": {
      "database_reference": {
          "project_id": alloydb_project_id,
          "region": location,
          "cluster_id": alloydb_cluster_id,
          "instance_id": alloydb_instance_id,
          "database_id": alloydb_database_id,
          "table_ids":["table1", "table2", "table3"]
      },
      # Optional: Include this if you have pre-authored context for the agent
      # "agent_context_reference": {
      #     "context_set_id": f"projects/alloydb_project_id/locations/{location}/contextSets/your_context_set_id"
      # }
  }
}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">your_table_id</var>: The comma separated list of table IDs. If not set, all tables in the database are considered.
- <var class="readonly" translate="no">your_context_set_id</var>: Optional: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the [AlloyDB documentation](https://docs.cloud.google.com/alloydb/docs/ai/inspect-data-agent#find-agent-context-id).

### Python SDK

The following sample Python code demonstrates how to define your AlloyDB data source details by using the Python SDK.

```python
# AlloyDB data source
alloydb_table_ids = ["your_table_id_1", "your_table_id_2"]
alloydb_context_set_id = "your_context_set_id"

alloydb_reference = geminidataanalytics.AlloyDbReference()
alloydb_reference.database_reference.table_id = alloydb_table_ids
alloydb_reference_database_reference.project_id = billing_project
alloydb_reference_database_reference.region = "us-central1" # Example region
alloydb_reference_database_reference.cluster_id = alloydb_cluster_id
alloydb_reference_database_reference.instance_id = alloydb_instance_id
alloydb_reference_database_reference.database_id = alloydb_database_id
# optional set agent context reference
# alloydb_ref_1.agent_context_reference.context_set_id = f"projects/{billing_project}/locations/{location}/contextSets/your_context_set_id"

alloydb_reference.agent_context_reference.context_set_id = alloydb_context_set_id

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.alloydb.alloydb_reference = alloydb_reference
```

Replace the sample values as follows:

- <var class="readonly" translate="no">your_table_id_1</var>: The ID of the first table in your AlloyDB instance.
- <var class="readonly" translate="no">your_table_id_2</var>: The ID of the second table in your AlloyDB instance.
- <var class="readonly" translate="no">your_context_set_id</var>: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the [AlloyDB documentation](https://docs.cloud.google.com/alloydb/docs/ai/inspect-data-agent#find-agent-context-id).

### Connect to GoogleSQL for Spanner

The user or service account must have the `spanner.databaseReader` role. For more information, see [Apply IAM roles](https://docs.cloud.google.com/spanner/docs/grant-permissions.md.txt).

### HTTP using Python

The following sample code demonstrates how to specify your GoogleSQL for Spanner data source details within the body of your HTTP request.

```python
spanner_data_sources = {
    "spanner_reference": {
        "database_reference": {
            "project_id": spanner_project_id,
            "region": location,
            "engine": engine,
            "instance_id": spanner_instance_id,
            "database_id": spanner_database_id,
        },
        # Optional: Include this if you have pre-authored context for the agent
        # "agent_context_reference": {
        #     "context_set_id": f"projects/{billing_project}/locations/{location}/contextSets/your_context_set_id"
        # }
    }
}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">your_table_id_1</var>: The ID of the first table in your GoogleSQL for Spanner instance.
- <var class="readonly" translate="no">your_table_id_2</var>: The ID of the second table in your GoogleSQL for Spanner instance.
- <var class="readonly" translate="no">your_context_set_id</var>: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the [Spanner documentation](https://docs.cloud.google.com/spanner/docs/inspect-data-agent#find-agent-context-id).

### Python SDK

The following sample Python code demonstrates how to define your GoogleSQL for Spanner data source details by using the Python SDK.

```python
# Spanner data source
spanner_table_ids = ["your_table_id_1", "your_table_id_2"]
spanner_context_set_id = "your_context_set_id"

spanner_reference = geminidataanalytics.SpannerReference()
spanner_reference.database_reference.table_id = spanner_table_ids
spanner_reference.agent_context_reference.context_set_id = spanner_context_set_id

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.spanner.spanner_reference = spanner_reference
```

Replace the sample values as follows:

- <var class="readonly" translate="no">your_table_id_1</var>: The ID of the first table in your GoogleSQL for Spanner instance.
- <var class="readonly" translate="no">your_table_id_2</var>: The ID of the second table in your GoogleSQL for Spanner instance.
- <var class="readonly" translate="no">your_context_set_id</var>: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the [Spanner documentation](https://docs.cloud.google.com/spanner/docs/inspect-data-agent#find-agent-context-id).

### Connect to Cloud SQL for MySQL and Cloud SQL for PostgreSQL

The following sections provide instructions for connecting to both Cloud SQL for MySQL and Cloud SQL for PostgreSQL. The user or service account must have the `cloudsql.studioUser` and `cloudsql.instanceUser` roles. For more information, see [Manage users with IAM database authentication for Cloud SQL for MySQL](https://docs.cloud.google.com/sql/docs/mysql/add-manage-iam-users.md.txt) and [Manage users with IAM database authentication for Cloud SQL for PostgreSQL](https://docs.cloud.google.com/sql/docs/postgres/add-manage-iam-users.md.txt#gcloud).

### HTTP using Python

The following sample code demonstrates how to specify your Cloud SQL for MySQL and
Cloud SQL for PostgreSQL data source details within the body of your HTTP
request.

```python
cloudsql_data_source = {
    "cloudSqlReference": {
        "databaseReference": {
            "engine": MYSQL,
            "project_id": cloud-db-nl2sql,
            "region": us-central1,
            "instance_id": nl2sql-birdsql-mysql,
            "database_id": financial
        },
        # Optional: Include this if you have pre-authored context for the agent
        # "agent_context_reference": {
        #     "context_set_id": f"projects/cloud-db-nl2sql/locations/us-central1/contextSets/bdf_pg_all_templates\"
        # }
    }
}
```

Replace the sample values as follows:

- <var class="readonly" translate="no">your_table_id_1</var>: The ID of the first table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- <var class="readonly" translate="no">your_table_id_2</var>: The ID of the second table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- <var class="readonly" translate="no">your_context_set_id</var>: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the documentation for [Cloud SQL for MySQL](https://docs.cloud.google.com/sql/docs/mysql/inspect-data-agent#find-agent-context-id) and [Cloud SQL for PostgreSQL](https://docs.cloud.google.com/sql/docs/postgres/inspect-data-agent#find-agent-context-id).

### Python SDK

The following sample Python code demonstrates how to define your Cloud SQL for MySQL and Cloud SQL for PostgreSQL data source details by using the Python SDK.

```python
# Cloud SQL data source
cloudsql_table_ids = ["your_table_id_1", "your_table_id_2"]
cloudsql_context_set_id = "your_context_set_id"

cloudsql_reference = geminidataanalytics.CloudSqlReference()
cloudsql_reference.database_reference.table_id = cloudsql_table_ids
cloudsql_reference.agent_context_reference.context_set_id = cloudsql_context_set_id

# Connect to your data source
datasource_references = geminidataanalytics.DatasourceReferences()
datasource_references.cloudsql.cloudsql_reference = cloudsql_reference
```

Replace the sample values as follows:

- <var class="readonly" translate="no">your_table_id_1</var>: The ID of the first table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- <var class="readonly" translate="no">your_table_id_2</var>: The ID of the second table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- <var class="readonly" translate="no">your_context_set_id</var>: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the documentation for [Cloud SQL for MySQL](https://docs.cloud.google.com/sql/docs/mysql/inspect-data-agent#find-agent-context-id) and [Cloud SQL for PostgreSQL](https://docs.cloud.google.com/sql/docs/postgres/inspect-data-agent#find-agent-context-id).