# Connect your own BigQuery

This guide is for the Google Cloud administrator of an organization that runs its Keboola projects on its own BigQuery ([Bring Your Own Database](https://help.keboola.com/storage/byodb/)). Before you decide on your own BigQuery, check the [BigQuery limits](https://help.keboola.com/storage/byodb/#bigquery-limitations).

This guide is designed to walk you through the essential processes of setting up and configuring various Google Cloud resources for connecting your BigQuery 
and related services to your Keboola project. From creating a dedicated folder in your Google organization to creating a project and a service account, 
each section is tailored to ensure a smooth and efficient setup.

![How to connect BigQuery](https://help.keboola.com/storage/byobq/pic.png)

***Note:** Naming conventions are up to you, but we strongly recommend naming all resources in a way that makes their connection to Keboola clearly identifiable (e.g., include "keboola" in the name). This makes debugging significantly easier when issues arise.*

**Before you start**

You need:

- **These Google Cloud roles for yourself:** Folder Admin (or Folder Creator plus Folder IAM Admin) on the organization, Project Creator on the new folder, and Billing Account Administrator on the billing account. An Organization Administrator can grant them.
- **A Google Cloud billing account** for the new project.
- **The region of the Keboola product** you bought through the marketplace. The Cloud Storage bucket and both registrations use it.
- **A Keboola Management API token** for the registration at the end. Generate one in **Admin > Account > Access Tokens (https://connection.keboola.com/admin/account/access-tokens)**. Registering the file storage works with any admin's personal access token; registering the BigQuery backend needs a super admin.
- **An HTTP client** such as `curl`, for the two Management API calls.

## Create BigQuery and GCP resources

**Goal:** a folder, a project, a service account and a storage bucket in Google Cloud that Keboola can use.

### 1. Create a Folder 
[**Learn more**](https://cloud.google.com/resource-manager/docs/creating-managing-folders)

1.	Go to **IAM & Admin > [Manage Resources](https://console.cloud.google.com/cloud-resource-manager)** and click on the **Create folder** button.
2.	In the **Folder name** box, enter the new folder’s name: “Keboola”.
3.	Under **Organization** and **Location**, select the organization resource or folder under which you want to create your new folder. If you have any difficulties creating a folder, learn more [here](https://cloud.google.com/resource-manager/docs/creating-managing-folders#folder-permissions). Click **Create**.
4.	On the **Resource Manager** page, uncollapse the organization, locate your created folder, and copy its ID to the Keboola BigQuery registration form.

### 2. Create a Project 
[**Learn more**](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project)

1.	In the Resource Manager, click **Create Project**.
2.	Set the **Project name** to “Keboola Main”.
3.	In **Location**, choose the previously created folder. Click **Create**.
4.	Once the project is created, select it by clicking **Select project** in the notification or find the project using the selector in the top left corner of the Google console.
5.	Ensure that **billing is enabled** for your Google Cloud project ([verify that billing is enabled](https://cloud.google.com/billing/docs/how-to/verify-billing-enabled#console)).

### 3. Enable API Services 
[**Learn more**](https://cloud.google.com/endpoints/docs/openapi/enable-api#enabling_an_api)

1.	Open **APIs & services** in the navigation menu of your project. Click **Enable APIs and Services**.
2.	Search and enable each service from the following list by clicking the **Enable** button on its detail page:
    - Cloud resource manager API (cloudresourcemanager.googleapis.com)
    - Service usage API (serviceusage.googleapis.com)
    - Identity and access management (IAM) API (iam.googleapis.com)
    - Cloud billing API (cloudbilling.googleapis.com)
    - Analytics hub API (analyticshub.googleapis.com)
    - BigQuery API (bigquery.googleapis.com)

### 4. Create a Service Account 
[**Learn more**](https://cloud.google.com/iam/docs/service-accounts-create#creating)

1.	Go to **IAM & Admin > Service Accounts** in your project. Click **Create service accounts**. 
2.	Name the service account “Keboola Main Service Account” and provide a description. Click **Create and continue**.
3.	Go back to the **Service Accounts** page and click on the newly created service account.
    - In the **Keys** tab, click **Add key** and select **Create new key**.
    - Choose JSON as the key type and click **Create**. The key file will be downloaded to your system. This file is needed for the Keboola BigQuery registration form.
4.	Assign roles to the service account:    
    1. Assign **Project Roles** to the service account.
        - Go to **IAM & Admin > IAM** in the project.
        - Click **Grant access** and copy the service account’s email address into the **New principal** field.
        - Select the role **Basic > Owner**. Click **Save**.
        - Click **Add another role** and select **Cloud Storage > Storage Object Admin**.
        - Click **Save**.
    2. Assign **Folder Roles** to the service account:
        - Paste your folder name or ID into the search bar and select it.
        - In the **IAM & Admin** section, go to **IAM**.
        - Click **Grant access** and enter the service account’s email address.
        - Select the role **Basic > Browser**.
        - Click **Add another role**.
        - Select the role **Resource Manager > Project Creator**.
        - Click **Save**.
    3. Assign **Billing Roles** to the service account:
        - Open the **Billing** section from the navigation menu in your project.
        - In the **Overview** section, find the card with the **Billing account** with your billing account name and its ID.
        - Click on the **Manage** link.
        - Click **Add principal**.
        - Paste the principal email address of the service account created earlier.
        - Select the role **Billing > Billing Account User** and click **Save**.

### 5. Create a Google Storage Bucket 
[**Learn more**](https://cloud.google.com/storage/docs/creating-buckets#create_a_new_bucket)

1.	Navigate to **Cloud Storage > Buckets**. Click **Create Bucket**.
2.	**Name the bucket**, e.g., “keboola-yourorganization-files”. The bucket name must be globally unique, so choose a custom name.
3.	**Choose the region** where your data will be stored. <br/>**Important**: The bucket must be set in the same region as the product bought through the marketplace. For example, if you buy a Keboola EU product, select the EU region; otherwise, the registration of the BigQuery backend in Keboola will be rejected.
4.	Set **other options** as desired. We recommend checking _Enforce public access prevention on this bucket_. Click **Create**.
5.	Once the bucket is created, select the **Lifecycle** tab and click **Add rule**.
    - Select the action **Delete object** and click **Continue**.
    - Under **Select object condition > Set rule scopes**, select **Object Name Matches Prefix** and set the prefix to `exp-2/`.
    - Under **Select object condition > Set conditions**, select `Age`. Set the age to `2 days`and click **Create**.
    - Repeat the previous steps for the prefix `exp-15/` with an age of `15 days`.

**Result:** You have the folder ID, the JSON key file of the service account, and the name and region of the bucket. The registration below uses all of them.

**Check:** in **IAM & Admin > IAM** of the project, the service account has **Owner** and **Storage Object Admin**. On the folder's IAM page it has **Browser** and **Project Creator**, on the billing account it has **Billing Account User**, and the bucket's **Lifecycle** tab shows two **Delete object** rules, for `exp-2/` and `exp-15/`.

***Note:** If you want to see your BigQuery tables directly in the BigQuery console, go to your folder and add a BigQuery Data Viewer or higher role.*

## Register resources in Keboola

**Goal:** a file storage and a BigQuery backend registered in Keboola, ready for Keboola Support to attach to your project.

### 1. Register GCS File Storage in Keboola

Once the GCS bucket is ready, register it in Keboola via the [Management API](https://api.keboola.com/?service=manage#post-/manage/file-storage-gcs), with the Management API token from [Before you start](https://help.keboola.com/storage/byobq/#before-you-start).

Send a `POST` request to `/manage/file-storage-gcs`:

```
POST https://<your-keboola-url>/manage/file-storage-gcs
X-KBC-ManageApiToken: <your-manage-api-token>
Content-Type: application/json
```

```json
{
  "gcsCredentials": {
    "type": "service_account",
    "project_id": "...",
    "private_key_id": "...",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
    "client_email": "...",
    "client_id": "...",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "..."
  },
  "filesBucket": "<your-gcs-bucket-name>",
  "owner": "client",
  "region": "<your-gcs-region>"
}
```

The `gcsCredentials` object is the full content of the JSON key file downloaded when creating the service account. Set `filesBucket` to the name of the bucket created in the previous step, and `region` to the region where the bucket was created (e.g., `europe-west1`).

A successful response returns HTTP `200` with an object containing the new file storage `id`. Note this `id`: Keboola Support needs it in the next step.

### 2. Register BigQuery Backend in Keboola

Register the BigQuery table backend via the Management API. Send a `POST` request to `/manage/storage-backend/bigquery`:

```
POST https://<your-keboola-url>/manage/storage-backend/bigquery
X-KBC-ManageApiToken: <your-manage-api-token>
Content-Type: application/json
```

```json
{
  "credentials": {
    "type": "service_account",
    "project_id": "...",
    "private_key_id": "...",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
    "client_email": "...",
    "client_id": "...",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "..."
  },
  "folderId": "folders/<your-folder-id>"
}
```

The `credentials` object is the full content of the service account JSON key file. Set `folderId` to the ID of the folder created in the first step, prefixed with `folders/` (e.g., `folders/1234567890`).

A successful response returns HTTP `200` with an object containing the new backend `id`. Note this `id`: Keboola Support needs it in the next step.

**Result:** You have a file storage ID and a BigQuery backend ID.

***Note:** Once both resources are registered, contact Keboola Support and provide the file storage ID and BigQuery backend ID. Support will set up your project with these backends.*

For full API reference, see the [Keboola Management API documentation](https://api.keboola.com/?service=manage).

**Check:** both calls returned an object with an `id`. The BigQuery backend registration checks the service account's roles and lists each one as held or missing; grant any missing role in Google Cloud and send the call again.

---

**Next:** [External Datasets →](https://help.keboola.com/storage/byodb/external-buckets/)
