Layered API architecture
On a creator commerce platform I built, the Hono API is organised in four layers:
presentation/ routes, auth middleware, validation, error handler
application/ use cases (CreateOrder, ConfirmPayment, RequestFreshLink) and ports (interfaces)
domain/ entities and rules (Order.markPaid, DownloadToken expiry and limits)
infrastructure/ Drizzle + Neon repositories, Razorpay, R2 storage, Resend email, queue
Use cases depend on interfaces, not on Razorpay or Postgres directly, so they can be unit-tested with fakes and a provider can be replaced without touching business rules.
Handling side effects safely
In payment and order code, most failures happen in side effects (writes, emails, calls to other services) rather than reads. The patterns I use:
- Webhooks are queued. Webhook routes verify and enqueue; a queue consumer does the work.
- Idempotency. Every handler checks current state before acting, so retries are safe.
- Atomic transitions. State changes such as “pending → paid” should be conditional updates, so two concurrent workers can’t both win. This is the first thing I’d tighten in my own creator-store code, where the check and the write are separate steps (see the webhooks article).
- Reconciliation. A cron job looks for inconsistent state (paid but unfulfilled) and repairs it.
Evidence
- Ekonavi: core backend on Workers and Hasura (auth, data pipelines) and the Mercado commerce API (cart, checkout, Mercado Pago, order lifecycle, shipping) documented with OpenAPI and Zod.
- Creator commerce platform: Hono on Workers with Drizzle and Neon Postgres, Razorpay, R2 private files, Cloudflare Queues with a dead-letter queue, and a six-hourly fulfilment sweep.