Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Routing & Handlers/Route Groups & Scoping

Route Groups & Scoping

Organize prefixes and group middleware without hidden handlers.

Complete nested group example

index.ts
import { Volt } from 'voltts';

const app = new Volt();
app.group('/api/v1', (api) => {
  api.get('/products', () => ({ products: [] }));
  api.group('/admin', (admin) => {
    admin.use((c, next) => {
      if (c.headers.get('x-demo-key') !== 'tutorial-key') {
        return c.json({ error: 'Unauthorized' }, 401);
      }
      return next();
    });
    admin.post('/products', (c) => c.json(c.body, 201));
  });
});
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Resulting URLs

RouteAccess
GET /api/v1/productsPublic list
POST /api/v1/admin/productsRequires the demo header
bash
bun run index.ts
curl http://localhost:3000/api/v1/products
curl -i -X POST http://localhost:3000/api/v1/admin/products -H 'x-demo-key: tutorial-key' -H 'Content-Type: application/json' -d '{"title":"Coffee"}'

Scoping rules

Call use() before the routes it protects. A group with path '/' registers its prefix without a trailing slash. Groups collect routes and middleware; they do not create an isolated dependency container or per-request store.