Skip to content
RustPress

Documentation Build

Headless and REST API

Use RustPress headless with Next.js, Astro or SvelteKit: the WordPress-shaped REST API, auth, headless mode, CORS, signed webhooks and previews.

Updated

This page explains the RustPress REST API and how to run RustPress headless, with your public site built in Next.js, Astro, SvelteKit or an app.

The REST API

The API lives at /rp-json/wp/v2/ and covers posts, pages, media, categories, tags, menus, comments, users, search and custom types. It uses WordPress’s shapes, so WordPress headless libraries work with it. That includes the same parameters and response fields, _embed, _fields, _envelope and the X-WP-Total header.

Public content needs no key. Drafts, private content and writing need an application password, kept on your front end’s server and never in the browser.

Authentication

MethodHow
CookieLogged-in cookie plus an X-WP-Nonce header
Application passwordsHTTP Basic
Plugin tokensBearer

Headless mode

Turn it on under Settings › Headless. In headless mode:

  • The theme front end is off.
  • Visitors are redirected to the same address on your front end.
  • Permalinks, sitemaps and View links point to your front end.
  • Media stays on RustPress.

CORS

List the exact origins that may call the API. Cross-origin requests carry no cookies.

Webhooks

Changes to posts, pages, terms, menus, comments and settings are POSTed to your front end in batches, so it can rebuild only what changed. Each request is signed:

X-RP-Signature: sha256=HMAC_SHA256(secret, X-RP-Timestamp + "." + body)

A Next.js route that checks the signature and the timestamp, then revalidates the changed pages:

// app/api/revalidate/route.ts
import crypto from 'node:crypto';
import { revalidatePath } from 'next/cache';
export async function POST(req: Request) {
  const body = await req.text();
  const ts = req.headers.get('x-rp-timestamp') ?? '';
  const sig = req.headers.get('x-rp-signature') ?? '';
  const want = 'sha256=' + crypto.createHmac('sha256', process.env.RP_WEBHOOK_SECRET!).update(`${ts}.${body}`).digest('hex');
  const fresh = Math.abs(Date.now() / 1000 - Number(ts)) < 300;
  if (!fresh || sig.length !== want.length || !crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(want))) {
    return new Response('bad signature', { status: 401 });
  }
  for (const e of JSON.parse(body).events) if (e.link) revalidatePath(new URL(e.link).pathname);
  revalidatePath('/');
  return Response.json({ ok: true });
}

Previews

Preview on a draft opens <front end>/api/preview?id=&type=&token=. The token reads that one draft for 10 minutes:

GET /rp-json/wp/v2/<type>/<id>?preview_token=<token>

A Next.js preview route:

// app/api/preview/route.ts
import { draftMode } from 'next/headers';
import { redirect } from 'next/navigation';
export async function GET(req: Request) {
  const u = new URL(req.url);
  const [id, type, token] = ['id', 'type', 'token'].map((k) => u.searchParams.get(k));
  const r = await fetch(`${process.env.RP_API}/wp/v2/${type}/${id}?preview_token=${token}`, { cache: 'no-store' });
  if (!r.ok) return new Response('Preview link expired', { status: 401 });
  (await draftMode()).enable();
  redirect(`/preview/${type}/${id}?token=${token}`); // that page fetches the same URL
}

Plugins can add their own REST routes, see Plugins. For caching on the RustPress side, see Performance.