Skip to content

Attach a Notion workspace to a sandbox, where its pages and databases are files.

Mount a Notion workspace in a sandbox, and its pages and databases appear as read-only files that any program in the sandbox reads, with no Notion API. The files are read from Notion as they are opened, not copied in, so they follow the workspace as it changes.

Connect Notion

  1. As a workspace owner, create an internal connection and give it the Read content capability.
  2. On the top-level page you want to expose, open the ••• menu, select Add connections, and choose your connection.
  3. Set NOTION_API_KEY in the environment of the program running the VirtX client, using the API token from the connection’s Configuration tab.

Mount and list pages

With a connected client, mount Notion at notion inside a new volume, then mount that volume at /context in the sandbox. The command lists the top-level pages visible to the connection.

The VirtX server calls the Notion API, so a self-hosted server needs access to api.notion.com and the sandbox can run with its network disabled.

import os
from virtx import NotionSource, SandboxVolume
volume = await client.create_volume()
await volume.mount("notion", NotionSource(os.environ["NOTION_API_KEY"]))
sandbox = await client.create_sandbox(
"alpine:latest",
volumes=[SandboxVolume(volume, "/context", readonly=True)],
network=False,
)
await sandbox.exec(["ls", "/context/notion/pages"]).complete(print=True)

Read a page

Each directory under /context/notion/pages has a name of the form <title>__<page-id>. Use a name from the listing to run a command like this inside the sandbox.

cat '/context/notion/pages/<title>__<page-id>/page.json'

Read the markdown field of page.json for the page body. Child pages appear in subdirectories beneath their parent. If the listing is empty, check that the connection has access to a top-level page.

Read a database

A database in a page appears in the page’s directory beside its child pages, named <title>__db__<database-id>. Its database.json holds the database’s schema in properties and one entry per row in rows. Each row carries its page_id, its directory name in dir, and its values in properties, as the Notion API returns them. The whole index comes from one query, so read database.json to pick rows rather than opening every row’s directory.

cd '/context/notion/pages/<title>__<page-id>/<database>__db__<database-id>'
jq -r '.rows[] | select(.properties.Status.status.name == "Done") | .dir' database.json

This prints the directories of the rows whose Status is Done:

Fix_login_redirect__1c2e4f6a-0b3d-4e5f-8a9b-0c1d2e3f4a5b
Write_release_notes__7d8e9fa0-1b2c-4d3e-9f4a-5b6c7d8e9fa0

Each property nests its value under its type’s name, such as status, select or date, so match the path to your database’s schema. One row of database.json looks like this, trimmed to two properties:

{
"page_id": "1c2e4f6a-0b3d-4e5f-8a9b-0c1d2e3f4a5b",
"dir": "Fix_login_redirect__1c2e4f6a-0b3d-4e5f-8a9b-0c1d2e3f4a5b",
"icon": "🐛",
"properties": {
"Name": { "type": "title", "title": [{ "plain_text": "Fix login redirect" }] },
"Status": { "type": "status", "status": { "name": "Done" } }
}
}

A row is a page like any other: its page.json has the same properties and the row’s body in markdown.

jq is not in alpine:latest, and a sandbox without a network cannot install it. Build an image with it as in Images and create the sandbox from that.

When you finish, call remove on the sandbox and then on the example volume. To keep the volume and disconnect Notion, call volume.unmount("notion") after removing the sandbox.