SDK
Add grist-widget-sdk to an existing Vite or Next.js project, then the core hooks every widget uses.
grist-widget-sdk wraps the Grist plugin API — the JavaScript bridge
between a custom widget iframe and a Grist document — into one hook, a
provider, and a boundary. New here? The Overview says what it does and when to use the
plugin API on its own.
Already have a scaffolded project (Your first widget)? Its dependencies and plugin-api script tag are pre-wired — skip ahead to Hello world. This page is for adding the SDK to a project you already have.
Install
pnpm add grist-widget-sdk
# or
npm install grist-widget-sdk
# or
yarn add grist-widget-sdkPeer dependencies:
react >=18
react-dom >=18Load the Grist plugin API
The SDK expects the global grist object at runtime. Add the script in
your app shell:
<script src="https://docs.getgrist.com/grist-plugin-api.js"></script>Hello world
A widget is a declaration and a component, in src/app/App.tsx:
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
title: "Hello",
requiredAccess: "read table",
})
export default function App() {
const w = widget.useGrist()
if (w.mode === "empty") return <p>Select a row.</p>
if (w.mode === "new-row") return <p>Add the row in Grist.</p>
return <p>Selected row #{w.record?.id}</p>
}and one call mounts it, in src/main.tsx:
import { mountGristWidget } from "grist-widget-sdk/app"
import * as entry from "./app/App"
mountGristWidget(entry, { dev: import.meta.env.DEV })That is everything you need to:
- wait for Grist to answer, and show a page when it is opened outside Grist;
- show a screen while access or columns are missing, with a retry;
- re-render on the selected row, in
empty,rowornew-rowmode.
See Mounting a widget for what mountGristWidget()
renders, and the Overview for why use this package instead of
calling grist directly.
What useGrist() returns
| Group | Properties |
|---|---|
| Connection | status, error, reload(), theme |
| Selection and mapping | record, records, mode, currentTableId, mappings, columnMappingStatus, mappedRecord, resolveMappedColumnId, mapBack |
| Writes (records) | table, getTable(id), actionStatus, actionError |
| Writes (schema) | applyActions(actions) |
| Reads | fetchRows, listTables, listColumns |
| Widget options | widgetOptions, setWidgetOptions, patchWidgetOptions |
| Linking | setCursorPosition, setLinkedRowSelection |
| Attachments / REST | getAttachmentUrl, fetchAttachmentBlob, uploadAttachment, fetchWithAuth |
See the API reference for every field.
Asking for write access and declared columns
In the widget configuration panel, the user maps these logical names
to real columns. You consume them via w.mappedRecord and
w.updateRecord(...) on writes — see Declare and map columns.
Run a write
w.updateRecord(...) writes the selected row, w.table.update([...]) many
rows as one action, and w.applyActions([...]) changes the schema.
Write data explains each.
Explore the rest of the SDK
Singleton hook
A single useGrist() handles all Grist states to avoid race conditions and code drift.
Robust handshake
Provider, boundary, and modes for clean errors and status management within your app.
Decoded values
Mapped records typed from the declaration and checked cell by cell, read-only.
Mapped columns
mappedRecord, updateRecord and addRecord under your own column names.
The catalog
Widgets built with the SDK, each with its demo in a real Grist, and its spec, code and tests.
Guide contents
| Topic | What it covers |
|---|---|
| Overview | Why use this package instead of calling grist directly |
| Declare and map columns | Naming the columns a widget needs, and reading and writing under those names |
| Read data | The section kept up to date, and other tables on demand |
| Write data | Under your names, any table, the schema |
| Widget options | Persisting per-section settings in Grist's own store |
| Attachments & REST | Attachment bytes, SQL, and the REST API via pre-authenticated fetch |
| Widget linking & theme | Driving another widget's cursor; adopting the document theme |
| Typed reads | Records typed from the declaration, checked cell by cell |
| Errors and alerts | Where a widget's problems show |
| Performance | How useGrist() re-renders, and keeping large widgets fast |
| Troubleshooting | Symptom-first fixes |
See the API reference for every exported symbol, and the Replica document for the shape a document is reduced to for tests and tooling.
Where to go next
- Overview — why it exists, and when to reach for the raw plugin API instead.
- Core concepts — the mental model behind selection modes, mappings, and the ready handshake.
- CLI reference — scaffold a new project instead of adding the SDK to an existing one.
- Emulator — test widgets without a live Grist document.