---
title: "Recipe: your own REST server"
description: "The REST starter creates a shared-list app and a Bun server."
modificationDate: "2026-11-18"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: your own REST server

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/rest-backend: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

The REST starter creates a shared-list app and a Bun server:

```prompt Add my own REST server
Give the Exact app in this folder its own REST server, written in Bun. Use https://github.com/expo/exact/blob/main/docs/recipes/rest-backend.md as the source for each step. The Exact checkout has the same page at docs/recipes/rest-backend.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. Ask me whether to start from the REST starter or to connect a server I already have. For the starter, run `bun exact.mjs pattern rest`: it writes the Contract app and the Bun server together. If it refuses a file I edited, tell me; do not add `--force` without asking.
3. Put the server where `test` and `verify` find it: `server.ts`, `server/index.ts` or `server/server.ts`, or the entry `"commands": { "server": [...] }` names in app.json. Its address is a setting's local value, else the first `http://127.0.0.1:<port>` in app.ts or an app-local module it imports transitively. Its database is the path in `DB` (`new Database(process.env.DB ?? 'data.db')`); a server with no database needs `"server": { "database": false }`.
4. If the app must reach a deployed API, follow docs/recipes/rest-backend.md: declare `"env": ["API_BASE_URL"]` in app.json, and read `const API = import.meta.env.API_BASE_URL ?? 'http://127.0.0.1:<port>'` in app.ts or an app-local module it imports transitively. Use the local server's address as the default. The value is public: never put a secret there.
5. Validate each read in the data source before it returns. Return a shaped reply for each refused write (for example, a duplicate), so `then` keeps the form open and shows the reason.
6. The starter's `persona` values are public development identities, not sign-in. If the app needs real authentication, tell me: the session helper is the app's to write, and Exact does not provide it.
7. In app.test.contract, add a test for each read and write, and one for each refusal. `request POST "/<path>" {…} as <persona> expect <status>` writes through the server as another person.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. They start the server on an empty database and stop it: start none of your own. If a dev server is on that port, they stop and name its pid: tell me, and stop it only if you started it. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me the server's entry, address and database, what the tests and verify reported, and the launch command for each target platform.
```

```sh
bun exact.mjs pattern rest
```

The [REST starter](https://github.com/expo/exact/blob/41ded8c5ede0b2959ef0daa83b1ff78545dd4428/docs/patterns/rest/app.contract) writes the Contract app and
Bun server together. Its data source validates read responses before returning
them. When a read fails because of a transport error, an HTTP error or an invalid
response, Exact keeps the prior answer and exposes the failure. The view shows
that answer only when `lib.owner == persona`; otherwise, it shows loading or
retry state. A validated empty list is a successful answer.

The starter's writes return shaped replies for operation refusals, such as a
duplicate title, so `then` can keep the form open and display the reason. Its
mutation helper also converts transport failures to shaped replies. If a source
lets a transport rejection escape instead, the mutation gets no new reply, keeps
its previous value, and does not run `then`; `failure(mutation)` explains the
request failure.

The sample `persona` values are public development identities. A real app should
put a stable, non-secret principal id in the resource call and keep bearer
credentials in the granted store on native hosts. On the web, use a server-set
HttpOnly, Secure cookie. Select the transport inside the data source with
`exactHost()`, as [web sessions](https://exact.expo.dev/recipes/web-sessions.md) shows; one deploy serves both
hosts. Await an app-owned current-session helper inside the data source before
issuing authenticated requests, then verify that the session authorizes the
principal named by the resource. Exact does not
provide that session helper. This starter demonstrates owner gating, not a full
authentication lifecycle; a real authenticated view must also gate retained
answers by the app-owned authorized principal and auth generation.

## Where the server lives

`test`, `verify` and `agent --test` start the server themselves, empty for each test,
and stop it after; start none of your own for them. They find it this way:

- **Its entry:** `server.ts`, `server/index.ts` or `server/server.ts`, or the one `app.json` names:
  `"commands": { "server": ["bun", "server/main.ts"] }`. Another program runs as written
  (`["python3", "server.py"]`), told `DB` and `PORT` as Bun's is; its first argument that is a file
  of the app is the entry checked for `DB`. Arguments after the entry are passed through in order.
- **Its address:** a local setting read by `app.ts`, else the first local setting in its
  transitive imports, walked depth-first in source order, each module before its imports,
  else the first literal `http://127.0.0.1:<port>` in that order. Settings are inlined at build.
  Different local settings in the first module are ambiguous: keep the server's setting
  in `app.ts` and other clients' settings in their modules.
  Type-only imports and declarations supply no settings or addresses.
  Discovery reads the build's record of its modules and the names its resolutions would
  have chosen: `test` and `verify` build first, as a wrapper using
  `withAppServer` must, and a newly added preferred extension needs a rebuild.
  To use another port, change the setting; the server receives the same value.
  An origin fixed at build (a deployed API's) is a setting: `"env": ["API_BASE_URL"]` in `app.json`,
  `const API = import.meta.env.API_BASE_URL ?? 'http://127.0.0.1:8080'` in a top-level const of
  `app.ts` or an app-local module it imports transitively. Its value comes from the environment,
  `.env.local` or `.env`, in that order. Keep the local server's address as the default.
  The value ships on every host, so a secret does not belong there.
- **Its database:** the path in `DB`, which the entry or a module it imports reads:
  `new Database(process.env.DB ?? 'data.db')`.
  If the server keeps no database, set `"server": { "database": false }` beside
  `commands` to omit `DB` and skip scratch database creation and removal; otherwise
  an entry that does not read `DB` is refused.

Each test starts with empty client storage and an empty server: between tests the process
restarts and its scratch database is removed. `database: false` restarts too, clearing memory.
`reload` and `request` within a test keep its data. A stop ends every process the server started,
workers included, before the database is removed; one it cannot end fails the run and
keeps the database, saying what is left. A run killed outright, its whole process group included, still ends its server. On macOS a process that leaves the server's process group on purpose (`setsid`,
as a daemon does) is not followed: it is left running, and the run fails while it holds the server's
port. `verify` reruns its chosen test empty before relaunching on the store it leaves, with that
server still running.
Remote servers stay shared. Only `test` and `agent --test` share an ancestor's server
(an older `api-test` wrapper's), saying so; `verify` refuses an answering local server,
including an ancestor's.
Snapback’s `fresh` remains opt-in.

A dev server already on that port makes them stop and name its pid: stop it first.
