HTML page API
Reference for the `think` object available to every [HTML page](https://openthink.app/n/4727d3d3ab91).
think.executeSql — read and write tables
```js
const { rows } = await think.executeSql(
'SELECT _id, "Name", "Amount" FROM "Expenses" WHERE "Amount" > ?',
[100000]
);
```
- Runs SQLite over the data tables in the **same space** as the page. `SELECT` reads; `INSERT`, `UPDATE` and `DELETE` really change rows, and every place showing the table updates.
- Refer to tables and columns by **name in double quotes**. Every table has a hidden `_id` column for updating or deleting the right row.
- Parameters: an array for `?`, or an object for `:name`. Always pass user input as parameters — never build SQL strings from it.
- Values: Date is `'yyyy-mm-dd'`, Checkbox is `0`/`1`, Select is the option name (a new name adds the option), Multi-select is names joined by `", "`, Number is a plain number.
- Returns `{ columns, rows, values, inserted, updated, deleted, changes }`.
- Several statements can be sent in one call separated by `;`, but parameters only work with a single statement. Reads run in parallel; writes run in order.
- You can't create, drop or change columns with SQL.
**Who can write:** people with Edit access to the table, or people with a [row-level permission](https://openthink.app/n/26c72c31ab9c) on it. With row-level permissions, tables gain read-only `_by` (creator's id) and `_by_name` columns, and OpenThink only returns the rows the rule allows — no `WHERE _by = …` needed. Writing requires sign-in, and is limited to 60 rows a minute per person.
**Live updates:** when a table changes elsewhere — another tab, another person, an AI assistant — the page receives a `think:data` event (`event.detail.table` is the table name). Re-query and redraw:
```js
addEventListener("think:data", draw);
```
think.call — run a function on the server
Run a [function](https://openthink.app/n/0535ef448521) in the same space and get back what it returns. Use it for anything that mustn't be visible in the page's code: an API key, a sealed table, or work done on the space owner's behalf.
```js
const result = await think.call("Weekly spending report", { from: "2026-11-01" });
```
- The function must have **Called from an HTML page** turned on; otherwise the promise is rejected with a message.
- The function runs with the space owner's permissions for **everyone** who can open the page, link visitors included. Check who is calling inside the function with `event.viewer` (`null` for visitors who aren't known), not in the page.
- `args` must be JSON. An error in the function becomes an `Error` in the page (`e.message`); logs are visible only to the space owner.
- Doesn't run in the code view (`/n/<id>/code`), and isn't in the iPhone app yet.
think.params — state in the URL
Keep the page's state (selected tab, year, typed numbers) in the URL, so reloads keep it and shared links open the same view.
```js
const tab = think.params.get("tab") || "total";
think.setParams({ tab: "month" }, { push: true }); // push: Back can return
think.setParams({ year: null }); // null or "" removes it
addEventListener("think:params", draw); // Back / Forward
```
Limits: names of letters, digits, `_` and `-` up to 32 characters; values up to 500 characters; up to 20 parameters. Don't use `history.pushState` or `location.hash` — they have no effect. `<a href="#section">` links scroll within the page.
think.state — live state shared with your AI assistant
A JSON object per page **per viewer**, stored on OpenThink's server. All of that person's tabs and their AI assistant read and write the same state — for example, the user plays a chess move and the assistant sees the new position; the assistant sets a position and the board on screen changes.
```js
draw(think.state);
think.setState({ fen, lastMove: "e2e4" }); // shallow merge; null removes a key
addEventListener("think:state", (e) => draw(e.detail.state)); // e.detail.by: "mcp" or "web"
```
- Up to 16,000 characters in total.
- On load, `think.state` may be empty for a moment before the saved state arrives in a `think:state` event. Only call `setState` in response to the viewer's actions — writing defaults on startup would overwrite the saved state.
- Not saved to the server for signed-out viewers, fixed versions, the code view or the iPhone app.
Which to use: links → `think.params`; long-term, shared or historical data → tables; what's happening on one person's screen right now → `think.state`.
think.capabilities and think.role — edit or view?
Shared links open the same page for visitors, so hide editing tools from people who can't edit.
```js
const canEdit = think.capabilities.canEdit;
document.documentElement.classList.toggle("read-only", !canEdit);
```
- `canEdit` is `false` for View and Comment roles, link visitors, trashed pages and fixed versions.
- `think.role` is `"owner"`, `"edit"`, `"comment"` or `"view"`.
- Keep view-only features (zoom, pan, filters). Don't draw your own "View only" label — OpenThink already shows one. The real permission check happens on OpenThink's server.
think.viewer — who is viewing
`think.viewer` is `{ id, name, email, image }` or `null`. `id` is the person's OpenThink account id and never changes.
It's available for the owner, for anyone the owner has invited by email, and for people who agreed to tell this page who they are. It's `null` for signed-out visitors and uninvited link visitors — your page must work when it's `null`. If someone shares only their name, `email` is `null`, so filter by `id`.
think.requestViewer() — ask who's viewing
```js
button.onclick = () => think.requestViewer().then(draw); // or { email: true }
addEventListener("think:viewer", draw);
```
Shows "Let this page know who you are?" to a signed-in viewer. Call it only from a button click. OpenThink remembers the choice per page; viewers can revoke it from the page menu. No code needed: the owner can turn on **Ask viewers for their name and picture** in the Share dialog.
think.signedIn and think.signIn()
```js
loginButton.hidden = think.signedIn;
loginButton.onclick = () => think.signIn();
```
`signIn()` takes the tab to OpenThink sign-in and returns to the same page. Call it from a button click. Don't open the sign-in page in a new tab — the original tab would stay signed out. If `signedIn` is `true` but `viewer` is `null`, signing in again won't help: offer `requestViewer()` instead.
think.format and think.parse — numbers
Format numbers by each **viewer's** number-format setting (1.234,5 or 1,234.5):
```js
think.format(1250000, { currency: "VND" });
think.parse("1.250,5");
addEventListener("think:locale", redraw);
```
Options: `currency` (`"VND"`, `"USD"`, `"EUR"`), `percent`, `digits`, `compact`. `toLocaleString()` follows the browser, not the viewer's setting.
Full-screen exit button
```js
think.setExitButton("manual");
menu.onclick = () => think.showExitButton(); // or think.hideExitButton()
```
In `"manual"` mode, give viewers their own way to show the button, or on a phone they can only press Back.
Loading bar
```js
think.ready(); // main content is ready — finish the bar now
think.setLoading("manual"); // you draw your own loading screen
```
Load several tables at once with `Promise.all` rather than awaiting each query.
iPhone app
Check that newer functions exist before calling them, for example `typeof think.signIn === "function"`. In the app, `think.signedIn` is always `true`, and HTML pages embedded in text pages get `viewer` as `null`.