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

# Embed charts in your app

> Show a dashboard's charts in your own page, React or Next.js app, drawn as the dashboard draws them, with picks that filter the other charts.

You will end with blocks from a dashboard inside your own page, drawn as the dashboard draws them,
with a click on a bar or a map region filtering the other blocks. The steps use a plain HTML page,
then React, then Next.js; follow the one your app uses. Every chart reads
[serve's API](/docs/reference/api.md), so it shows the numbers of the last good build.

## 1. Let your app read the API

Run `hatchmark serve` as in [Production](/docs/guides/production.md). An app served from the same origin needs
no setting. An app on another origin needs that origin listed:

```sh theme={null}
HATCHMARK_ALLOWED_ORIGINS=https://app.example.com hatchmark serve
```

The browser then sends the viewer's cookies to serve, so your gateway decides who sees the numbers,
as it does for the pages ([calling from another origin](/docs/reference/api.md#calling-from-another-origin)).
Let `/_hatchmark/static/` through without a sign-in, and nothing else: browsers load the script
from there without cookies from another origin. That path serves only the script, its chunks, fonts
and map boundaries, which never change once built and hold none of your data. The API, the pages
and the other files under `/_hatchmark/` stay behind your sign-in.

## 2. Add charts to a plain HTML page

Load the script serve hands out and place a chart per block, named `<dashboard>.<block id>`:

```html theme={null}
<script
  type="module"
  src="https://bi.example.com/_hatchmark/static/elements.js"
></script>

<hatchmark-scope endpoint="https://bi.example.com/api/v1">
  <hatchmark-chart block="sales.revenue_trend"></hatchmark-chart>
  <hatchmark-chart block="sales.revenue_by_region"></hatchmark-chart>
</hatchmark-scope>
```

Charts in one `<hatchmark-scope>` share its endpoint and theme, and a pick in one filters the others.
The script comes from the same build as the API, so the two always match. Set `id:` on every block
you embed ([Dashboards](/docs/reference/dashboards.md)): a block without one is named by its
position, which changes when blocks move.

## 3. Use the React components

```sh theme={null}
npm install @hatchmark/react
```

```tsx theme={null}
import { HatchmarkChart, HatchmarkProvider } from "@hatchmark/react";

export function Revenue() {
  return (
    <HatchmarkProvider endpoint="https://bi.example.com/api/v1">
      <HatchmarkChart block="sales.revenue_trend" period="last_90_days" />
      <HatchmarkChart block="sales.revenue_by_region" filters={{ channel: "Social" }} />
    </HatchmarkProvider>
  );
}
```

`period` and `filters` take the values the dashboard's filters offer. To read a block's numbers for
your own UI, such as its as-of date, call `useHatchmarkBlock`. Install `@hatchmark/react` at the
version of `hatchmark` your serve runs.

To catch a misspelled block, period or filter when your app compiles, save the build's types into
your app and commit them:

```sh theme={null}
curl -o src/hatchmark.d.ts https://bi.example.com/api/v1/types.d.ts
```

This works only on a public site, since curl doesn't sign in. Behind sign-in, copy the file from
`_hatchmark/api/types.d.ts` in the folder you pass to `hatchmark serve --out`.

Fetch them again when you add or rename blocks or filters.

## 4. Draw charts on the server with Next.js

Fetch a block in a server component and hand it to the chart, which then shows before any script
runs and doesn't fetch it again:

```tsx theme={null}
import { fetchBlock } from "@hatchmark/react/server";
import { Charts } from "./charts";

export default async function Page() {
  const revenue = await fetchBlock("sales.revenue_trend");
  return <Charts revenue={revenue} />;
}
```

```tsx theme={null}
"use client";
import { type BlockPayload, HatchmarkChart, HatchmarkProvider } from "@hatchmark/react";

export function Charts({ revenue }: { revenue: BlockPayload }) {
  return (
    <HatchmarkProvider endpoint="https://bi.example.com/api/v1">
      <HatchmarkChart block="sales.revenue_trend" payload={revenue} />
    </HatchmarkProvider>
  );
}
```

`fetchBlock` reads serve's API from `HATCHMARK_URL`, such as `http://hatchmark:4000/api/v1` inside
your network, and passes your gateway's headers with `headers`.

## 5. Follow picks in your app

A pick fires `hatchmark-pick` on the scope, or `onPick` on the provider, with every pick on screen:

```tsx theme={null}
const [picks, setPicks] = useState<Picks>({});

<HatchmarkProvider endpoint={api} picks={picks} onPick={(event) => setPicks(event.picks)}>
```

Passing `picks` makes the provider controlled, so your app can show, clear or set them.

## Next steps

* [Embedding](/docs/reference/embed.md): every attribute, prop, event and function.
* [Serve API](/docs/reference/api.md): the payload each chart draws, for apps that draw their own.
* The repository's `examples/embed` folder has the three apps above, ready to run.


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