DEV Community

Alex Spinov
Alex Spinov

Posted on

Hono Has a Free API: The Ultrafast Web Framework That Runs Everywhere

Hono is an ultrafast web framework for Cloudflare Workers, Deno, Bun, Node.js, and AWS Lambda. It's 3x faster than Express, has zero dependencies, and uses Web Standard APIs.

Why Hono?

  • Universal — same code on Workers, Deno, Bun, Node, Lambda
  • Fast — 3x Express, near-raw performance on Bun
  • Tiny — 14KB min, zero dependencies
  • Type-safe — full TypeScript with route-level types
  • Web Standards — uses fetch, Request, Response APIs

Quick Start

# Create project
npm create hono@latest my-app
cd my-app
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

Basic API

import { Hono } from 'hono';

const app = new Hono();

app.get('/', (c) => c.text('Hello Hono!'));

app.get('/api/users', (c) => {
  return c.json([
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
  ]);
});

app.get('/api/users/:id', (c) => {
  const id = c.req.param('id');
  return c.json({ id, name: 'Alice' });
});

app.post('/api/users', async (c) => {
  const body = await c.req.json();
  return c.json({ id: 3, ...body }, 201);
});

export default app;
Enter fullscreen mode Exit fullscreen mode

Middleware

import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { logger } from 'hono/logger';
import { bearerAuth } from 'hono/bearer-auth';
import { compress } from 'hono/compress';
import { etag } from 'hono/etag';
import { cache } from 'hono/cache';

const app = new Hono();

// Global middleware
app.use('*', logger());
app.use('*', cors());
app.use('*', compress());
app.use('*', etag());

// Protected routes
app.use('/api/*', bearerAuth({ token: 'secret-token' }));

// Cache static routes
app.get('/static/*', cache({ cacheName: 'static', cacheControl: 'max-age=3600' }));
Enter fullscreen mode Exit fullscreen mode

Route Groups & Typing

import { Hono } from 'hono';
import { zValidator } from '@hono/zod-validator';
import { z } from 'zod';

const app = new Hono();

// Zod validation
const createUserSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
});

app.post('/api/users',
  zValidator('json', createUserSchema),
  async (c) => {
    const user = c.req.valid('json'); // Fully typed!
    return c.json({ id: 1, ...user }, 201);
  }
);

// Route groups
const api = new Hono();
api.get('/users', (c) => c.json([]));
api.get('/posts', (c) => c.json([]));

app.route('/api/v1', api);
Enter fullscreen mode Exit fullscreen mode

Deploy Anywhere

// Cloudflare Workers
export default app;

// Deno
Deno.serve(app.fetch);

// Bun
export default { port: 3000, fetch: app.fetch };

// Node.js
import { serve } from '@hono/node-server';
serve(app, (info) => console.log(`Listening on :${info.port}`));

// AWS Lambda
import { handle } from 'hono/aws-lambda';
export const handler = handle(app);
Enter fullscreen mode Exit fullscreen mode

RPC Mode (End-to-End Type Safety)

// server.ts
const routes = app
  .get('/api/users', (c) => c.json([{ id: 1, name: 'Alice' }]))
  .post('/api/users', async (c) => {
    const body = await c.req.json();
    return c.json({ id: 2, ...body }, 201);
  });

export type AppType = typeof routes;

// client.ts (browser)
import { hc } from 'hono/client';
import type { AppType } from './server';

const client = hc<AppType>('http://localhost:3000');
const res = await client.api.users.$get(); // Fully typed!
const users = await res.json(); // { id: number, name: string }[]
Enter fullscreen mode Exit fullscreen mode

Performance

Framework Requests/sec (Bun)
Hono 130K
Elysia 140K
Express 45K
Fastify 65K

Resources


Building APIs or web apps? Check my Apify actors or email spinov001@gmail.com.

Top comments (0)