06-api-development

A set of coding rules for building REST APIs with Hono, a web framework for handling HTTP requests. It recommends placing route handlers directly in routes, splitting them by feature, and validating input with Zod.

In plain words
What is it for?
Use it when structuring Hono API endpoints, organizing feature-based routes, and checking request data at runtime and during TypeScript development.
Why use it?
It keeps API code easier to follow and improves confidence that incoming data has the expected shape, without adding a separate controller layer.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/dafthunk-com/dafthunk/06-api-development
Clone the repo
git clone --depth 1 https://github.com/dafthunk-com/dafthunk

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 801 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00801
Opus 5 $0.00000 $0.00400
Sonnet 5 $0.00000 $0.00160
Haiku 4.5 $0.00000 $0.00080

Measured yesterday against content hash 05365f8d0af4, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

06-api-development scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/06-api-development.mdc · 110 lines

How it starts

The opening of the file, as written. The whole thing — 110 lines — stays where its author put it; the contents beside it link to each section on GitHub.

REST API

Routing & Handler Structure

  • Define routes inline for simplicity
    Avoid traditional "controller" layers. Use route handlers directly with Hono for better type inference and simpler structure.

    app.get('/books/:id', (c) => {
      const id = c.req.param('id');
      return c.json({ id });
    });
    
  • Modularize routes by feature
    Use app.route() to separate route logic into distinct modules.

    // routes/books.ts
    import { Hono } from 'hono';
    
    const books = new Hono();
    
    books.get('/', (c) => c.json(['book1', 'book2']));
    books.get('/:id', (c) => c.json({ id: c.req.param('id') }));
    
    export default books;
    
    // main.ts
    import books from './routes/books';
    const app = new Hono();
    app.route('/books', books);
    

Type Safety & Validation with Zod

  • Use Zod for schema validation
    Combine zod and @hono/zod-validator for strict runtime and compile-time validation.

    import { z } from 'zod';
    import { zValidator } from '@hono/zod-validator';
    
    const userSchema = z.object({
      name: z.string(),
    });
    
    app.post('/users', zValidator('json', userSchema), (c) => {
      const data = c.req.valid('json');
      return c.json({ message: `Hello, ${data.name}` });
    });
    
  • Always validate request bodies, query params, and route params to prevent runtime issues and improve DX.

Middleware Usage

  • Built-in Middleware
    Use Hono's middleware for logging, CORS, etc.

    import { logger } from 'hono/logger';
    import { cors } from 'hono/cors';
    
    app.use('*', logger());
    app.use('*', cors());
    
  • Custom Middleware for JWT and API key Auth (see auth.ts)

Authorization & Organization Scoping

  • Authentication & Context Establishment:
    • Authentication is handled by custom middleware detailed in [auth.ts](mdc:apps/api/src/auth.ts).
    • This middleware supports both JWT (JSON Web Tokens) for user sessions and API Keys for programmatic access.
    • Upon successful authentication, the middleware establishes the organizationId in the request context (e.g., c.set("organizationId", ...)) based on the authenticated user or API key.

Read the full file on GitHub · 110 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 110 lines · 0 tokens per session scan A 05365f8d0af4

Subscribe to this mod's changes

06-api-development is a cursor rule published in the GitHub repository dafthunk-com/dafthunk (119 stars, last pushed 5d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 801 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.