Generate TypeScript types straight from a live WordPress REST API — core endpoints plus your custom post types, fields, and taxonomies. Zero runtime dependencies.
WordPress already publishes a full JSON Schema for every resource (via an OPTIONS request to each route). Most projects still hand-write these types or reach for a heavy generic OpenAPI generator that knows nothing about WordPress. wp-rest-typegen reads the schema your site is already serving and emits clean, WP-aware TypeScript — including the view / edit / embed context model that generic tools miss.
Core routes, custom post types, register_rest_field additions, and taxonomies — pulled from the live route index.
Emit view, edit, or embed shapes. Edit-only fields like title.raw appear only where they should.
Ships as plain compiled JavaScript on Node's built-in fetch — nothing to audit, nothing to pull in.
Enums become string-literal unions; output passes tsc --strict.
# install as a dev dependency npm install --save-dev wp-rest-typegen # generate types for the wp/v2 namespace npx wp-rest-typegen https://example.com --namespace wp/v2 -o src/types/wp.d.ts
Then import the generated types in your plugin / theme / block code:
import apiFetch from '@wordpress/api-fetch'; import type { Post } from './types/wp.d.ts'; const posts = await apiFetch<Post[]>({ path: '/wp/v2/posts' }); posts[0].status; // "publish" | "draft" | ... — autocompletes & type-checks
The same resource differs by context. WordPress exposes edit-only fields (e.g. title.raw) only under edit. wp-rest-typegen honours that:
// --context view export interface Post { title: { rendered: string }; }
// --context edit export interface Post { title: { raw: string; rendered: string }; }
| Flag | Description |
|---|---|
<site-url> | Site URL (positional) or --url. |
--input <file> | Read schemas from a local JSON dump instead of HTTP. |
-o, --out | Output file (defaults to stdout). |
--context | view | edit | embed (default view). |
--include | Comma-separated rest bases to keep. |
--exclude | Comma-separated rest bases to drop. |
--namespace | Only routes in this namespace, e.g. wp/v2. |
--header k:v | Extra request header (repeatable) — e.g. for auth. |
--insecure | Skip TLS verification (self-signed certs, e.g. *.local). |
--no-registry | Omit the WPRestTypes registry interface. |
Local by Flywheel, DevKinsta and friends serve https://*.local with a self-signed certificate that Node rejects. Target the plain-HTTP URL, or pass --insecure:
npx wp-rest-typegen https://testwp.local --insecure -o src/types/wp.d.ts
import { discover, generate } from 'wp-rest-typegen'; const resources = await discover('https://example.com', { namespace: 'wp/v2' }); const dts = generate(resources, { context: 'view' });
Every run also emits a WPRestTypes interface mapping each REST base to its type — handy for generic, base-keyed fetch helpers.