BigQuery Python API Docs | dltHub
Build a BigQuery-to-database pipeline in Python using dlt with AI Workbench support for Claude Code, Cursor, and Codex.
Last updated:
BigQuery is a serverless, highly scalable data warehouse service for storing, querying and managing large datasets. The REST API base URL is https://bigquery.googleapis.com and All requests require an OAuth 2.0 Bearer token..
dlt is an open-source Python library that handles authentication, pagination, and schema evolution automatically. dlthub provides AI context files that enable code assistants to generate production-ready pipelines. Install with uv pip install "dlt[workspace]" and start loading BigQuery data in under 10 minutes.
What data can I load from BigQuery?
Here are some of the endpoints you can load from BigQuery:
| Resource | Endpoint | Method | Data selector | Description |
|---|---|---|---|---|
| projects | /bigquery/v2/projects | GET | projects | Lists projects the caller has access to. |
| datasets | /bigquery/v2/projects/{projectId}/datasets | GET | datasets | Lists datasets in a project. |
| tables | /bigquery/v2/projects/{projectId}/datasets/{datasetId}/tables | GET | tables | Lists tables in a dataset. |
| tabledata | /bigquery/v2/projects/{projectId}/datasets/{datasetId}/tables/{tableId}/data | GET | rows | Lists rows of a table (paginated). |
| jobs | /bigquery/v2/projects/{projectId}/jobs | GET | jobs | Lists jobs in a project. |
| jobs_get | /bigquery/v2/projects/{projectId}/jobs/{jobId} | GET | Gets metadata for a specific job. | |
| queries_getQueryResults | /bigquery/v2/projects/{projectId}/queries/{jobId} | GET | rows | Returns query results with pagination. |
| models | /bigquery/v2/projects/{projectId}/datasets/{datasetId}/models | GET | models | Lists models in a dataset. |
| routines | /bigquery/v2/projects/{projectId}/datasets/{datasetId}/routines | GET | routines | Lists routines in a dataset. |
How do I authenticate with the BigQuery API?
BigQuery REST API uses Google Cloud OAuth 2.0 credentials. Calls require an OAuth access token sent in the Authorization header as Bearer <ACCESS_TOKEN>.
1. Get your credentials
- In Google Cloud Console, go to IAM & Admin > Service accounts.
- Create a service account (or select an existing one) and grant it BigQuery roles (e.g., BigQuery Data Viewer, BigQuery Job User).
- Create and download a JSON key for the service account.
- Use the JSON key to obtain an OAuth access token (gcloud auth activate-service-account --key-file=KEY.json or via application default credentials) or configure your client to use the service account JSON.
- Provide the token in requests as Authorization: Bearer <ACCESS_TOKEN> or let Google client libraries manage auth.
2. Add them to .dlt/secrets.toml
[sources.bigquery_data_management_source] service_account_key = "<PATH_TO_SERVICE_ACCOUNT_JSON>"
dlt reads this automatically at runtime — never hardcode tokens in your pipeline script. For production environments, see setting up credentials with dlt for environment variable and vault-based options.
How do I set up and run the pipeline?
Set up a virtual environment and install dlt:
uv venv && source .venv/bin/activate uv pip install "dlt[workspace]"
1. Install the dlt AI Workbench:
dlt ai init --agent <your-agent> # <agent>: claude | cursor | codex
This installs project rules, a secrets management skill, appropriate ignore files, and configures the dlt MCP server for your agent. Learn more →
2. Install the rest-api-pipeline toolkit:
dlt ai toolkit rest-api-pipeline install
This loads the skills and context about dlt the agent uses to build the pipeline iteratively, efficiently, and safely. The agent uses MCP tools to inspect credentials — it never needs to read your secrets.toml directly. Learn more →
3. Start LLM-assisted coding:
Use /find-source to load data from the BigQuery API into DuckDB.
The rest-api-pipeline toolkit takes over from here — it reads relevant API documentation, presents you with options for which endpoints to load, and follows a structured workflow to scaffold, debug, and validate the pipeline step by step.
4. Run the pipeline:
python bigquery_data_management_pipeline.py
If everything is configured correctly, you'll see output like this:
Pipeline bigquery_data_management_pipeline load step completed in 0.26 seconds 1 load package(s) were loaded to destination duckdb and into dataset bigquery_data_management_data The duckdb destination used duckdb:/bigquery_data_management.duckdb location to store data Load package 1749667187.541553 is LOADED and contains no failed jobs
Inspect your pipeline and data:
dlt pipeline bigquery_data_management_pipeline show
This opens the Pipeline Dashboard where you can verify pipeline state, load metrics, schema (tables, columns, types), and query the loaded data directly.
Python pipeline example
This example loads tables and tabledata from the BigQuery API into DuckDB. It mirrors the endpoint and data selector configuration from the table above:
import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def bigquery_data_management_source(service_account_key=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "https://bigquery.googleapis.com", "auth": { "type": "bearer", "service_account_key": service_account_key, }, }, "resources": [ {"name": "tables", "endpoint": {"path": "projects/{projectId}/datasets/{datasetId}/tables", "data_selector": "tables"}}, {"name": "tabledata", "endpoint": {"path": "projects/{projectId}/datasets/{datasetId}/tables/{tableId}/data", "data_selector": "rows"}} ], } yield from rest_api_resources(config) def get_data() -> None: pipeline = dlt.pipeline( pipeline_name="bigquery_data_management_pipeline", destination="duckdb", dataset_name="bigquery_data_management_data", ) load_info = pipeline.run(bigquery_data_management_source()) print(load_info)
To add more endpoints, append entries from the resource table to the "resources" list using the same name, path, and data_selector pattern.
How do I query the loaded data?
Once the pipeline runs, dlt creates one table per resource. You can query with Python or SQL.
Python (pandas DataFrame):
import dlt data = dlt.pipeline("bigquery_data_management_pipeline").dataset() sessions_df = data.tabledata.df() print(sessions_df.head())
SQL (DuckDB example):
SELECT * FROM bigquery_data_management_data.tabledata LIMIT 10;
In a marimo or Jupyter notebook:
import dlt data = dlt.pipeline("bigquery_data_management_pipeline").dataset() data.tabledata.df().head()
See how to explore your data in marimo Notebooks and how to query your data in Python with dataset.
What destinations can I load BigQuery data to?
dlt supports loading into any of these destinations — only the destination parameter changes:
| Destination | Example value |
|---|---|
| DuckDB (local, default) | "duckdb" |
| PostgreSQL | "postgres" |
| BigQuery | "bigquery" |
| Snowflake | "snowflake" |
| Redshift | "redshift" |
| Databricks | "databricks" |
| Filesystem (S3, GCS, Azure) | "filesystem" |
Change the destination in dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. See the full destinations list.
Troubleshooting
Authentication failures
If Authorization: Bearer is missing, expired or not scoped for BigQuery you'll receive 401 Unauthorized or 403 Forbidden. Ensure token is valid, not expired, and includes one of the required scopes (https://www.googleapis.com/auth/bigquery or https://www.googleapis.com/auth/cloud-platform). For service accounts, verify the JSON key and that the service account has the appropriate IAM role.
Quotas and rate limits
BigQuery enforces per-project quotas for API calls and operations. Exceeding quotas returns 403 with a rate‑limit or quota exceeded message. Check Cloud Console Quotas page and use exponential backoff on 429/403 quota errors.
Pagination
List endpoints return nextPageToken or pageToken (tabledata.list uses pageToken; datasets.list uses nextPageToken). Use the returned token in the pageToken query parameter to retrieve subsequent pages.
Table data format and large field handling
tabledata.list returns rows[] where each row is a Struct‑style representation; responses include totalRows as a string. Use selectedFields or startIndex/maxResults for paging. Large tables may require pagination and may be slow—consider exporting or using jobs.query for large result sets.
Ensure that the API key is valid to avoid 401 Unauthorized errors. Also, verify endpoint paths and parameters to avoid 404 Not Found errors.
Next steps
Continue your data engineering journey with the other toolkits of the dltHub AI Workbench:
data-exploration— Build custom notebooks, charts, and dashboards for deeper analysis with marimo notebooks.dlthub-runtime— Deploy, schedule, and monitor your pipeline in production.
dlt ai toolkit data-exploration install dlt ai toolkit dlthub-runtime install
Was this page helpful?
Community Hub
Need more dlt context for BigQuery?
Request dlt skills, commands, AGENT.md files, and AI-native context.