DEV Community

Alex Spinov
Alex Spinov

Posted on

Hono Has a Free API Framework That Runs on Every JavaScript Runtime

Hono is the ultrafast web framework that runs on Cloudflare Workers, Deno, Bun, Node.js, and AWS Lambda — with the same code. Zero dependencies, sub-millisecond routing.

What Is Hono?

Hono (meaning flame in Japanese) is a small, fast web framework built on Web Standards. Write once, deploy everywhere.

Quick Start

npm create hono@latest my-api
cd my-api && npm install && npm run dev
Enter fullscreen mode Exit fullscreen mode
import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => c.json({ message: 'Hello Hono!' }))

app.get('/users/:id', (c) => {
  const id = c.req.param('id')
  return c.json({ id, name: `User ${id}` })
})

app.post('/users', async (c) => {
  const body = await c.req.json()
  return c.json({ created: true, ...body }, 201)
})

export default app
Enter fullscreen mode Exit fullscreen mode

Built-in Middleware

import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { jwt } from 'hono/jwt'
import { logger } from 'hono/logger'
import { rateLimit } from 'hono/rate-limit'
import { cache } from 'hono/cache'
import { compress } from 'hono/compress'

const app = new Hono()

// Apply middleware
app.use('*', logger())
app.use('*', compress())
app.use('/api/*', cors({ origin: 'https://myapp.com' }))
app.use('/api/*', jwt({ secret: process.env.JWT_SECRET! }))

app.get('/api/protected', (c) => {
  const payload = c.get('jwtPayload')
  return c.json({ user: payload.sub })
})
Enter fullscreen mode Exit fullscreen mode

RPC Mode (Type-Safe Client)

// server.ts
import { Hono } from 'hono'

const app = new Hono()
  .get('/api/posts', async (c) => {
    const posts = await db.posts.findMany()
    return c.json(posts)
  })
  .post('/api/posts', async (c) => {
    const body = await c.req.json<{ title: string; content: string }>()
    const post = await db.posts.create({ data: body })
    return c.json(post, 201)
  })

export type AppType = typeof app

// client.ts — FULL TYPE SAFETY
import { hc } from 'hono/client'
import type { AppType } from './server'

const client = hc<AppType>('http://localhost:3000')

const res = await client.api.posts.$get()
const posts = await res.json() // fully typed!
Enter fullscreen mode Exit fullscreen mode

OpenAPI Integration

import { OpenAPIHono, createRoute, z } from '@hono/zod-openapi'

const app = new OpenAPIHono()

const route = createRoute({
  method: 'get',
  path: '/users/{id}',
  request: { params: z.object({ id: z.string() }) },
  responses: {
    200: {
      content: { 'application/json': { schema: z.object({ id: z.string(), name: z.string() }) } },
      description: 'User found',
    },
  },
})

app.openapi(route, (c) => {
  const { id } = c.req.valid('param')
  return c.json({ id, name: `User ${id}` })
})

// Auto-generated docs at /doc
app.doc('/doc', { openapi: '3.0.0', info: { title: 'My API', version: '1.0.0' } })
Enter fullscreen mode Exit fullscreen mode

Deploy Everywhere

# Cloudflare Workers
npx wrangler deploy

# Deno Deploy
deno deploy --project=my-api src/index.ts

# Bun
bun run src/index.ts

# Node.js
node --experimental-modules src/index.ts
Enter fullscreen mode Exit fullscreen mode

Hono vs Express vs Fastify

Feature Hono Express Fastify
Runtime All Node Node
Bundle size 14KB 200KB+ 500KB+
TypeScript Native Types pkg Built-in
RPC client Yes No No
Edge ready Yes No No
Speed Fastest Slowest Fast

Need a fast API for scraped data? Scrapfly extracts web data, Hono serves it. Email spinov001@gmail.com for custom API + scraping solutions.

Top comments (0)