1. Create Project
Terminal
mkdir my-api && cd my-api
npm init -y
npm install hono @hono/node-server
npm install -D typescript @types/node tsx tsx allows you to run TypeScript directly.
2. Write Your First API
src/index.ts
import { Hono } from 'hono';
import { serve } from '@hono/node-server';
const app = new Hono();
// Health check
app.get('/health', (c) => {
return c.json({
status: 'ok',
timestamp: new Date().toISOString(),
});
});
// Hello API
app.get('/api/hello', (c) => {
const name = c.req.query('name') || 'World';
return c.json({ message: `Hello, ${name}!` });
});
// Start server
serve({ fetch: app.fetch, port: 8000 });
console.log('🚀 Server running on http://localhost:8000');Run
Terminal
npx tsx src/index.ts
# Test
curl http://localhost:8000/health
curl http://localhost:8000/api/hello?name=Choorai3. Add CRUD API
src/index.ts
import { Hono } from 'hono';
import { serve } from '@hono/node-server';
import { cors } from 'hono/cors';
const app = new Hono();
// CORS configuration
app.use('*', cors({
origin: ['http://localhost:5173'],
credentials: true,
}));
// In-memory storage
interface Project {
id: string;
name: string;
description: string | null;
created_at: string;
}
const projects = new Map<string, Project>();
// List projects
app.get('/api/v1/projects', (c) => {
const items = Array.from(projects.values());
return c.json({ items, total: items.length });
});
// Create project
app.post('/api/v1/projects', async (c) => {
const body = await c.req.json();
const project: Project = {
id: crypto.randomUUID(),
name: body.name,
description: body.description || null,
created_at: new Date().toISOString(),
};
projects.set(project.id, project);
return c.json(project, 201);
});
// Get project
app.get('/api/v1/projects/:id', (c) => {
const project = projects.get(c.req.param('id'));
if (!project) return c.json({ error: 'Not found' }, 404);
return c.json(project);
});
// Delete project
app.delete('/api/v1/projects/:id', (c) => {
const deleted = projects.delete(c.req.param('id'));
if (!deleted) return c.json({ error: 'Not found' }, 404);
return c.body(null, 204);
});
serve({ fetch: app.fetch, port: 8000 });
console.log('🚀 Server running on http://localhost:8000');4. Deploy with Docker
Dockerfile
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 8000
CMD ["node", "dist/index.js"]Terminal
# Local test
docker build -t my-hono-api .
docker run -p 8000:8000 my-hono-api
# Cloud Run deployment
gcloud builds submit --tag gcr.io/YOUR_PROJECT/my-hono-api
gcloud run deploy my-hono-api --image gcr.io/YOUR_PROJECT/my-hono-apiFastAPI vs Hono
Hono (this page)
- ✅ Unified JavaScript/TypeScript
- ✅ Same language as frontend
- ✅ Edge Runtime support
- ⚠️ Type validation needs manual implementation
FastAPI (Lv.2)
- ✅ Auto documentation (Swagger)
- ✅ Pydantic type validation
- ✅ Python ecosystem (ML/AI)
- ⚠️ Requires learning a separate language
Full Example Code
Check out the Hono version of the B2B Admin API: examples/b2b-admin/api-hono