Structuring a larger Hono API
Hono provides routing and middleware and leaves architecture to you. On the creator commerce API I used four layers:
| Layer | Responsibility | Example |
|---|---|---|
| Presentation | HTTP, auth middleware, request validation | POST /v1/webhooks/razorpay |
| Application | Use cases and ports (interfaces) | ConfirmPaymentUseCase, IPaymentService |
| Domain | Entities and rules | Order.markPaid(), DownloadToken expiry |
| Infrastructure | Adapters for external systems | Razorpay, Drizzle/Neon, R2, Resend |
A request-scoped middleware injects dependencies into Hono’s context, so routes call c.get("dependencies").useCases.x instead of constructing services.
Route handlers
The webhook route on that API reads the raw body, checks that the signature header exists, hands both to a use case and returns. Verification, lookup and queueing happen in the use case, where they can be unit-tested without HTTP.
webhooksRoute.post("/razorpay", async (c) => {
const rawBody = await c.req.text(); // raw body: needed for HMAC verification
const signature = c.req.header("x-razorpay-signature");
if (!signature) throw new UnauthorizedError("Missing signature header");
await c.get("dependencies").useCases.enqueuePaymentEvent.execute(rawBody, signature);
return c.json({ received: true });
});
Validation and errors
Requests are validated with @hono/zod-validator. Domain errors (NotFoundError, UnauthorizedError) are thrown from any layer and translated to HTTP status codes in one error-handling middleware, so status codes are consistent across every route.