DocsPluginsShowcaseReleases
npmStar us on GitHub

Getting Started

IntroductionInstallationProject StructureLayouts and PagesLinking and Navigating

Routing

Folder-Based RoutingFile-Based RoutingDynamic RoutesParallel RoutesIntercepting RoutesCatch-All RoutesMDX & Markdown Pages

Special Files

Loading UIError BoundariesTemplateDefaultNot Found

Data Fetching

LoadersActionsAccessing Route Data

Metadata

Metadata & SEOOpen Graph & TwitterIcons & Favicons

API Routes

API Routes OverviewPlain Function HandlersHono IntegrationDynamic API Routes

Environment Variables

OverviewPrefixes & Client ExposureUsing in API Routes

Styling

CSS OverviewTailwind CSSCSS Modules

Platforms

WebWindowsmacOSLinuxAndroidiOS

Deployment

Deployment OverviewProduction ServerStatic ExportHosting Providers

A native React framework for building cross-platform applications from a single codebase.

Resources

  • Documentation
  • Plugins
  • Showcase
  • Releases

Community

  • GitHub Discussions
  • Reddit
  • Discord
  • LinkedIn

More

  • npm
  • GitHub
  • Issues
  • License

© 2026 Bini Cooperation.

Hono Integration

Build powerful APIs with Hono in Bini.js - file-based routing, middleware, and type safety.

File-Based API Routing

Hono is a fast, lightweight web framework that works everywhere. Bini.js integrates Hono with file-based API routing - your file structure defines your API routes, and Hono routes are written without the /api prefix.

Hono is the recommended approach for complex APIs in Bini.js. It provides routing, middleware, validation, and strong TypeScript support - all with zero-config file-based routing.

Your API route is determined by the file path inside src/app/api/. The file name becomes the route segment:

