# Notion (https://docs.ctxpipe.ai/docs/self-hosting/notion)

## Overview

Self-hosted Notion uses the same connector routes as hosted ctxpipe. The
main path is to register a **private Notion OAuth app** in the product UI. Then
you can connect a workspace without `NOTION_*` environment variables and
without a restart. Environment variables stay as an optional shortcut for hosted
and operator setups.

Traffic stays on **this** deployment's `AUTH_BASE_URL`. Do not point OAuth
callbacks or webhook URLs at hosted ctxpipe.

## 1. Register the OAuth app in the UI

1. Open **Connectors**, select **Add connection**, then select **Connect** on
   the **Notion** row. ctxpipe creates a draft Notion connection.
2. On **Register Notion OAuth app**, create an OAuth app in the
   [Notion integrations portal](https://www.notion.so/profile/integrations).
   For a private install, choose **Selected workspaces only**. Do not list the app
   on the Marketplace. An internal (token-only) integration does not work.
3. Copy the callback URL from the wizard into the app's OAuth / redirect URI
   field. It is:

```text
{AUTH_BASE_URL}/api/v1/connectors/notion/oauth/callback
```

Set **`NOTION_REDIRECT_URI`** only when the public callback differs from the
URL that ctxpipe derives from `AUTH_BASE_URL`.

4. Paste the **Client ID** and **Client secret**, then save. ctxpipe stores the
   secret encrypted on that connection. It never shows the secret again.
5. On **Add the webhook**, open the **Webhooks** tab of the same app. Add a webhook.
   Paste the wizard event URL into **Webhook URL**. Select the page, data
   source, and database event types (`page.*`, `data_source.*`, and
   `database.*`). ctxpipe ignores other events. If you do not select a type, ctxpipe
   does not get its updates. Query parameters scope the URL to this connection:

```text
{AUTH_BASE_URL}/api/v1/webhook/notion?connectionId={id}&provisioningToken={hmac}
```

The provisioning token is an HMAC of the saved client secret. Notion's first
verification request is unsigned. ctxpipe checks that HMAC. It stores the
verification token on the same row and shows it in the wizard. Paste that
**Verification token** into Notion's **Verify subscription** dialog. Then select
**Verify subscription**.

6. Choose **Continue**, then **Connect Notion**. Then authorize the workspace.

The worker reads the OAuth client credentials from the connection row. Token
refresh therefore works when the `NOTION_*` environment variables are empty.

## 2. Optional env shortcut

Operators who have a shared OAuth app can set
**`NOTION_CLIENT_ID`**, **`NOTION_CLIENT_SECRET`**, and
**`NOTION_WEBHOOK_SECRET`** on the backend and worker instead of saving them
in the wizard. When you set those environment variables, **Add Notion** skips the
register step and opens the OAuth popup.

For webhook provisioning with environment variables only (no `connectionId` on
the webhook URL), generate the provisioning token from the repository root:

```bash
NOTION_CLIENT_SECRET='your-client-secret' bun -e 'import { createHmac } from "node:crypto"; process.stdout.write(createHmac("sha256", process.env.NOTION_CLIENT_SECRET ?? "").update("ctxpipe:notion-webhook-provisioning:v1").digest("base64url"))'
```

Use the result in:

```text
{AUTH_BASE_URL}/api/v1/webhook/notion?provisioningToken={generated-token}
```

Copy the **verification token** from Notion after the first delivery. Set
**`NOTION_WEBHOOK_SECRET`** to it. Provisioning with environment variables only
does not save that token on a connection row.

Do not ask each ctxpipe organization to create a second webhook path. One
webhook URL for each integration is enough. A self-hosted deployment scopes it
with query parameters.

## 3. Supply env secrets when you use the shortcut

### Docker Compose

Add the values to the root `.env`, then restart the backend and the worker:

```bash
NOTION_CLIENT_ID=...
NOTION_CLIENT_SECRET=...
NOTION_WEBHOOK_SECRET=...
pnpm start
```

### AWS CDK

Pass `notionClientId`, `notionClientSecret`, and `notionWebhookSecret` through
the construct's `connectorSecrets`. Keep the values in your CDK secret source.
Do not commit them in stack code.

## 4. Complete connector setup

After you register the integration in the UI or with environment variables:

1. Authorize the Notion workspace.
2. In **Add to Workspaces**, select the Workspaces that receive Notion
   content.
3. Select the Notion pages and databases to mirror.
4. Review and merge the pull request that adds `sources/notion/<name>.md`.

ctxpipe writes pages as Markdown under `notion/pages/`. It writes databases
under `notion/databases/`. Each database has one `table.csv` snapshot, and each
row has one Markdown file, for task and knowledge retrieval. ctxpipe copies
provider-declared files and explicit embedded external media into an `assets/`
directory beside the page or row that uses them. The limits are 25 MiB for each file and
100 MiB for each page or row. ctxpipe never saves Notion URLs that expire. A
failed, unsafe, or oversized download leaves a Notion page link or a caption
stub.

## Verify the complete flow

1. Add Notion and save a private OAuth app (or confirm that the environment variables are set).
2. Authorize a clean Notion workspace through the product flow.
3. Share a page or database with the integration and confirm it appears.
4. Select a Workspace in **Add to Workspaces** and merge the pull request that
   adds `sources/notion/<name>.md`.
5. Confirm the initial mirror writes readable files under `notion/`.
6. Edit an in-scope source and confirm the deployment webhook receives the
   event.
7. Confirm the generated git file changes and the Workspace re-indexes.

## Troubleshooting

### OAuth returns to the wrong deployment

The integration callback must match the public `AUTH_BASE_URL` exactly, including
scheme, host, and path. Set `NOTION_REDIRECT_URI` only when the external route
differs.

### Register step does not appear

The wizard skips registration when both `NOTION_CLIENT_ID` and
`NOTION_CLIENT_SECRET` are set. That is the hosted/operator shortcut.

### Signed events return `401` or `503`

If you used the UI webhook URL, confirm Notion's verification completed on that
connection (the webhook URL includes `connectionId`). If you used the
environment variable shortcut, set `NOTION_WEBHOOK_SECRET` to the verification token shown after
Notion's first delivery, then restart the backend. The provisioning token in
the webhook URL does not replace the signing secret.

### Content sync cannot refresh authorization

Confirm the connection still has an OAuth app (or that both backend and worker
receive the same Notion client ID and secret) and can reach `api.notion.com`.

### No pages appear

Notion returns only the pages and databases that you share with the
integration. Share the resource in Notion. Then refresh resources in the ctxpipe
setup flow.

## User guide

See [Notion connector](/docs/connections/source-connectors/notion) for scope,
repository layout, review workflow, and user-facing recovery steps.

## Related pages

* [Workspaces](/docs/workspaces)
* [GitHub connector](/docs/connections/source-connectors/github)
* [Indexing](/docs/workspaces/indexing)
