Skip to Content
hono-bento

Add an endpoint

Add a route to an existing feature, touching one schema file and one feature folder.

This guide adds POST /v1/todos/:id/archive to the todos feature.

Or generate it

hono-bento add endpoint adds the contract, the handler file and the route, and an it.todo in the feature's test:

bunx hono-bento add endpoint todos archive   # POST /:id/archive; --method and --path change it

The handler returns 501 until you write it, so the schema, store and test steps below are still yours. It inserts into spec.ts without reformatting it. If the file isn't in the usual shape, it prints what to paste instead. The CLI has its options.

Add or extend the schemas

If the endpoint takes a body or returns a new shape, describe it in the feature's schema module. This one returns a todo, so add an archived field to the existing schema:

packages/schemas/src/todos.ts
export const todoSchema = z
  .object({
    object: z.literal('todo'),
    id: z.string(),
    title: z.string(),
    done: z.boolean(),
    archived: z.boolean().openapi({ description: 'Whether the todo has been archived.' }), 
  })
  .openapi('Todo');

Store it

The schema is the wire shape; storage is separate. Add the column to the Prisma model in packages/db/prisma/schema.prisma:

packages/db/prisma/schema.prisma
model Todo {
  // ...
  archived  Boolean  @default(false) 
}

Create the migration, and commit the file it writes under packages/db/prisma/migrations/:

bun run db:migrate:dev --name archive-todos

Then map the column in the feature's store.ts (archived: row.archived in toTodo) and add the query:

apps/api/src/features/todos/store.ts
  async archive(db: Database, id: string): Promise<Todo> {
    return toTodo(await db.todo.update({ where: { id }, data: { archived: true } }));
  },

Add the contract

Add an archive entry to contracts in the feature's spec.ts:

apps/api/src/features/todos/spec.ts
export const contracts = {
  // ...the existing contracts
  archive: contract({
    operation: 'archive',
    method: 'post',
    path: '/:id/archive',
    summary: 'Archive a todo',
    auth: { scopes: ['todos.write'] },
    responses: { 200: { description: 'The archived todo.', schema: todoSchema } },
  }),
};

Write the handler

The file is named after the operation, archive, and the handler takes its types from the contract, so params and respond are typed with nothing restated:

apps/api/src/features/todos/archive.ts
import { defineHandler } from 'hono-bento';

import type { Session } from '../../auth';
import type { contracts } from './spec';
import { store } from './store';

// A missing todo needs no check here: Prisma throws, and `mapPrismaError` answers 404.
export const archiveTodo = defineHandler<Session, typeof contracts.archive>(async ({ deps, params, respond }) =>
  respond(200, await store.archive(deps.db, params.id))
);

Add the route to the spec

apps/api/src/features/todos/spec.ts
import { archiveTodo } from './archive'; 

// ...
  routes: [
    // ...
    route({ ...contracts.archive, handler: archiveTodo }), 
  ],

Routing and OpenAPI pick the route up from here. Its operation id is todos_archive.

Test it and regenerate the docs

Add a test to todos.test.ts (see Testing), then:

bun run openapi   # regenerate openapi.json and commit it
bun run test

New scopes

If the route needs a scope no role has yet, add it to your authorizer's role table in auth.ts. Otherwise every caller gets 403 forbidden.

On this page