# Connect BigQuery to Contextflo

Create a read-only service account, enter it in Contextflo's secure form, and ask your first question.

Source: https://contextflo.com/docs/data-sources/bigquery-setup

Contextflo connects to BigQuery with a Google Cloud service account that can read your tables and run queries, and nothing else. This takes about 10 minutes.

## Why it's safe

- **Read-only.** The two roles below can read tables and run queries. They can't change or delete data, and Contextflo checks every query and blocks writes.
- **Credentials only go in Contextflo's secure form.** Never paste the key into a chat, and never print the key file in a terminal.
- **Stored encrypted.** Contextflo encrypts the key and never shows it again, not even to you.
- **Revocable any time.** Delete the key in Google Cloud and Contextflo loses access immediately (see "Revoke access" below).

## What you need

- The Google Cloud project that holds the BigQuery datasets you want to query.
- Permission in that project to create a service account, grant it these roles, and create a key: **Owner**, or **Service Account Admin**, **Service Account Key Admin** and **Project IAM Admin**.

## Step 1: Create the service account

Create a service account in the project that holds your datasets, and give it two roles. Both are required:

- **BigQuery Data Viewer** (`roles/bigquery.dataViewer`): reads your tables.
- **BigQuery Job User** (`roles/bigquery.jobUser`): runs queries.

Pick one way. Both end with a JSON key file on your computer.

### Option 1: terminal (gcloud)

Replace `PROJECT_ID` with the ID of the project that holds your datasets.

```bash
gcloud iam service-accounts create contextflo-reader \
  --project=PROJECT_ID \
  --display-name="Contextflo (read-only)"

gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:contextflo-reader@PROJECT_ID.iam.gserviceaccount.com" \
  --role="roles/bigquery.dataViewer"

gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:contextflo-reader@PROJECT_ID.iam.gserviceaccount.com" \
  --role="roles/bigquery.jobUser"

gcloud iam service-accounts keys create ./contextflo-key.json \
  --iam-account=contextflo-reader@PROJECT_ID.iam.gserviceaccount.com
```

The last command writes the key to `./contextflo-key.json`. Don't print or open it in the terminal: the next step uploads the file as is.

Then find where your datasets live. Each dataset in the output has a `location`, such as `US`, `EU` or `us-central1`:

```bash
bq ls --format=prettyjson --project_id=PROJECT_ID
```

### Option 2: Google Cloud console

1. Open [Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) and check the project selector at the top shows the project that holds your datasets.
2. Click **Create service account**, name it `contextflo-reader`, and click **Create and continue**.
3. Under **Grant this service account access**, add **BigQuery Data Viewer**, then **Add another role** and add **BigQuery Job User**. Click **Continue**, then **Done**.
4. Open the new service account, go to the **Keys** tab, click **Add key** > **Create new key**, choose **JSON** and click **Create**. The key file downloads.
5. In the [BigQuery console](https://console.cloud.google.com/bigquery), click a dataset you want to connect and note its **Data location**, such as `US`, `EU` or `us-central1`.

## Step 2: Add it in the Contextflo console

On **Data Sources**, add a connection and choose **Google BigQuery**. Fill in:

- **Name**: anything that tells your team which project this is, such as "Analytics BigQuery".
- **Description** (optional).
- **Project ID**: the project that holds your BigQuery datasets, the same one you created the service account in. You'll also find it as `project_id` in the key file.
- **Location**: your datasets' location, exactly as BigQuery shows it. `US` and `us-central1` are different locations.
- **Service Key**: click **Upload .json** and choose the key file.

Click **Test connection**, then **Connect**.

## Step 3: Pick tables and ask a question

Choose the tables your team asks about. Then tell your agent "done", and ask it a first question about those tables.

Next, add context so answers are right the first time: [After connecting your data](https://contextflo.com/docs/after-connecting-data.md).

## Notes

- **One location per connection.** A connection queries one location. If your datasets are in more than one, start with the location you need most.
- **Key creation blocked?** If Google Cloud says key creation is disabled, your organization has the policy `constraints/iam.disableServiceAccountKeyCreation` turned on. An organization admin has to allow key creation for this project.
- **Only some datasets?** Grant **BigQuery Data Viewer** on those datasets instead of the whole project. **BigQuery Job User** must stay on the project.
- **Delete the local key** once it's uploaded: `rm ./contextflo-key.json`, or delete the downloaded file. Contextflo has its own encrypted copy.

## Troubleshooting

- **The test passes, but every query fails with a permission error.** The service account is missing **BigQuery Job User**, which is what lets queries run. Listing tables works without it. Add the role and try again.
- **Queries fail with "Dataset not found in location X".** **Location** doesn't match where the datasets are. The test and table list still work with a wrong location, so check it against `bq ls` or the dataset's **Data location**, then update the connection.
- **"Project not found" or the test fails straight away.** Check **Project ID** is the project ID (not its name or number) and that the key belongs to that project.

## Revoke access

Delete the key, and Contextflo can no longer connect:

- Console: open the service account, go to **Keys**, and delete the key.
- Terminal: list the keys, then delete the one you created.

```bash
gcloud iam service-accounts keys list \
  --iam-account=contextflo-reader@PROJECT_ID.iam.gserviceaccount.com

gcloud iam service-accounts keys delete KEY_ID \
  --iam-account=contextflo-reader@PROJECT_ID.iam.gserviceaccount.com
```

To remove access completely, delete the `contextflo-reader` service account.
