Skip to main content
Need the document in your own code — a custom build step, a test, a deploy script? buildOpenApiDocument builds it from a live Payload instance, and the downconverters are exported alongside it. Everything comes from the main entry point (see Exports).

Signature

The returned document already honors options.openapiVersion: '3.0' and '3.1' are downconverted before the promise resolves, so you only call toOpenApi30 / toOpenApi31 yourself when you need an extra version of a 3.2 document.

Where options comes from

buildOpenApiDocument takes resolved options — the internal shape with every default filled in — not the raw object you pass to openapi(). You never construct it by hand. The plugin resolves your options once at config build time and stashes the result on config.custom under the package name; read it from there:
This is exactly what the openapi:generate CLI does internally, and it means your script always builds with the same options the runtime endpoint uses — one source of truth.
ResolvedOptions is an internal type and is not exported. Keep the config.custom value opaque and pass it straight through to buildOpenApiDocument — its parameter type checks the shape for you.

Example script

scripts/build-spec.ts
If all you want is the document on disk, you don’t need a script — the openapi:generate CLI covers that, including one file per language with --lang all.

Downconverters

The plugin always builds 3.2 and downconverts from there — the same passes run when you set openapiVersion.

See also