Skip to main content

Build a documented / type-safe API with hono, drizzle, zod, OpenAPI and scalar

In this video CJ shows how to use hono and @hono/zod-openapi to build a fully type-safe API documented with an OpenAPI specification including interactive documentation with scalar. He also shows hono best practices and shows some things you should do / setup in every hono app you create. He also shows how to use a drizzle schema as the single source of truth for type safety and documentation throughout your application. He also shows how to use vitest to write typesafe tests for your API.

View the code here: https://github.com/w3cj/hono-open-api-starter View the stoker utilities / helpers here: https://github.com/w3cj/stoker


00:00 Intro
01:10 Quick Code Tour
02:51 stoker - a hono library
03:19 hono-open-api-starter
04:00 generate hono app
04:50 eslint / editor settings setup
07:11 typescript import alias
07:41 install @hono/zod-openapi
08:10 extract hono app to own file
09:43 notFound handler
11:30 onError handler
13:08 logger middleware
13:52 structured logging with pino and hono-pino
15:46 hono-pino pretty logs and customization
20:23 typesafe hono AppBindings
21:56 pino log level setting
23:35 dotenv setup
25:30 typesafe env with zod
32:25 serveEmojiFavicon middleware
33:33 clean up app creation
35:36 hono strict mode
36:16 configure openapi doc endpoint
38:36 createRouter helper
39:33 create index route
42:42 mount index router on application
44:02 open api overview
45:00 scalar documentation setup
46:30 package.json type module
47:00 scalar customization
49:21 OpenAPIHono defaultHook
50:48 stoker openapi helpers
54:01 group route definitions with tags
54:34 tasks router setup
55:32 tasks list route
57:22 tasks list handler
57:45 RouteHandler type setup
59:48 AppRouteHandler custom type
01:01:21 register tasks list route and handler
01:02:32 tasks route tags
01:02:55 drizzle setup
01:03:50 DATABASE_URL env setup
01:04:52 zod superRefine for better errors
01:05:42 setup drizzle schema
01:08:49 drizzle-kit config setup
01:09:59 generate drizzle migrations
01:10:51 using drizzle-kit studio
01:11:27 update schema date logic
01:12:44 query db from tasks list handler
01:14:28 drizzle-zod setup
01:16:06 tasks create route
01:20:01 tasks create handler
01:25:06 stoker createErrorSchema helper
01:27:46 customize drizzle-zod schema
01:29:37 tasks getOne route
01:33:17 tasks getOne handler
01:38:37 tasks patch route
01:44:08 tasks patch handler
01:47:41 tasks remove route
01:49:17 tasks remove handler
01:51:15 test setup / helpers with vitest
01:59:23 typed test client helper
02:05:33 full test setup walkthrough
02:07:20 hono RPC client type
02:09:57 hono client example
02:11:03 open api client generators
02:11:40 runtime and deployment considerations
02:12:17 Thanks!

hono | https://hono.dev/ @hono/zod-openapi | https://github.com/honojs/middleware/tree/main/packages/zod-openapi scalar | https://scalar.com/ What is Open API? | https://swagger.io/docs/specification/v3_0/about/ stoker | https://www.npmjs.com/package/stoker zod | https://zod.dev/ @antfu/eslint-config | https://github.com/antfu/eslint-config pino | https://getpino.io hono-pino | https://www.npmjs.com/package/hono-pino pino-pretty | https://www.npmjs.com/package/pino-pretty dotenv | https://www.npmjs.com/package/dotenv dotenv-expand | https://github.com/motdotla/dotenv-expand @scalar/hono-api-reference | https://github.com/scalar/scalar/tree/main/packages/hono-api-reference drizzle | https://orm.drizzle.team/ drizzle-zod | https://orm.drizzle.team/docs/zod zod-to-openapi | https://github.com/asteasolutions/zod-to-openapi OpenAPI Generator | https://github.com/OpenAPITools/openapi-generator