一键导入
zod-openapi
Define API endpoints with Zod schemas and auto-generated OpenAPI specs using @hono/zod-openapi. Use when adding, modifying, or documenting API routes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Define API endpoints with Zod schemas and auto-generated OpenAPI specs using @hono/zod-openapi. Use when adding, modifying, or documenting API routes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Add a new CLI command to the hfs tool. Use when adding user-facing commands, subcommands, or flags to the CLI.
Create a new D1 database migration for the secret-vault. Use when adding tables, columns, or indexes to the vault's schema.
Review code changes for security issues specific to this encrypted secret vault. Use when modifying auth, encryption, scope enforcement, or any data-access paths.
Diagnose and fix issues with the secret-vault Worker, hfs CLI, D1 database, Cloudflare Access auth, and encryption. Use when something is broken, returning errors, or behaving unexpectedly.
Add a new API endpoint to the secret-vault Cloudflare Worker. Use when creating new routes, resources, or API functionality in the vault.
Write tests for the secret-vault Worker and hfs CLI. Use when adding test coverage, fixing bugs with regression tests, or validating behavior.
| name | zod-openapi |
| description | Define API endpoints with Zod schemas and auto-generated OpenAPI specs using @hono/zod-openapi. Use when adding, modifying, or documenting API routes. |
All Worker API endpoints use @hono/zod-openapi. Schemas define validation AND documentation in one place. The OpenAPI spec is auto-generated - no manual maintenance.
@hono/zod-openapi - not hono-openapi (different package)z from @hono/zod-openapi, not from zod directlyOpenAPIHono instead of Hono for routers that have OpenAPI routesHono is fine for routers that only use middleware (no OpenAPI routes)schemas-*.ts file: schemas.ts (common), schemas-secrets.ts, schemas-tokens.ts, schemas-rbac.ts.openapi("SchemaName") on response schemas for spec readability.openapi({ example: ... }) on fields where the example aids understandingz.infer<>z.string().openapi({ param: { name: "key", in: "path" } })z.string().optional().openapi({ param: { name: "limit", in: "query" } })createRoute() - includes method, path, schemas, and descriptionsapp.openapi(route, handler) - never app.get() for API routestags: ["Secrets"]c.req.json() - Zod validates automatically via c.req.valid("json")c.req.valid("param")c.json({ error: "message" }, status) - Zod handles input validation, you handle business logic errorsdefaultHook on OpenAPIHono to return { error: "message" } format on validation failuresapp.doc("/doc", { ... }) - auto-generated from all registered routesinfo, security schemes, and tagsSee route patterns for complete examples. See schema patterns for Zod schema conventions.
// schemas.ts
import { z } from "@hono/zod-openapi";
export const ErrorSchema = z.object({
error: z.string(),
}).openapi("Error");
export const SecretSchema = z.object({
key: z.string().openapi({ example: "api-key" }),
value: z.string().openapi({ example: "sk-..." }),
description: z.string(),
created_at: z.string(),
updated_at: z.string(),
}).openapi("Secret");
export const KeyParam = z.object({
key: z.string().openapi({ param: { name: "key", in: "path" } }),
});
// routes/secrets.ts
import { createRoute } from "@hono/zod-openapi";
import { OpenAPIHono } from "@hono/zod-openapi";
import { ErrorSchema, KeyParam, SecretSchema } from "../schemas-secrets.js";
const app = new OpenAPIHono<HonoEnv>();
const getRoute = createRoute({
method: "get",
path: "/{key}",
tags: ["Secrets"],
request: { params: KeyParam },
responses: {
200: { content: { "application/json": { schema: SecretSchema } }, description: "Secret" },
404: { content: { "application/json": { schema: ErrorSchema } }, description: "Not found" },
},
});
app.openapi(getRoute, async (c) => {
const { key } = c.req.valid("param");
// ... handler logic, return c.json(...)
});
// index.ts
import { OpenAPIHono } from "@hono/zod-openapi";
const app = new OpenAPIHono<HonoEnv>({
defaultHook: (result, c) => {
if (!result.success) {
return c.json({ error: result.error.issues[0].message }, 400);
}
},
});
app.doc("/doc", {
openapi: "3.0.0",
info: { title: "Secret Vault API", version: "1.0.0" },
});
schemas.ts with .openapi("Name")createRoute() including all response codesc.req.valid("json") / c.req.valid("param") - not c.req.json()tags: ["Secrets"]ErrorSchema for consistency/doc includes the new route| Pattern | Why | Instead |
|---|---|---|
import { z } from "zod" | Missing .openapi() method | import { z } from "@hono/zod-openapi" |
app.get("/path", handler) | Skips OpenAPI spec | app.openapi(route, handler) |
await c.req.json() | Bypasses Zod validation | c.req.valid("json") |
decodeURIComponent(c.req.param("key")) | Manual, no validation | c.req.valid("param").key |
Manual if (!body.value) checks | Duplicates Zod schema | Define as z.string().min(1) |
Schemas without .openapi("Name") | Anonymous in spec | Always name response schemas |
import { Hono } from "hono" for API routes | No OpenAPI support | import { OpenAPIHono } from "@hono/zod-openapi" |