# Database API: records, filters and visitors

Read and write a Truetask database from code: the /v1/db routes, the row shape, paging, filters, visitor tokens, errors and the JavaScript and Python clients.

URL: https://docs.truetask.io/integrations/databases

The Truetask database API, under `/v1/db`, lets a website or a script read and write a database's records directly, with one route per thing you want and a flat object per row.
A **database** is a Truetask board that holds records instead of tasks: a table with the columns you define.

The rest of the [REST API](https://docs.truetask.io/integrations/api) shows a record as a task plus its custom field values plus its links, so reading one row means joining three resources.
These routes do that join for you.

| Need | Use |
| --- | --- |
| Put a customer list, a price table or a shot list on your website | A [database key](https://docs.truetask.io/boards/database-keys) and one of the clients |
| Let visitors post to a database and edit only what they posted | A key that gives visitors an identity, set to **Own rows** |
| Pull records into a script, a notebook or a DCC | The Python client |
| Read and change everything else in Truetask | The [REST API](https://docs.truetask.io/integrations/api) |

## Get a key

A board admin of the database mints a key in the database's **Share** dialog, under **API keys**, and picks what it may do to rows, whether it gives each visitor an identity, when it stops working and how many requests a minute it gets.
The key starts with `ttdb_`, is shown once beside curl, JavaScript and Python snippets for this database, and reaches that database's records and nothing else in Truetask.
A board admin's own token with write access manages keys from a script too, through `/v1/db/{database}/keys`, which a key itself can never call.
[Database API keys](https://docs.truetask.io/boards/database-keys) covers each setting, changing and revoking a key, the audit trail, and what Truetask Cloud's plans allow.

## Call it

```
Authorization: Bearer ttdb_...
```

| Route | What it does |
| --- | --- |
| `GET /v1/db/{database}` | The columns, with each one's type and options |
| `GET /v1/db/{database}/records` | One page of rows |
| `GET /v1/db/{database}/records/{id}` | One row by id |
| `POST /v1/db/{database}/records` | Insert one row |
| `PATCH /v1/db/{database}/records/{id}` | Change the columns you name |
| `DELETE /v1/db/{database}/records/{id}` | Move one row to the database's trash |

The database id is the last part of its address, `/board/<id>`, and **Connect**, beside the **API keys** heading in the database's **Share** dialog, shows it already pasted into the snippets.

The same routes also take a personal access token (`tt_`) or a signed-in session.
Then the person's role on the database decides: a board viewer reads, an editor and an admin read and write, and a guest is refused.

```sh
curl -G 'https://truetask.example.com/v1/db/8cv0b1xqk2m4z7p/records' \
  -H 'Authorization: Bearer ttdb_...' \
  --data-urlencode 'filter=Status = "Active"' \
  --data-urlencode 'sort=-Amount'
```

A list answers a page, the same shape every Truetask list answers:

```json
{ "page": 1, "perPage": 30, "totalItems": 213, "totalPages": 8, "items": [] }
```

### A row

```json
{
	"id": "rec0m3k1x7q9w2d",
	"key": "CUST-12",
	"title": "Northwind Traders",
	"created": "2026-11-10 13:24:49.408Z",
	"updated": "2026-11-10 13:24:49.408Z",
	"Status": "Active",
	"Amount": 1200.5,
	"Markets": ["EU", "US"],
	"Contact": ["rec7p2v8n4m1k6z"]
}
```

`id`, `key`, `title`, `created` and `updated` are on every row; every other name is one of your columns.
A column name is matched loosely wherever you write one, so `unit_price`, `unitprice` and `Unit price` all reach the same column.
An empty cell reads back as null.

An insert is the row itself, with `title` required and every other key a column.
Values are typed per column: a number as a number, a checkbox as a boolean, a date as `"2026-01-31"`, a select as one of its choices, a duration as `"1h 30m"`, a person column as user ids, and a relation as the ids of the records it should link to.
An update names only the columns to change, `null` clears a cell, and a relation column takes the full list of ids it should end up with.

A write goes through Truetask the way the app's own does, so the automations and webhooks a new record fires still fire.

### Paging, sorting and columns

- `page` and `per_page` walk the pages, 30 rows by default and 500 at most.
- `sort` names a column, with a leading `-` for descending, several separated by commas.
  The default is `created`, oldest first, so paging is stable.
- `fields` trims the columns a row carries, and is the only way to include a rollup or a formula.
  Those are computed per row as it is read, so a page asking for one holds at most 100 rows, and a larger `per_page` answers 400.
- `expand` resolves a relation column to the records it points at, under the row's own `expand`.

## Filtering

The filter is the grammar the rest of the API uses, over your database's own columns.

```
Status = "Active" && Amount > 10
(Stage = "Won" || Stage = "Lost") && Owner != null
`unit price` <= 3 && Notes ~ "urgent"
```

- Operators are `=`, `!=`, `>`, `>=`, `<`, `<=`, `~` (contains, case insensitive) and `!~` (does not contain).
- Combine with `&&` and `||`, and parenthesize to group.
- Values are quoted text, a number, `true`, `false`, or `null` for an empty cell.
- `id`, `title`, `created` and `updated` are always there; every other name is a column.
- A column whose name is not a plain word goes in backticks.
- A multi select, a person column or a relation matches when any of its entries does.
- A rollup or a formula cannot be filtered or sorted; read it with `fields` and compare in your own code.

A filter that cannot be read answers 400 saying what is wrong, where in the string, and which columns the database has.

> **Never paste what somebody typed into a filter**
>
> A filter is a query language, not a search box.
> Both clients bind values for you, quoted and escaped: `tt.filter('Name ~ {:q}', { q: box.value })`.

Filtering or sorting on a column reads the whole database, so a request doing it is refused past 10,000 live rows.
`id`, `title`, `created` and `updated` page without reading the table, so a plain `sort=-created` walk stays cheap at any size.

## Visitors

A key is one credential for a whole site, so every browser holding it is the same caller.
A **visitor** is an identity for one browser, minted with a key that has **Give each visitor an identity** on: your site keeps the visitor's token and sends it where the key went.
What the key's visitor settings mean, and when to choose **Own rows**, is in [Database API keys](https://docs.truetask.io/boards/database-keys#visitors).

```js
let token = localStorage.getItem('tt_visitor');
if (!token) {
	const minted = await tt.database(COMMENTS).visitors.create();
	localStorage.setItem('tt_visitor', (token = minted.token));
}

// From here on the visitor is the credential. Nothing else changes.
const comments = new Truetask(URL, token).database(COMMENTS);
await comments.create({ title: 'Nice post', Body: 'Thanks for writing this.' });
```

`visitors.create()` calls `POST /v1/db/{database}/visitors`, which answers the visitor's `id`, its `token`, its `external_id` and when it `expires`; the token is in that answer and in no other.
A visitor carries the key's rights, the key's requests a minute and the key's expiry, and lasts as many days as the key's **A visitor lasts (days)** says, counted from when it is minted.
Revoking the key, or turning its visitors off, invalidates every visitor it minted, at once.

What it adds is ownership:

- a row the visitor inserted comes back carrying `mine: true`;
- `filter=mine = true` is that visitor's own rows, which is how a page shows somebody what they wrote;
- with the key set to **Own rows**, changes and deletes reach only those rows, and another visitor's row answers 404.

A visitor cannot mint another visitor, and its token (`ttv_`) reaches nothing in Truetask but this one database's rows.
On an **Own rows** key the key itself may not change or delete rows at all: only its visitors may, and only their own.

Read with a workspace credential instead, a row a visitor inserted carries `visitor` with the visitor's `id` and `external_id`, in place of `mine`.

`DELETE /v1/db/{database}/visitors/{id}` is the "forget me" a site owes somebody who asks, callable with the visitor's own token, or with the key for any visitor it minted.
The identity goes and the rows stay, unowned, so on an **Own rows** key nobody may change them again.

> **Binding a visitor to your own user**
>
> `visitors.create({ externalId: user.id })` writes your user id beside the visitor, which your team then sees in Truetask.
> It is a label and not a proof: a key sitting in a page lets anybody send anybody's id, so bind one only when you mint from your server.

## The clients

Both wrap these routes with the method names the PocketBase SDK uses, and both bind a filter's values for you.

### JavaScript

One file, no dependencies, plain ESM, for a browser or Node 20 and newer.

```sh
npm install @truetask/db
```

```js
import Truetask from '@truetask/db';

const tt = new Truetask('https://truetask.example.com', 'ttdb_...');
const customers = tt.database('8cv0b1xqk2m4z7p');

const page = await customers.getList(1, 20, {
	filter: tt.filter('Status = {:status} && Amount > {:min}', { status: 'Active', min: 10 }),
	sort: '-Amount',
	expand: 'Contact'
});

for (const row of page.items) console.log(row.title, row.Amount);
```

`schema`, `getList`, `getFullList`, `getFirstListItem`, `getOne`, `create`, `update`, `delete` and `visitors.create`.
Everything that goes wrong rejects with one `ClientResponseError` carrying `status`, `message` and `retryAfter`, so a site has one `catch`.

The source and the full method reference are on GitHub: [truetask-io/truetask-client-js](https://github.com/truetask-io/truetask-client-js).

If the page is served by Truetask itself, pass `''` as the url and calls go to the page's own origin.

### Python

Nothing outside the standard library, Python 3.7 and later, so it also runs in the Python a DCC ships.

```sh
pip install truetask-db
```

```python
from truetask_db import Truetask

tt = Truetask("https://truetask.example.com", "ttdb_...")
customers = tt.database("8cv0b1xqk2m4z7p")

page = customers.get_list(1, 30, filter=tt.filter(
    "Status = {:status}", status="Active",
), sort="-Amount")

for row in page["items"]:
    print(row["title"], row["Amount"])
```

`schema`, `get_list`, `get_full_list`, `get_first_list_item`, `get_one`, `create`, `update`, `delete` and `visitors.create`, and one `TruetaskError` with a `status` on it.
`url` and `credential` fall back to the `TRUETASK_URL` and `TRUETASK_API_TOKEN` environment variables, so a script can carry neither.

The source and the full method reference are on GitHub: [truetask-io/truetask-client-python](https://github.com/truetask-io/truetask-client-python).

## Errors and limits

| Status | What it means |
| --- | --- |
| 400 | The filter, the sort, `fields` or `expand` cannot be read, the database is too large to filter on a column, or a page asking for a rollup or a formula is over 100 rows |
| 401 | The credential is missing, expired, revoked or not a credential |
| 403 | The credential may not do this to this database, or the Cloud plan has no database keys (`detail.feature` is `database_keys`) |
| 404 | No such database, or no such row in it |
| 409 | The database has no list to hold a record, which only a board broken outside the app can be |
| 422 | A value a column cannot hold, an unknown column, or a computed one. The message names the column |
| 429 | The key's requests a minute are spent. Wait `Retry-After` seconds |
| 503 | Something Truetask depends on is not answering |

Every answer carries the key's minute as `X-RateLimit-Limit` and `X-RateLimit-Remaining`, and a 429 carries `Retry-After` counted to the moment there is room again.
The minute belongs to the key, its visitors included.

A database a key was not minted on answers 404, the same as a database that does not exist, so a key cannot be used to find out what else you have.

> **Truetask Cloud only**
>
> On a Truetask Cloud plan without database keys, every call made with a key or a visitor answers 403 with `{"feature": "database_keys", "message": "Database API keys need a paid plan"}` as its `detail`, so a program can tell it from a missing right.
> Which plans have keys is in [Database API keys](https://docs.truetask.io/boards/database-keys).

> **Self-hosted only**
>
> `/v1` answers cross-origin requests, so a page on any address can call your instance directly.
> If your instance sits behind a proxy, make sure it passes the `Authorization` header through.

## Related

- [Database API keys](https://docs.truetask.io/boards/database-keys)
- [REST API](https://docs.truetask.io/integrations/api)
- [Databases](https://docs.truetask.io/boards/databases)
- [MCP: connect any AI tool](https://docs.truetask.io/agents/mcp)
- [Webhooks](https://docs.truetask.io/integrations/webhooks)
