Hono Integration
Learn how to build powerful APIs with Hono in Bini.js — file-based routing, middleware, and type safety.
Hono is a fast, lightweight web framework that works everywhere. Bini.js integrates Hono seamlessly with file-based API routing — your file structure defines your API routes.
File-Based API Routing
Your API route is determined by the file path inside src/app/api/. The file name becomes the route segment:
| File Path | API Route |
|---|---|
| src/app/api/hello.ts | /api/hello |
| src/app/api/user.ts | /api/user |
| src/app/api/posts.ts | /api/posts |
| src/app/api/posts/[id].ts | /api/posts/:id |
| src/app/api/[...catch].ts | /api/* |
/api prefix — bini-router strips it in dev/preview and mounts the app under /api in production.Basic Hono App
Create a Hono app in src/app/api/ and default export it:
Routing with Hono
Hono provides a powerful routing system with path parameters, query parameters, and more:
| Method | Route Pattern | Full URL |
|---|---|---|
| GET | /users | /api/users |
| GET | /users/:id | /api/users/123 |
| POST | /users | /api/users |
| PUT | /users/:id | /api/users/123 |
| DELETE | /users/:id | /api/users/123 |
Dynamic API Routes
Use [param] in filenames for dynamic segments:
Middleware
Hono has built-in middleware for common tasks:
| Middleware | Purpose |
|---|---|
| cors | Cross-Origin Resource Sharing |
| logger | Request logging |
| jwt | JWT authentication |
| timeout | Request timeout |
| prettyJSON | Pretty JSON responses |
Request Handling
Hono provides convenient methods for accessing request data:
Response Handling
Hono provides flexible response methods:
Validation
Validate incoming requests with Zod:
zod and @hono/zod-validator for powerful request validation with TypeScript inference.Environment Variables
Use getEnv() and requireEnv() from bini-env:
c once at the top of your handler with const ctx = c as any. Then use requireEnv(ctx, 'KEY') for required vars and getEnv(ctx, 'KEY') ?? 'default' for optional ones.Error Handling
Handle errors gracefully with Hono's error handling:
Nested Routes
Organize complex APIs with nested sub-routers:
When to Use Hono
| Scenario | Recommendation |
|---|---|
| Multiple endpoints in one file | Hono |
| Need middleware (CORS, auth, logging) | Hono |
| Complex routing patterns | Hono |
| Production APIs with many routes | Hono |
| Single endpoint with simple logic | Plain handler |
| Quick prototypes | Plain handler |