> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jedify.com/llms.txt
> Use this file to discover all available pages before exploring further.

# dbt Cloud

> Connect dbt Cloud with a read-only service token, so Jedify reads the models your production runs build.

Jedify reads your dbt project from dbt Cloud's Discovery API: the models your production environment's runs built. It signs in with a service token that can only read that metadata, kept in your secret manager.

## Before you start

* **A dbt plan with the Discovery API**: Starter, Enterprise or Enterprise+.
* **A dbt account admin**, to create the token.
* **A production environment with at least one successful job run.** Jedify reads the models those runs built. With no run, the source still connects, but there is nothing to read.

## 1. Create a read-only token

A dbt account admin creates it under **Account settings > Service tokens**:

| Setting | Value |
| - | - |
| Name | `jedify`, or any name you will recognize |
| Permission set | **Metadata Only** |
| Projects | Only the project Jedify should read |

Metadata Only reads models, runs, sources and tests through the Discovery API, and can change nothing. It can't call dbt's Admin API, which Jedify doesn't use.

> [!NOTE]
>
> dbt's documentation disagrees on whether Starter plans offer Metadata Only tokens. Connecting with one worked on a Starter trial account.

## 2. Collect the four values

| Key | Where to find it |
| - | - |
| `project_id` | In dbt, open **Orchestration > Environments**, then your production environment. Its address ends in `/projects/<project_id>/environments/<environment_id>`. |
| `environment_id` | The same address. |
| `discovery_api_url` | **Account settings > Access URLs**: the URL with `.metadata.` in it, with `/graphql` added to the end. |
| `service_token` | The token from step 1. |

For example, the environment at `https://ab123.us1.dbt.com/deploy/1234/projects/70001/environments/80002` has `project_id` `70001` and `environment_id` `80002`, and that account's `discovery_api_url` is `https://ab123.metadata.us1.dbt.com/graphql`.

> [!WARNING]
>
> `discovery_api_url` must end in `/graphql`. dbt's Access URLs page shows it without (`https://ab123.metadata.us1.dbt.com`), and Jedify sends its queries to the URL exactly as written. Without `/graphql`, dbt answers `HTTP 404`.

### Check them

Whoever stores the secret checks the four values first, on a workstation with `curl`, `jq` and `gcloud`:

```sh theme={null}
export DBT_PROJECT_ID="70001"
export DBT_ENVIRONMENT_ID="80002"
export DBT_URL="https://ab123.metadata.us1.dbt.com/graphql"
# Paste the token and press Enter. It isn't shown.
read -rs DBT_TOKEN && export DBT_TOKEN

QUERY="{ environment(id: $DBT_ENVIRONMENT_ID) {
  applied { models(first: 5) { edges { node { name } } } } } }"
jq -nc --arg query "$QUERY" '{$query}' \
  | curl -sS "$DBT_URL" -w '\nHTTP %{http_code}\n' -d @- \
      -H "Authorization: Bearer $DBT_TOKEN" \
      -H "Content-Type: application/json"
```

