This page describes how to authenticate against the Conversational Analytics API. It also explains how to configure connections to your data in Looker, BigQuery, Data Studio, and 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.
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.
If you plan to use customer-managed encryption keys (CMEK) 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 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.
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:
- ENDPOINT_URL: The service endpoint URL that corresponds to your location, for example,
geminidataanalytics.googleapis.comfor theglobaldefault location, orgeminidataanalytics.us.rep.googleapis.comfor theusmulti-region. - PROJECT_ID: The ID of the Google Cloud project where you enabled the required APIs.
LOCATION: The location where your resources are stored (for example,
globalorus-east4).METHOD: The target endpoint method or resource path (for example,
dataAgentsto create an agent, ordataAgents/AGENT_IDto 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.
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:
- PROJECT_ID: The ID of the Google Cloud project where you enabled the required APIs.
LOCATION: The location where your resources are stored (for example,
globalorus-east4).METHOD: The target endpoint method or resource path. For example:
- To create a data agent, use the
POSTmethod and specifydataAgents. - To list existing data agents, use the
GETmethod and specifydataAgents.
- To create a data agent, use the
Python SDK
The following sample Python code demonstrates how to authenticate your Google Account for access to the Conversational Analytics API within Colaboratory:
from google.colab import auth
auth.authenticate_user()
Connect to Looker with the Conversational Analytics API
For the required roles and permissions, authorization options, and sample code for connecting to Looker Explores, see Connect to Looker. For instances that use only private connections, see Connect to a Looker (Google Cloud core) instance that uses private connections.
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.
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.
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:
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:
- PROJECT_ID, PROJECT_ID_2, PROJECT_ID_3: 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, specify
bigquery-public-data. - DATASET_ID, DATASET_ID_2, DATASET_ID_3: The ID of the BigQuery dataset (or
catalog.namespacefor Lakehouse runtime catalog tables). - TABLE_ID, TABLE_ID_2, TABLE_ID_3: 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.
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:
- PROJECT_ID, PROJECT_ID_2, PROJECT_ID_3: 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, specify
bigquery-public-data. - DATASET_ID, DATASET_ID_2, DATASET_ID_3: The ID of the BigQuery dataset (or
catalog.namespacefor Lakehouse runtime catalog tables). For example,san_francisco. - TABLE_ID, TABLE_ID_2, TABLE_ID_3: 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 by using either HTTP requests or the Python SDK.
For information about BigQuery edition requirements and graph query limitations, see Connect to BigQuery Graph.
HTTP using Python
The following sample code defines a connection to a BigQuery graph:
bigquery_data_sources = {
"bq": {
"propertyGraphReferences": [
{
"projectId": "GRAPH_PROJECT_ID",
"datasetId": "GRAPH_DATASET_ID",
"propertyGraphId": "GRAPH_ID"
}
]
}
}
Replace the sample values as follows:
- GRAPH_PROJECT_ID: The ID of the Google Cloud project that contains the BigQuery graph.
- GRAPH_DATASET_ID: The ID of the BigQuery dataset that contains the graph.
- GRAPH_ID: 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:
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:
- GRAPH_PROJECT_ID: The ID of the Google Cloud project that contains the BigQuery graph.
- GRAPH_DATASET_ID: The ID of the BigQuery dataset that contains the graph.
- GRAPH_ID: 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. 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.
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.
looker_studio_data_source = {
"studio":{
"studio_references":
[
{
"datasource_id": "DATASOURCE_ID"
}
]
}
}
Replace your_studio_datasource_id 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.
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 STUDIO-DATASOURCE-ID 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) with your database data source, to create, upload, or manage context sets, you must also enable the Knowledge Catalog API 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.
Connect to AlloyDB for PostgreSQL
The user or service account must have the alloydb.databaseUser role. For more information, see AlloyDB, IAM database authentication.
HTTP using Python
The following sample code demonstrates how to specify your AlloyDB data source details in the body of your HTTP request.
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:
- your_table_id: The comma separated list of table IDs. If not set, all tables in the database are considered.
- your_context_set_id: 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.
Python SDK
The following sample Python code demonstrates how to define your AlloyDB data source details by using the Python SDK.
# 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:
- your_table_id_1: The ID of the first table in your AlloyDB instance.
- your_table_id_2: The ID of the second table in your AlloyDB instance.
- your_context_set_id: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the AlloyDB documentation.
Connect to GoogleSQL for Spanner
The user or service account must have the spanner.databaseReader role. For more information, see Apply IAM roles.
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.
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:
- your_table_id_1: The ID of the first table in your GoogleSQL for Spanner instance.
- your_table_id_2: The ID of the second table in your GoogleSQL for Spanner instance.
- your_context_set_id: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the Spanner documentation.
Python SDK
The following sample Python code demonstrates how to define your GoogleSQL for Spanner data source details by using the Python SDK.
# 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:
- your_table_id_1: The ID of the first table in your GoogleSQL for Spanner instance.
- your_table_id_2: The ID of the second table in your GoogleSQL for Spanner instance.
- your_context_set_id: The complete ID of the context set to retrieve. For more information about how to find the context set ID, see the Spanner documentation.
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 and Manage users with IAM database authentication for Cloud SQL for PostgreSQL.
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.
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:
- your_table_id_1: The ID of the first table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- your_table_id_2: The ID of the second table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- your_context_set_id: 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 and Cloud SQL for PostgreSQL.
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.
# 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:
- your_table_id_1: The ID of the first table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- your_table_id_2: The ID of the second table in your Cloud SQL for MySQL and Cloud SQL for PostgreSQL instance.
- your_context_set_id: 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 and Cloud SQL for PostgreSQL.