Guides
Deploying
Run a newt backend in production, on any host that runs Bun.
Deploying a newt backend means running bun src/index.ts on a server, with the right environment variables and, on SQLite, a disk that survives restarts. There is no build step: Bun runs your TypeScript directly.
What you need
Section titled “What you need”- Bun 1.4 or later on the server, or a container image with it.
- Your project’s code and its dependencies (
bun install). - The environment variables your stores need.
- On SQLite or local files: a persistent folder for
data/.
A backend on one machine, with SQLite and local files, looks like this:
Everything under data/ lives on a persistent volume.
1. Start the backend
Section titled “1. Start the backend”The generated package.json has a start script:
"scripts": { "start": "bun src/index.ts"}On the server:
bun install --productionbun run start--production skips the dev dependencies (@newt-js/cli, lint, TypeScript): the backend does not need them to run.
On start, the backend:
- Opens each context’s database and runs its pending migrations. If one fails, the backend stops with the error and serves nothing.
- Serves HTTP on
PORT. - Starts each context’s worker and scheduled tasks.
2. Set the environment variables
Section titled “2. Set the environment variables”Bun reads .env from the folder you start in, but in production you usually set real environment variables in your host instead. A SQLite backend with local files needs:
| Variable | Example | What it is |
|---|---|---|
PORT |
3000 |
The HTTP port; 3000 if unset |
PUBLIC_URL |
https://newthub.example.com |
The address users reach |
FILES_SECRET |
64 hex characters | Signs file links |
TASKS_DATABASE |
/app/data/databases/tasks/tasks.sqlite |
One per context |
TASKS_FILES |
/app/data/files/tasks |
One per context |
PUBLIC_URLandFILES_SECRETare required with local files; the backend does not start without them.PUBLIC_URLbuilds the links to uploaded files, so it must be the public address, withhttps.FILES_SECRETmust have at least 32 characters. Generate one withopenssl rand -hex 32.TASKS_DATABASEandTASKS_FILESfall back todata/databases/tasks/tasks.sqliteanddata/files/tasks, relative to the folder you start in.
Other setups add or replace variables:
- Postgres or MySQL:
<CONTEXT>_DATABASE_URLper context, required. See Postgres and MySQL. - Files on S3:
S3_ACCESS_KEY_ID,S3_SECRET_ACCESS_KEY,S3_REGION,S3_ENDPOINT, and<CONTEXT>_S3_BUCKETper context. See Files. - Valkey cluster:
VALKEY_URL. See Running several instances.
.env.example lists every variable your project reads. Configuration and environment variables explains each one.
3. Keep data/ on a persistent disk
Section titled “3. Keep data/ on a persistent disk”With SQLite and local files, everything your backend stores lives in data/: data/databases/, a folder per context with its .sqlite file (plus its -wal and -shm files), and data/files/, a folder per context. Mount a persistent volume there.
If your host gives each deploy a fresh disk, you lose that data on every deploy. Use a volume, or move to Postgres and files on S3.
Back the folder up: SQLite shows how to copy a live database safely.
4. Check health
Section titled “4. Check health”The generated backend serves a health route:
curl https://newthub.example.com/health-check| State | Status | Body |
|---|---|---|
| Running | 200 |
{"status":"ok"} |
| Stopping | 503 |
{"status":"stopping"} |
Point your host’s or load balancer’s health check at /health-check. Each context that serves routes also answers its own, as /tasks/health-check.
5. Stop gracefully
Section titled “5. Stop gracefully”On SIGTERM or SIGINT the backend stops in order:
- The health route answers
503, and the server stops taking new connections. - Requests in progress finish, for up to 8 seconds.
- Workers finish their running jobs, for up to 8 seconds. A job that does not finish in time goes back to the queue and runs again later.
- Each context closes its database.
Your host only needs to send SIGTERM and wait about 30 seconds before it kills the process.
A minimal Dockerfile
Section titled “A minimal Dockerfile”FROM oven/bun:1WORKDIR /app
COPY package.json bun.lock ./RUN bun install --frozen-lockfile --production
COPY . .
ENV PORT=3000EXPOSE 3000VOLUME /app/data
HEALTHCHECK CMD bun -e "fetch('http://localhost:3000/health-check') \ .then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))"
CMD ["bun", "run", "start"]node_modulesdata.envBuild and run it with a named volume for data/, and the variables in a file kept on the server (production.env, outside git):
docker build -t newthub .docker run -d -p 3000:3000 -v newthub-data:/app/data \ --env-file production.env newthubBehind a reverse proxy
Section titled “Behind a reverse proxy”Most hosts put a proxy or load balancer in front of your backend, which handles HTTPS. These serve options in src/backend.ts matter then:
return runContexts(contexts, { platformWithoutContexts: platform, serve: { port, trustProxy: 1, drainMs: 5000 },})trustProxy: how many proxies stand in front of the backend. With1, newt takes the caller’s IP fromX-Forwarded-Forand the host fromX-Forwarded-Host. Without it, every request seems to come from the proxy, and rate limits by IP count everyone together.0by default.drainMs: on stop, how long to keep serving while the health route already answers503, so the load balancer stops sending traffic first.0by default.hsts:trueadds aStrict-Transport-Securityheader (one year). Use it once your site is served only over HTTPS.
A context on its own host
Section titled “A context on its own host”Each context answers under its name on the backend’s address: https://midominio.com/backoffice/api/…. Give a context a host, and it answers there instead, at its own paths:
BACKOFFICE_HOST=backoffice.midominio.com| URL | Answered by |
|---|---|
https://backoffice.midominio.com/api/orders |
backoffice’s API |
https://backoffice.midominio.com/orders/12 |
backoffice’s app |
https://backoffice.midominio.com/health-check |
backoffice’s health |
https://backoffice.midominio.com/backoffice/api/orders |
backoffice’s API too |
https://midominio.com/shop/api/products |
shop, which has no host |
https://midominio.com/backoffice/api/orders |
nothing: 404 |
https://midominio.com/health-check |
the whole backend’s health |
- At its host, a path that starts with its name keeps working, so a link built for
/backofficestill lands. On any other host, its name is a404, however the path is written. - Point the host’s DNS at the same server. Behind a proxy, set
trustProxy: newt reads the host fromX-Forwarded-Host. - Two contexts at one host stop the backend before it starts.
- With local files, its file links are signed at its host, with the protocol of
PUBLIC_URL. - Its generated app (
app/base.ts) finds its base by itself, and an app built elsewhere for/<context>/works as it is. - Point the load balancer’s health check at an address that is not a context’s host, such as the server’s own.
The generated <context>.context.ts reads the variable once, const host = Bun.env.BACKOFFICE_HOST, and hands it to its files and to run, next to its router.
A backend serves its contexts’ apps from the same process. How each one is built and served is in Adding an app.