Skip to content
ES

Get started

Your first project

From an empty folder to a running backend with a context, a module and its routes, on SQLite.

This page takes you from nothing to a backend that saves tasks in SQLite and answers over HTTP. It takes about ten minutes.

You need Bun 1.4.0 or newer. See installation.

Terminal window
bun create newt newthub
created newthub/package.json
created newthub/tsconfig.json
created newthub/eslint.config.ts
created newthub/AGENTS.md
created newthub/src/backend.ts
created newthub/src/stores.ts
created newthub/src/env.ts
created newthub/src/backend.test.ts
created newthub/src/contexts.ts
created newthub/src/index.ts
created newthub/src/auth.ts
created newthub/.env
created newthub/.env.example
created newthub/.gitignore
created newthub/CLAUDE.md
Next steps:
cd newthub
bun install
bun run dev

Each file is explained in project structure.

Terminal window
cd newthub
bun install
bun run dev
$ bun --watch src/index.ts
Listening on http://localhost:3000/

bun run dev restarts the backend every time you save a file. Leave it running in its own terminal.

Every backend answers GET /health-check:

Terminal window
curl localhost:3000/health-check
{"status":"ok"}

While the backend stops, it answers 503 with {"status":"stopping"}, so a load balancer stops sending it requests.

A context is one part of your app with its own database. Newthub starts with tasks:

Terminal window
bunx newt add context tasks
created src/contexts/tasks/tasks.context.ts
created src/contexts/tasks/tasks-modules.ts
created src/contexts/tasks/migrations/.gitkeep
skipped src/auth.ts (already exists)
updated .env
updated .env.example
updated src/contexts.ts
Next steps:
bun run dev

The context opens its SQLite database at data/databases/tasks/tasks.sqlite, created on the next start. .env now says so:

.env
TASKS_DATABASE=data/databases/tasks/tasks.sqlite
TASKS_FILES=data/files/tasks

See contexts and modules.

A module is one kind of thing in a context. Add tasks to the tasks context:

Terminal window
bunx newt add module tasks/task
created src/contexts/tasks/modules/task/value-objects/task-id.ts
created src/contexts/tasks/modules/task/task.schema.ts
created src/contexts/tasks/modules/task/events/task-created.ts
created src/contexts/tasks/modules/task/errors/task-not-found.ts
created src/contexts/tasks/modules/task/task.ts
created src/contexts/tasks/modules/task/task.repository.ts
created src/contexts/tasks/modules/task/task.module.ts
created src/contexts/tasks/modules/task/task.routes.ts
created src/contexts/tasks/modules/task/task.module.test.ts
created src/contexts/tasks/migrations/00001_task_up.sql
created src/contexts/tasks/migrations/00001_task_down.sql
updated src/contexts/tasks/tasks-modules.ts
Next steps:
bun test src/contexts/tasks/modules/task
its routes ask for a session: write authenticate in src/auth.ts to call them from outside the tests

The module comes with four use cases (create, get, find, paginate), a table, and three routes:

src/contexts/tasks/modules/task/task.routes.ts
export function registerTaskRoutes(
router: HttpRouter,
tasks: Task_Module,
) {
router.post('/tasks', async request =>
Response.json(await tasks.create(await bodyOf(request)), {
status: 201,
}),
)
router.get('/tasks', async request =>
Response.json(await tasks.paginate({}, pageRequestFrom(request))),
)
router.get('/tasks/:id', async request =>
Response.json(
await tasks.get(request.params.id),
),
)
return { one: router.group('/tasks/:taskId') }
}

A route’s path is the context, then /api, then the module’s plural: POST /tasks/api/tasks.

Try to create a task:

Terminal window
curl -i -X POST localhost:3000/tasks/api/tasks \
-H 'content-type: application/json' \
-d '{"name":"Write the docs"}'
HTTP/1.1 401 Unauthorized
...
{"error":"Sign in first","code":"not_signed_in"}

This is on purpose. Every route is private unless you mark it { public: true }, and a private route needs to know who is calling. src/auth.ts answers that question, and the generated one answers “nobody”:

src/auth.ts
import type { Authenticator } from '@newt-js/core/http'
export const authenticate: Authenticator = async () => null

This is the path every request to a private route takes:

alt [nobody] [a user] POST /tasks/api/tasks who is calling? null 401 not_signed_in { userId } run the route create(body) the new task 201 and the task curl tasks router authenticate task.routes.ts Task_Module

To try the API on your machine, let a fixed token from .env sign you in:

src/auth.ts
import { bearerAuthenticator } from '@newt-js/core/http'
export const authenticate = bearerAuthenticator(async token =>
token === Bun.env.DEV_TOKEN ? { userId: 'dev' } : null,
)
.env
DEV_TOKEN=let-me-in

Restart bun run dev so it reads the new .env, then call the route with the token:

Terminal window
curl -X POST localhost:3000/tasks/api/tasks \
-H 'authorization: Bearer let-me-in' \
-H 'content-type: application/json' \
-d '{"name":"Write the docs"}'
{
"id": "01a1e8be-dbae-7142-8f59-770eb529979d",
"name": "Write the docs",
"createdAt": "2026-11-17T09:03:04.622Z",
"updatedAt": "2026-11-17T09:03:04.622Z"
}

List the tasks:

Terminal window
curl localhost:3000/tasks/api/tasks -H 'authorization: Bearer let-me-in'
{
"items": [
{
"id": "01a1e8be-dbae-7142-8f59-770eb529979d",
"name": "Write the docs",
"createdAt": "2026-11-17T09:03:04.622Z",
"updatedAt": "2026-11-17T09:03:04.622Z"
}
],
"total": 1
}

Errors come back as JSON with a status, a message and a code your frontend can test:

Request Status code
no session 401 not_signed_in
{"name":" "} 400 validation
GET /tasks/api/tasks/nope 400 invalid_uuid
an id that does not exist 404 task.not_found

A validation error also lists what failed in issues. See errors.

Terminal window
bun run test
$ bun test
bun test v1.4.4
9 pass
0 fail
16 expect() calls
Ran 9 tests across 2 files.

The tests run on an SQLite database in memory with the context’s migrations, so they never touch data/. They sign in on their own, so they pass whatever src/auth.ts says. See testing.

Two more checks belong with the tests:

Terminal window
bun run typecheck # types
bun run lint # newt's rules