Helper Functions
Helper functions for working with Hono OpenAPI
Hide Routes
You can conditionally hide routes from the OpenAPI specification by using the hide
property in the describeRoute
function. This is useful when you want to hide certain routes in production or based on some environment variable.
app.get(
"/",
describeRoute({
// ...
hide: process.env.NODE_ENV === "production",
}),
(c) => {
return c.text("Private Route");
}
);
You can also pass a function to the hide
property which will be called with the context object.
app.get(
"/",
describeRoute({
hide: ({ c, path, method }) => c.env.NODE_ENV === "production",
}),
);
Note
The c
object is the context object which you passed in openAPIRouteHandler
or generateSpecs
function. The path
and method
properties are the path and method of the current route. This is useful when you want to globally declare the function to hide routes.
OperationId
Out of the box, the describeRoute
function will generate a unique operationId for each route. This is useful for generating the OpenAPI specification. However, in some cases, you might want to manually set the operationId for a route. You can do this by passing the operationId
property to the describeRoute
function.
app.get(
"/",
describeRoute({
operationId: "getHello",
}),
);
You can also pass a function to the operationId
property which will be called with the route object.
app.get(
"/",
describeRoute({
operationId: (route) => `${route.method}-${route.path}`,
}),
);
Vendor Loading
In v1, we added support for loadVendor
function, which you can use for -
- Loading other vendor libraries, which support Standard Schema
- Implement custom parsing/processing logic for supported vendors
import { z } from "zod/v4";
import { toJSONSchema } from "zod/v4/core";
import { loadVendor } from "hono-openapi"
import { convertToOpenAPISchema } from "@standard-community/standard-openapi/convert";
loadVendor("zod", {
toOpenAPISchema: (schema, context) => {
return convertToOpenAPISchema(toJSONSchema(schema, {
io: "input"
}), context);
},
});
The vendor name used here comes from the Standard Schema library's vendor name, which you can find by 'schema["~standard"].vendor'
where schema
is the Standard Schema library's schema object.
As the hono-openapi
uses @standard-community/standard-openapi
and @standard-community/standard-json
under the hood, they expose the currently used functions for transforming the schemas. You can use them and either create a wrapper function or create one from scratch.