Building APIs with Hono on Cloudflare Workers
In the world of edge computing, speed and simplicity are everything. Hono — a small, fast, and ultralight web framework — paired with Cloudflare Workers has quickly become one of my favourite stacks for building robust APIs that run at the edge, literally everywhere on the planet.
Why Hono?
When I first started exploring edge-compatible frameworks, I was looking for something that felt familiar (Express-like routing) but was purpose-built for modern runtimes like Cloudflare Workers, Bun, and Deno. Hono checks every box:
- Ultra-lightweight: No unnecessary bloat. Hono's core is tiny.
- Blazing-fast router: Uses a Trie-based router (RegExpRouter) for optimal performance.
- First-class TypeScript support: Excellent type inference out of the box.
- Multi-runtime: Runs seamlessly on Cloudflare Workers, Bun, Node.js, and Deno.
Setting Up Your First Hono Worker
Getting started is refreshingly simple. Here's the core of a Hono API on Cloudflare Workers:
import { Hono } from 'hono';
import { cors } from 'hono/cors';
const app = new Hono<{ Bindings: Env }>();
app.use('*', cors({ origin: ['https://yourdomain.com'] }));
app.get('/api/health', (c) => c.json({ status: 'ok' }));
app.post('/api/contact', async (c) => {
const body = await c.req.json();
// Process contact form...
return c.json({ message: 'Received!' }, 200);
});
export default app;
Type-Safe Bindings
One of the standout features is how cleanly Hono integrates with Cloudflare's environment bindings. By passing a generic Bindings type, you get full type safety on your env object:
interface Env {
RESEND_API_KEY: string;
DB: D1Database;
}
const app = new Hono<{ Bindings: Env }>();
app.get('/users', async (c) => {
const users = await c.env.DB.prepare('SELECT * FROM users').all();
return c.json(users.results);
});
Final Thoughts
If you're building any kind of API that needs to be globally distributed, fast, and maintainable, the Hono + Cloudflare Workers combination is exceptional. It's the stack powering the API layer of this very portfolio, handling everything from contact forms to newsletter subscriptions.
Give it a try — you won't look back.
