Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

API Template

A minimal Hono API ready to deploy.

jack new my-api -t api

What You Get

  • Hono - Fast, lightweight web framework
  • TypeScript - Full type safety
  • CORS - Pre-configured for cross-origin requests
  • Health endpoint - Ready for monitoring

Project Structure

my-api/
├── src/
│   └── index.ts
├── package.json
├── tsconfig.json
├── wrangler.jsonc
└── AGENTS.md
  • src/index.ts — API routes
  • wrangler.jsonc — Deployment config
  • AGENTS.md — AI context

Example Code

The template includes working example routes:

import { Hono } from "hono";
import { cors } from "hono/cors";
 
const app = new Hono();
 
app.use("/*", cors());
 
// Root endpoint
app.get("/", (c) => {
  return c.json({ message: "Hello from my-api!" });
});
 
// Health check
app.get("/health", (c) => {
  return c.json({ status: "ok", timestamp: Date.now() });
});
 
// POST endpoint
app.post("/api/echo", async (c) => {
  const body = await c.req.json();
  return c.json({ received: body });
});
 
// URL params
app.get("/api/users/:id", (c) => {
  const id = c.req.param("id");
  return c.json({ userId: id });
});
 
export default app;

Adding Routes

Add new routes in src/index.ts:

// GET with query params
app.get("/api/search", (c) => {
  const query = c.req.query("q");
  return c.json({ results: [] });
});
 
// POST with body
app.post("/api/items", async (c) => {
  const body = await c.req.json();
  // Save to database, etc.
  return c.json({ created: body }, 201);
});
 
// Middleware
app.use("/api/admin/*", async (c, next) => {
  const token = c.req.header("Authorization");
  if (!token) return c.json({ error: "Unauthorized" }, 401);
  await next();
});

Deploying

jack ship

Your API is live. The URL is copied to your clipboard.

Adding a Database

Need persistent storage? Add a D1 database:

jack services db

Then access it in your routes:

app.get("/api/users", async (c) => {
  const { results } = await c.env.DB.prepare(
    "SELECT * FROM users"
  ).all();
  return c.json({ users: results });
});

Resources