API Routes

API routes

Build REST endpoints alongside your frontend pages. API routes live in src/api/ and share the same process as your pages.

File-based API routing

Like pages, API routes use the file system. A file at src/api/hello.ts becomes /api/hello.

src/api/hello.ts
import type { ApiHandler } from "@thexjs/core";export const GET: ApiHandler = ({ request }) => {  return Response.json({ message: "Hello from x!" });};

Request & response

Each exported HTTP method receives the request and returns a standard Response object. Dynamic segments work the same as pages: api/users/[id].ts/api/users/:id.

src/api/users.ts
import type { ApiHandler } from "@thexjs/core";export const GET: ApiHandler = async ({ request }) => {  const users = await db.query("SELECT * FROM users");  return Response.json(users);};export const POST: ApiHandler = async ({ request }) => {  const body = await request.json();  const result = await db.query(    "INSERT INTO users (name, email) VALUES (?, ?) RETURNING *",    [body.name, body.email]  );  return Response.json(result, { status: 201 });};

POST endpoint example

src/api/contact.ts
import type { ApiHandler } from "@thexjs/core";export const POST: ApiHandler = async ({ request }) => {  const form = await request.formData();  const email = form.get("email");  const message = form.get("message");  if (!email || !message) {    return Response.json(      { error: "Email and message are required" },      { status: 400 }    );  }  await sendEmail({ email, message });  return Response.json({ success: true });};

API route tree

API routes support the same file-tree conventions as pages — nested folders, dynamic segments, and index files.

file tree
src/api/
hello.ts -> GET /api/hello
users.ts -> GET, POST /api/users
users/
[id].ts -> GET, PUT, DELETE /api/users/:id
auth/
login.ts -> POST /api/auth/login
register.ts -> POST /api/auth/register

Process sharing

API routes run in the same Bun process as your pages and server functions. This means you can share database connections, in-memory caches, and configuration without any network overhead.