Guides

File-based APIs

Export HTTP methods from api/*.ts. The filename is the path.

Each file under api/ becomes a route. api/planets.ts serves /planets. Nested folders work the same way: api/v1/items.ts serves /v1/items.

Export get, post, put, patch, or delete as defineApi(...) handlers.

api/planets.ts

import { planets } from "../database";
import { defineApi, z } from "robodev-lib";
export const get = defineApi({
query: z.object({ climate: z.string().optional() }),
response: z.array(z.object({
id: z.string(),
name: z.string(),
})),
handler: async ({ db }) => db.select().from(planets),
});
export const post = defineApi({
body: z.object({ name: z.string().min(1) }),
handler: async ({ db, body }) => {
const [row] = await db.insert(planets).values(body).returning();
return row;
},
});

Handler context

  • db — Drizzle client for the project's tenant database.
  • query — parsed query string when you pass query.
  • body — parsed JSON body when you pass body.
  • headers — incoming request headers.

Swagger

After deploy, open /docs on the project host for the generated OpenAPI. The dashboard also embeds that page under a project's Swagger tab.