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

> ## Agent Instructions
> To set up Hatchmark for a user, follow https://hatchmark.dev/docs/get-started/agent-setup.md. Inside a Hatchmark project, prefer `hatchmark docs`: it prints the docs of the installed version.

# Load data from other tools with dlt

> Load data from SaaS tools such as Stripe, HubSpot or GitHub into a DuckDB file or Parquet files with dlt, read it as a source, and refresh it before every build.

Hatchmark reads files and databases, not the APIs of tools such as Stripe, HubSpot, Zendesk or
GitHub. [dlt](https://dlthub.com/docs/intro), an open-source Python library (Apache-2.0), copies
data from those APIs into a DuckDB file or Parquet files in your project, and a `duckdb` or
`parquet` source reads them like any other table. Run the pipeline before `hatchmark build`, and the
dashboards show the latest load.

The steps load the issues and pull requests of a public GitHub repository, which needs no account.
For your own tools, dlt has [verified sources](https://dlthub.com/docs/dlt-ecosystem/verified-sources/)
for many SaaS products and a [REST API source](https://dlthub.com/docs/dlt-ecosystem/verified-sources/rest_api/)
for any other JSON API.

## 1. Install dlt

In the project folder, create a Python environment and install dlt with its DuckDB destination:

```sh theme={null}
python3 -m venv .venv
.venv/bin/pip install "dlt[duckdb]"
```

Add `.venv/` to `.gitignore`.

## 2. Write the pipeline

```python theme={null}
# load_github.py
import dlt
from dlt.sources.rest_api import rest_api_source

source = rest_api_source({
    "client": {"base_url": "https://api.github.com/repos/duckdb/duckdb/"},
    "resource_defaults": {"primary_key": "id", "write_disposition": "merge"},
    "resources": [
        {
            "name": "issues",
            "endpoint": {"path": "issues", "params": {"state": "all", "per_page": 100}},
        },
    ],
})

pipeline = dlt.pipeline(
    pipeline_name="github",
    destination=dlt.destinations.duckdb("data/saas.duckdb"),
    dataset_name="github",
)
print(pipeline.run(source.add_limit(3)))
```

* `add_limit(3)` reads three pages, 300 rows, to try it out. Remove it to load every page.
* `merge` on `primary_key` updates the rows that changed since the last run instead of adding them
  again, so the file holds one row per issue.
* Name the file differently from `dataset_name`. In `github.duckdb`, the schema `github` and the
  database `github` share a name, and dlt's load fails with `Ambiguous reference to catalog or
  schema "github"`.
* A source that needs a key reads it from an environment variable or `.dlt/secrets.toml`, never
  from the script; see [credentials](https://dlthub.com/docs/general-usage/credentials/) in dlt's
  docs. Keep `.dlt/secrets.toml` out of git.

## 3. Run it

```sh theme={null}
.venv/bin/python load_github.py
```

dlt writes the rows to the `github` schema of `data/saas.duckdb`: one table per resource, here
`issues`, and its own bookkeeping tables, whose names start with `_dlt`.

* Nested objects become columns joined with a double underscore: the author's login is
  `user__login`, and a pull request's URL is `pull_request__url`.
* Lists become child tables, such as `issues__labels`, whose `_dlt_parent_id` matches the parent
  row's `_dlt_id`.
* A `merge` load also writes a `github_staging` schema, which no source needs.

## 4. Add the source

```yaml theme={null}
sources:
  issues:
    type: duckdb
    path: data/saas.duckdb
    table: github.issues
```

```sh theme={null}
hatchmark data describe issues
```

Timestamps arrive as `TIMESTAMP WITH TIME ZONE` and read as `time`, so they are bucketed in the
project's `timezone`. Run the pipeline and the build one after the other, not at the same time: the
build reads the file read-only, and dlt holds it while it loads. See
[DuckDB files](/docs/reference/sources.md#duckdb-files) for how the file is read.

## 5. Define metrics on it

GitHub's issues API returns pull requests too; a pull request is a row with a `pull_request__url`.

```yaml theme={null}
# metrics/issues_opened.yaml
label: Issues opened
type: aggregate
table: issues
aggregate: count
filters:
  - column: pull_request__url
    op: is_null
time: created_at
format: { type: number }
better: up
```

```yaml theme={null}
# metrics/pull_requests_opened.yaml
label: Pull requests opened
type: aggregate
table: issues
aggregate: count
filters:
  - column: pull_request__url
    op: not_null
time: created_at
format: { type: number }
better: up
```

```sh theme={null}
hatchmark check
hatchmark build
```

Put them on a page as in [build a dashboard](/docs/guides/build-a-dashboard.md).

## Keep it up to date

Run the pipeline and then the build in the same scheduled job, such as a cron entry or a CI workflow:

```sh theme={null}
.venv/bin/python load_github.py && hatchmark build
```

With `merge`, each run fetches the issues again and updates the ones that changed. For a large
account, load only what changed since the last run with dlt's
[incremental loading](https://dlthub.com/docs/general-usage/incremental-loading).

## Parquet files instead

dlt's [filesystem destination](https://dlthub.com/docs/dlt-ecosystem/destinations/filesystem)
writes Parquet files. A local `parquet` source reads one file, so name each table's file after the
table and replace it on every run:

```python theme={null}
source = rest_api_source({
    "client": {"base_url": "https://api.github.com/repos/duckdb/duckdb/"},
    "resource_defaults": {"primary_key": "id", "write_disposition": "replace"},
    "resources": [
        {
            "name": "issues",
            "endpoint": {"path": "issues", "params": {"state": "all", "per_page": 100}},
        },
    ],
})

pipeline = dlt.pipeline(
    pipeline_name="github_parquet",
    destination=dlt.destinations.filesystem("data", layout="{table_name}.{ext}"),
    dataset_name="github",
)
print(pipeline.run(source.add_limit(3), loader_file_format="parquet"))
```

Install it with `.venv/bin/pip install "dlt[parquet]"`. The run writes `data/github/issues.parquet`:

```yaml theme={null}
sources:
  issues:
    type: parquet
    path: data/github/issues.parquet
```

To keep the rows of earlier runs, append or merge into DuckDB as above. Loading to a bucket instead,
with the filesystem destination's `bucket_url` set to `s3://`, `gs://` or `az://`, works with a
[cloud storage](/docs/reference/sources.md#cloud-storage) source, whose path may be a glob that matches
every file dlt writes for a table.


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