> ## 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.

# Connect your data

> Add a source to hatchmark.yaml, look at its columns, and check that metrics read it, from a CSV file or a database.

You will end with a new source that metrics can read, checked against its columns and guarded by a
test. The steps use a CSV file in the sample project from the [quickstart](/docs/get-started/quickstart.md);
[step 5](#5-or-connect-a-database) swaps the file for a database table.

## 1. Add the file

Copy the file into `data/` and name it under `sources` in `hatchmark.yaml`. The key is the name
metrics use, in lowercase snake\_case.

```yaml theme={null}
sources:
  orders:
    type: csv
    path: data/orders.csv
  refunds:
    type: csv
    path: data/refunds.csv
```

`path` is relative to `hatchmark.yaml`. Parquet, JSON, DuckDB and SQLite files and cloud storage work
the same way with their own `type`; [data sources](/docs/reference/sources.md) lists every option.

## 2. Look at the columns

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

It prints each column's type and kind, null and distinct counts, the range of time and number
columns, and the first rows. These are the types metrics will see, so check that dates read as
`time` and amounts as `number`. A column that reads as `text` usually holds a stray value, such as
`n/a` in an amount; fix the export, or read it with a database `query` that casts it.

## 3. Ask it a question

```sh theme={null}
hatchmark data query "SELECT reason, count(*) AS refunds, sum(amount) AS total FROM refunds GROUP BY 1 ORDER BY 3 DESC"
```

Every source is a table named after it, and the query runs read-only over the same data `build` reads.
Use it to confirm the numbers you expect before you define metrics on them.

## 4. Check the project

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

Check reads the new source's columns and reports any metric or block that names one it doesn't have.
Next, [define metrics](/docs/guides/define-metrics.md) on it.

## 5. Or connect a database

A database source names an environment variable that holds the connection string, never the string
itself:

```yaml theme={null}
sources:
  refunds:
    type: postgres
    env: SHOP_DATABASE_URL
    table: public.refunds
    max_age:
      hours: 1
```

```sh theme={null}
export SHOP_DATABASE_URL="postgresql://reader:secret@db.example.com:5432/shop?sslmode=require"
hatchmark data describe refunds
```

* `mysql`, `sqlserver` and `bigquery` work the same way;
  [database sources](/docs/reference/sources.md#database-sources) has each connection string.
* `posthog` reads the result of a HogQL query in PostHog; see [Use PostHog data](/docs/guides/posthog.md).
* `query` in place of `table` runs one `SELECT` in the database, to cast, filter or join first.
* `max_age` reuses the local copy of the rows while it is younger than an hour, so `dev` doesn't fetch
  on every save.
* `check` runs without the variable and only warns that it couldn't read the columns, so it can run
  in CI. `build` needs the rows and must run where the database is reachable.

## 6. Guard it with a test

A row-count test fails when an export comes in short or a column fills with nulls:

```yaml theme={null}
refunds:
  type: csv
  path: data/refunds.csv
  tests:
    - name: full export
      rows: { min: 100 }
    - name: every refund has an amount
      rows: { max: 0 }
      where:
        - column: amount
          op: is_null
```

```sh theme={null}
hatchmark test refunds
```

It exits 1 when a test fails, so it fits a CI step. See
[row-count tests](/docs/reference/sources.md#row-count-tests).


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