Functions

A function is JavaScript that runs on OpenThink's servers, inside one space. Use it for work that shouldn't happen in the browser: calling another service with a secret API key, doing something on a schedule, reacting when a table changes, or answering a webhook.

A function is an item in the tree like a page and opens at `openthink.app/n/<id>`. All of a space's functions and secrets are also listed together at `openthink.app/spaces/<space>/functions`.

Who can do what

A function runs with the **space owner's** permissions, so only the space owner can create it, edit its code, set its triggers, read its logs and manage secrets. People you share it with can read the code, and can run it if you allow them (see *Who can run it* below).

Write a function

```js

// Weekly spending report

export default async function (event, think) {

const r = await think.executeSql(

'SELECT "Name", "Amount" FROM "Expenses" WHERE "Date" >= ?',

["2026-11-01"]

);

console.log("rows", r.rows.length);

return { total: r.rows.reduce((s, x) => s + x["Amount"], 0) };

}

```

- The first `// …` line is the function's name.

- `export default` must be a function (it can be `async`).

- What you `return` must be JSON. It's the result of the run — and what a page calling the function, or a webhook caller, gets back.

- A thrown error or syntax error makes the run fail. The stack points to the line in your code as `fn.js:<line>:<column>`.

What a function gets

**`event`** describes the run:

| Field | What it holds |

|---|---|

| `trigger` | What started it: `manual`, `mcp`, `app` (an HTML page), `cron` (schedule), `webhook`, `table` (a table changed) or `call` (another function) |

| `args` | Arguments passed to the run, or `null` |

| `viewer` | Who started it — `{ id, name, email, image }`. For `app`, the person viewing the page (`null` for signed-out or uninvited visitors). `null` for schedules, webhooks and table changes |

| `table` | For table changes: `{ id, name, op, rows }`, where `op` is `add`, `update` or `delete` and `rows` are the row ids |

| `request` | For webhooks: `{ method, query, headers, body, json }` |

| `function`, `space`, `run`, `at`, `page` | The function, its space, the run id, the start time, and the HTML page that called it |

**`think`** is what the function can use:

| Use | To |

|---|---|

| `await think.executeSql(sql, params)` | Read and write the space's data tables, with the same SQL as [HTML pages](https://openthink.app/n/880ac6e69d53). Rows it adds show the function as their creator |

| `think.secrets.NAME` | Read a secret, such as an API key |

| `await think.call("Name or id", args)` | Run another function in the same space and get its result |

| `fetch(url, options)` | Call an outside API — up to 50 times per run |

| `console.log`, `info`, `warn`, `error` | Write to the run's log |

Triggers

Set triggers in the right-hand column of the function's screen.

- **Run by hand or from an AI assistant** — always available.

- **Called from an HTML page** — an HTML page in the same space runs it with `await think.call("Name", args)` and gets the return value. Anyone who can open the page can call it, including link visitors, so check `event.viewer` in the function before doing anything meant only for some people.

- **Webhook** — the function gets its own address, `https://openthink.app/api/hooks/<code>`, which accepts GET, POST, PUT, PATCH and DELETE and replies with the returned JSON (or a 500 error if the code fails). The code in the address is the only secret; generate a new one if it leaks.

- **Schedule** — a 5-field cron expression in your time zone, at most every 5 minutes. For example `0 8 * * 1-5` runs at 8:00 every weekday.

- **Table changes** — runs whenever rows are added, changed or deleted in chosen tables of the space, from anywhere: the web, apps, AI assistants, HTML pages or other functions. Saving the row never waits for the function.

Call a function from an HTML page

```js

// In the HTML page

const { total } = await think.call("Weekly spending report", { from: "2026-11-01" });

```

The function must have **Called from an HTML page** turned on. It runs with the space owner's permissions for everyone who can open the page, which is how a page can use an API key or a sealed table without exposing it. Errors arrive in the page as an `Error` whose `message` you can show; the logs are visible only to the space owner. `think.call` doesn't work in the code view or (yet) in the iPhone app.

Who can run it

Choose in **Who can run** on the Triggers card:

| Setting | Who can run it |

|---|---|

| Owner (default) | Only the space owner |

| Edit | Also people with Edit access to the function, or to a folder or space containing it |

| View | Also every signed-in person who can view the function |

People you allow get the result or the error, but they don't see the logs or secrets and can't change the code or triggers. The code still runs with the owner's permissions, and `event.viewer` tells you who they are. Signed-out visitors can't run functions this way.

Secrets

Keep API keys out of your code: add them as secrets on the function's screen, then read them with `think.secrets.NAME`.

- Secrets are read-only in code. Their values never pass through AI assistants and don't appear in logs — unless your own code prints them, so don't `console.log` a secret.

- Write `think.secrets.NAME` (or `think.secrets["NAME"]`) directly, so OpenThink can tell which secrets the code needs and warn you when one isn't set.

- An AI assistant can't see or type your secrets. If one is missing, it sends you a link that opens the secret form with the name filled in, and you paste the value yourself.

Limits

- 30 seconds per run, 10 seconds of CPU, 50 `fetch` calls.

- 60 runs a minute per function; extra runs are skipped.

- No endless loops: if function A writes to a table that starts B, and B writes to a table A listens to, A doesn't run again. A chain is at most 4 steps; blocked runs show in the log as skipped.

- The log keeps the last 200 runs, with up to 200 lines and 20,000 characters each.

With AI

Through [OpenThink MCP](https://openthink.app/n/386121a81507), an assistant can write a function, run it with sample arguments, read the result and logs, fix the code and run it again:

> Write a function that runs every Monday at 8:00, totals last week's Expenses by category and saves the result to a "Weekly report" table.

Related

- [Integrations (n8n, Make, Zapier)](https://openthink.app/n/e2b5452d5e26) — connect outside tools to your tables

- [HTML page API](https://openthink.app/n/880ac6e69d53) — `think.call` and everything else a page can do

- [Authentication, authorization & accounting](https://openthink.app/n/b78dd1e6ad82) — sealed tables and row-level permissions