| You see | It means |
| - | - |
| Up to five of your model names, then `HTTP 200` | All four values work. |
| No model names and no error, then `HTTP 200` | The token and the IDs work, but the environment has no successful run yet, or it isn't your production environment. |
| `There is no data available for this input.` | dbt can't read that environment with that token: the token is wrong, it isn't scoped to the environment's project, or the environment ID is wrong. |
| `HTTP 403` and `Forbidden: Access denied` | Your dbt account restricts IP addresses, and this workstation's isn't on its list. See [If dbt restricts IP addresses](#if-dbt-restricts-ip-addresses). |
| `HTTP 404` | The URL: check the host, and that it ends in `/graphql`. |
| `Could not resolve host`, or `HTTP 000` | The host is wrong, or this workstation can't reach it. |

## 3. Store them in Secret Manager

In the same shell, store the four values as one JSON secret in your installation's project. The token goes straight into Secret Manager, never into a file:

```sh theme={null}
jq -n '{project_id: env.DBT_PROJECT_ID,
        environment_id: env.DBT_ENVIRONMENT_ID,
        discovery_api_url: env.DBT_URL,
        service_token: env.DBT_TOKEN}' \
  | gcloud secrets create dbt-cloud-credentials \
      --data-file=- --project=acme-jedify
unset DBT_TOKEN
```

## 4. Let the cluster reach dbt

Jedify calls the host of your `discovery_api_url` on port 443, from the cluster.

* **In a network OpenTofu created**, its Cloud NAT lets the call out. There is nothing to do.
* **In your own network**, your network team allows the host out, next to your warehouses' hosts. The preflight checks it from the install host: add `--egress ab123.metadata.us1.dbt.com` to the command in [Requirements](https://docs.jedify.com/self-hosted/gcp/requirements.md#check-before-you-start).

### If dbt restricts IP addresses

dbt Enterprise+ accounts can refuse every call from an address that isn't on their list, service tokens included. dbt then answers `HTTP 403` with `Forbidden: Access denied`, and the source fails to connect.

A dbt account admin adds the addresses Jedify's calls leave from. In your own network, your network team has them: your NAT's or firewall's. In a network OpenTofu created, they are its Cloud NAT's:

```sh theme={null}
gcloud compute routers get-status acme-jedify-router \
  --region=us-central1 --project=acme-jedify --format=json \
  | jq -r '.result.natStatus[].autoAllocatedNatIps[]'
```

> [!WARNING]
>
> Google picks the addresses of the Cloud NAT that OpenTofu creates, and adds or removes them as traffic needs. You can't know the next one in advance, so an allowlist of today's addresses can break later. Where dbt restricts IP addresses, install into a network whose traffic leaves from addresses you reserved: see [Install into an existing network](https://docs.jedify.com/self-hosted/gcp/install/existing-network.md).

## 5. Declare the source

Add it to your Jedify resource and apply:

```yaml theme={null}
spec:
  dataSources:
    - name: dbt-cloud
      type: dbt
      credentials:
        externalSecret:
          remoteRef: { key: dbt-cloud-credentials }
```

The operator reads the secret through the installation's own store, from its own project. For a secret in another project or another secret manager, see [Credentials](https://docs.jedify.com/self-hosted/gcp/configure/credentials.md). A Kubernetes Secret in the `jedify-be` namespace works too: `credentials: { name: dbt-cloud, key: credentials.json }`.

## 6. Check it

```sh theme={null}
kubectl -n jedify get jedify jedify \
  -o jsonpath='{range .status.dataSources[*]}{.name}{"\t"}{.state}{"\t"}{.sourceType}{"\t"}{.message}{"\n"}{end}'
```

Expected: `dbt-cloud   Connected   pipeline`. `pipeline` is how Jedify classes dbt Cloud: a source that is not a warehouse.

## When it fails

| Message | Fix |
| - | - |
| `invalid dbt credentials: discovery_api_url`, or other keys | The secret lacks those keys. Store it again as in [step 3](#3-store-them-in-secret-manager), with `gcloud secrets versions add dbt-cloud-credentials` in place of `gcloud secrets create dbt-cloud-credentials`. |
| `Failed to connect. Check credentials.` | Run the [check](#check-them) with the values you stored. If it passes on your workstation, the cluster can't reach dbt: see [step 4](#4-let-the-cluster-reach-dbt). The connect's log, below, shows what the cluster got. |
| `Waiting`, naming an ExternalSecret | The secret is missing from Secret Manager, or named differently from `remoteRef.key`. `kubectl get externalsecrets -A` says which. |
| `Connected`, but none of your models reach Jedify | The environment has no successful run, or `environment_id` names another environment. The [check](#check-them) shows which. |

The connect's log, when it says `Failed to connect`:

```sh theme={null}
kubectl -n jedify-be logs -l jedify.com/data-source=dbt-cloud \
    --all-containers --tail=200 \
  | jq -rR 'fromjson? | select(.record.level.name == "ERROR")
            | .record.message'
```

A line with `argument of type 'NoneType'` means dbt returned no data for the environment: the token is wrong, it isn't scoped to the environment's project, or the environment ID is wrong. Any other line carries dbt's own answer, or the network error.

## Rotate the token

1. A dbt account admin creates a new token, as in [step 1](#1-create-a-read-only-token).
2. Run the [check](#check-them) with the new token, then [step 3](#3-store-them-in-secret-manager) with `gcloud secrets versions add dbt-cloud-credentials` in place of `gcloud secrets create dbt-cloud-credentials`.
3. External Secrets reads the secret again within the hour, and the operator connects the source with the new token. An hour later, with the source still `Connected`, delete the old token in dbt.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.