app
api
ƒhello.ts
ƒusers.ts
posts
ƒ[id].ts
ƒ[...catch].ts
/api/hello
/api/users
/api/posts/:id
/api/*
File PathAPI Route
src/app/api/hello.ts/api/hello
src/app/api/users.ts/api/users
src/app/api/posts/index.ts/api/posts
src/app/api/posts/[id].ts/api/posts/:id
src/app/api/[...catch].ts/api/*
There are no root / API routes. Every API file maps to a named route based on its filename. Write your Hono routes without the /api prefix - the framework 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. Because the API layer strips /api, the route you define for /api/hello is /hello.

src/app/api/hello.ts
import { Hono } from 'hono'

const app = new Hono()

app.all('/hello', (c) => {
  return c.json({
    message: 'Hello from Bini.js!',
    timestamp: new Date().toISOString(),
    method: c.req.method,
  })
})

export default app

Routing with Hono

Hono provides a powerful routing system with path parameters, query parameters, and more. Routes are written with the /api prefix stripped.

src/app/api/users.ts
import { Hono } from 'hono'

const app = new Hono()

app.get('/users', (c) => c.json({ users: ['alice', 'bob'] }))
app.get('/users/:id', (c) => c.json({ id: c.req.param('id') }))
app.post('/users', async (c) => c.json({ created: await c.req.json() }, 201))
app.put('/users/:id', async (c) =>
  c.json({ id: c.req.param('id'), ...(await c.req.json()) })
)
app.delete('/users/:id', (c) =>
  c.json({ message: `Deleted ${c.req.param('id')}` })
)

export default app
MethodRoute PatternFull 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

Middleware

Hono has built-in middleware for common tasks. Import from the subpath:

src/app/api/secure.ts
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
import { timeout } from 'hono/timeout'

const app = new Hono()

app.use('*', cors())
app.use('*', logger())
app.use('*', timeout(5000))

app.get('/secure', (c) => c.json({ message: 'Public' }))

export default app
MiddlewareImportPurpose
corshono/corsCross-Origin Resource Sharing
loggerhono/loggerRequest logging
timeouthono/timeoutRequest timeout
secure-headershono/secure-headersCSP and security headers
pretty-jsonhono/pretty-jsonPretty JSON responses

Request Handling

Hono provides convenient methods for accessing request data:

src/app/api/echo.ts
import { Hono } from 'hono'

const app = new Hono()

app.all('/echo', async (c) => {
  const params = c.req.param()
  const query = c.req.query()
  const page = c.req.query('page')
  const userAgent = c.req.header('User-Agent')
  const body = await c.req.json().catch(() => null)

  return c.json({
    method: c.req.method,
    path: c.req.path,
    params,
    query: { page, ...query },
    headers: { userAgent },
    body,
  })
})

export default app

Validation

Validate incoming requests with Zod:

src/app/api/posts.ts
import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { z } from 'zod'

const app = new Hono()

const postSchema = z.object({
  title: z.string().min(1).max(100),
  content: z.string().min(1),
})

app.post('/posts', zValidator('json', postSchema), async (c) => {
  const body = c.req.valid('json')
  return c.json({ post: { id: Date.now(), ...body } }, 201)
})

export default app
Install zod and @hono/zod-validator for request validation with TypeScript inference.

Environment Variables

Hono passes environment variables through the c.env object. Access them directly in your handlers:

src/app/api/config.ts
import { Hono } from 'hono'

const app = new Hono()

app.get('/config', (c) => {
  const apiKey = c.env.MY_API_KEY
  const appName = c.env.APP_NAME ?? 'Bini.js'

  if (!apiKey) {
    return c.json({ error: 'Missing MY_API_KEY' }, 500)
  }

  return c.json({ appName, hasApiKey: !!apiKey })
})

export default app
On Cloudflare, secrets set via wrangler secret put are read from c.env. On Node, c.env is backed by process.env.

Error Handling

Handle errors gracefully with Hono's error handling:

src/app/api/robust.ts
import { Hono } from 'hono'

const app = new Hono()

app.onError((err, c) => {
  const isDev = c.env.NODE_ENV === 'development'
  return c.json(
    {
      error: 'Internal Server Error',
      ...(isDev && { details: err.message }),
    },
    500
  )
})

app.notFound((c) => c.json({ error: 'Not Found' }, 404))

app.get('/robust/users/:id', (c) => {
  const id = c.req.param('id')
  if (id === 'admin') {
    return c.json({ error: 'Access denied' }, 403)
  }
  return c.json({ id, name: 'John' })
})

export default app

When to Use Hono

ScenarioRecommendation
Multiple endpoints in one fileHono - Recommended
Need middleware (CORS, auth, logging)Hono - Recommended
Complex routing patternsHono - Recommended
Production APIs with many routesHono - Recommended
Single endpoint with simple logicPlain handler
Quick prototypesPlain handler
Use Hono for middleware and complex routing. Use a plain handler for a single endpoint. See the Plain Function Handlers page for per-method exports.

API Routes Overview

Files under app/api/ map to /api/*.

Plain Function Handlers

Backend endpoints with plain Request/Response handlers.

Dynamic API Routes

Path parameters and catch-all segments.

Environment Variables

Read secrets with c.env in your API routes.

Deployment

Deploy to Node, Vercel, Netlify, Cloudflare, or Deno.

Loaders

Fetch data before a route renders.

Was this helpful?
PreviousPlain Function Handlers
NextDynamic API Routes

On this page

  • File-Based API Routing
  • Basic Hono App
  • Routing with Hono
  • Middleware
  • Request Handling
  • Validation
  • Environment Variables
  • Error Handling
  • When to Use Hono
Edit this page on GitHub

Hono Integration

Build powerful APIs with Hono in Bini.js - file-based routing, middleware, and type safety.

File-Based API Routing

Hono is a fast, lightweight web framework that works everywhere. Bini.js integrates Hono with file-based API routing - your file structure defines your API routes, and Hono routes are written without the /api prefix.

Hono is the recommended approach for complex APIs in Bini.js. It provides routing, middleware, validation, and strong TypeScript support - all with zero-config file-based routing.

Your API route is determined by the file path inside src/app/api/. The file name becomes the route segment:

app
api
ƒhello.ts
ƒusers.ts
posts
ƒ[id].ts
ƒ[...catch].ts
/api/hello
/api/users
/api/posts/:id
/api/*
File PathAPI Route
src/app/api/hello.ts/api/hello
src/app/api/users.ts/api/users
src/app/api/posts/index.ts/api/posts
src/app/api/posts/[id].ts/api/posts/:id
src/app/api/[...catch].ts/api/*
There are no root / API routes. Every API file maps to a named route based on its filename. Write your Hono routes without the /api prefix - the framework 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. Because the API layer strips /api, the route you define for /api/hello is /hello.

src/app/api/hello.ts
import { Hono } from 'hono'

const app = new Hono()

app.all('/hello', (c) => {
  return c.json({
    message: 'Hello from Bini.js!',
    timestamp: new Date().toISOString(),
    method: c.req.method,
  })
})

export default app

Routing with Hono

Hono provides a powerful routing system with path parameters, query parameters, and more. Routes are written with the /api prefix stripped.

src/app/api/users.ts
import { Hono } from 'hono'

const app = new Hono()

app.get('/users', (c) => c.json({ users: ['alice', 'bob'] }))
app.get('/users/:id', (c) => c.json({ id: c.req.param('id') }))
app.post('/users', async (c) => c.json({ created: await c.req.json() }, 201))
app.put('/users/:id', async (c) =>
  c.json({ id: c.req.param('id'), ...(await c.req.json()) })
)
app.delete('/users/:id', (c) =>
  c.json({ message: `Deleted ${c.req.param('id')}` })
)

export default app
MethodRoute PatternFull 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

Middleware

Hono has built-in middleware for common tasks. Import from the subpath:

src/app/api/secure.ts
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
import { timeout } from 'hono/timeout'

const app = new Hono()

app.use('*', cors())
app.use('*', logger())
app.use('*', timeout(5000))

app.get('/secure', (c) => c.json({ message: 'Public' }))

export default app
MiddlewareImportPurpose
corshono/corsCross-Origin Resource Sharing
loggerhono/loggerRequest logging
timeouthono/timeoutRequest timeout
secure-headershono/secure-headersCSP and security headers
pretty-jsonhono/pretty-jsonPretty JSON responses

Request Handling

Hono provides convenient methods for accessing request data:

src/app/api/echo.ts
import { Hono } from 'hono'

const app = new Hono()

app.all('/echo', async (c) => {
  const params = c.req.param()
  const query = c.req.query()
  const page = c.req.query('page')
  const userAgent = c.req.header('User-Agent')
  const body = await c.req.json().catch(() => null)

  return c.json({
    method: c.req.method,
    path: c.req.path,
    params,
    query: { page, ...query },
    headers: { userAgent },
    body,
  })
})

export default app

Validation

Validate incoming requests with Zod:

src/app/api/posts.ts
import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { z } from 'zod'

const app = new Hono()

const postSchema = z.object({
  title: z.string().min(1).max(100),
  content: z.string().min(1),
})

app.post('/posts', zValidator('json', postSchema), async (c) => {
  const body = c.req.valid('json')
  return c.json({ post: { id: Date.now(), ...body } }, 201)
})

export default app
Install zod and @hono/zod-validator for request validation with TypeScript inference.

Environment Variables

Hono passes environment variables through the c.env object. Access them directly in your handlers:

src/app/api/config.ts
import { Hono } from 'hono'

const app = new Hono()

app.get('/config', (c) => {
  const apiKey = c.env.MY_API_KEY
  const appName = c.env.APP_NAME ?? 'Bini.js'

  if (!apiKey) {
    return c.json({ error: 'Missing MY_API_KEY' }, 500)
  }

  return c.json({ appName, hasApiKey: !!apiKey })
})

export default app
On Cloudflare, secrets set via wrangler secret put are read from c.env. On Node, c.env is backed by process.env.

Error Handling

Handle errors gracefully with Hono's error handling:

src/app/api/robust.ts
import { Hono } from 'hono'

const app = new Hono()

app.onError((err, c) => {
  const isDev = c.env.NODE_ENV === 'development'
  return c.json(
    {
      error: 'Internal Server Error',
      ...(isDev && { details: err.message }),
    },
    500
  )
})

app.notFound((c) => c.json({ error: 'Not Found' }, 404))

app.get('/robust/users/:id', (c) => {
  const id = c.req.param('id')
  if (id === 'admin') {
    return c.json({ error: 'Access denied' }, 403)
  }
  return c.json({ id, name: 'John' })
})

export default app

When to Use Hono

ScenarioRecommendation
Multiple endpoints in one fileHono - Recommended
Need middleware (CORS, auth, logging)Hono - Recommended
Complex routing patternsHono - Recommended
Production APIs with many routesHono - Recommended
Single endpoint with simple logicPlain handler
Quick prototypesPlain handler
Use Hono for middleware and complex routing. Use a plain handler for a single endpoint. See the Plain Function Handlers page for per-method exports.

API Routes Overview

Files under app/api/ map to /api/*.

Plain Function Handlers

Backend endpoints with plain Request/Response handlers.

Dynamic API Routes

Path parameters and catch-all segments.

Environment Variables

Read secrets with c.env in your API routes.

Deployment

Deploy to Node, Vercel, Netlify, Cloudflare, or Deno.

Loaders

Fetch data before a route renders.

Was this helpful?
PreviousPlain Function Handlers
NextDynamic API Routes

On this page

  • File-Based API Routing
  • Basic Hono App
  • Routing with Hono
  • Middleware
  • Request Handling
  • Validation
  • Environment Variables
  • Error Handling
  • When to Use Hono
Edit this page on GitHub