Preview draft content from a headless CMS on your site
By the SharpWrite team · Published
Short answer
To preview a draft from a headless CMS, add a private route on your own site that fetches content with a second credential, a preview token, alongside your normal API key. The CMS then returns drafts as well as published entries. Keep the token on the server, put the route behind a login, and never cache the response.
In this post
The CMS editor shows you what a post says. It does not show you what the post looks like on your site, with your fonts, your layout and your code blocks. Previewing a draft on the real site closes that gap, and it is one of the cheapest ways to catch mistakes before readers do.
The idea is simple. Your site already fetches published content from the CMS API. For a preview, it fetches the same entry with one extra credential that says "include drafts too".
How draft preview works
A headless CMS only returns published entries to a normal API key, so that unfinished work stays private. A preview token is a second secret that unlocks drafts.
In SharpWrite you create a draft preview token in the app. Your server sends it in an X-Preview-Token header next to the normal Authorization header, and the API answers with drafts as well as published entries. Each entry in the response carries a status of draft or published.
Step 1: create the token and keep it on the server
Create a preview token in the app and store it as an environment variable next to your API key:
SHARPWRITE_URL=https://app.sharpwritecms.com
SHARPWRITE_KEY=sw_live_YOUR_KEY
SHARPWRITE_PREVIEW_TOKEN=sw_prev_YOUR_TOKENThe token must never reach the browser. Anyone who has it can read every draft you have, so it belongs in server code only, never in client-side JavaScript or a public repository.
Step 2: fetch with the token
On the server, make the same request as for a normal page and add the header. Turn caching off so you always see the latest draft:
const res = await fetch(`${process.env.SHARPWRITE_URL}/api/public/v1/posts/${slug}`, {
headers: {
Authorization: `Bearer ${process.env.SHARPWRITE_KEY}`,
"X-Preview-Token": process.env.SHARPWRITE_PREVIEW_TOKEN,
},
cache: "no-store",
});
const { data: post } = await res.json();Preview responses are never cached by the API, and you should not cache them yourself.
Step 3: build a private preview route
Create a route that only you can open, such as /preview/my-post-slug, and render the entry with the same template your real post page uses. That way the preview is the real thing, not a lookalike.
Three rules for the route:
- Put it behind a login or a secret address. Anyone who can open the route can read your drafts.
- Keep it out of search. Add a
noindexrobots tag and leave it out of your sitemap. - Render on request. A static site cannot show drafts, because a static build has no idea what you wrote a minute ago. The preview route needs server rendering, even if the rest of the site is static.
Step 4: show a draft banner
Because every entry reports its status, you can warn yourself when you are looking at something unpublished:
{post.status === "draft" && <p className="banner">Draft preview. Not published.</p>}It sounds minor, but a banner stops you from sharing a preview link and assuming the post is live.
Common problems
| What you see | What to check |
|---|---|
| The preview shows the published version, not the draft | The X-Preview-Token header is missing or misspelled, or a cache is serving an old response |
| 401 error | The API key is wrong or revoked. The preview token is sent next to the key, not instead of it |
| 404 for a new draft | The slug in the URL does not match the entry's slug, or the entry is in a different content type |
| Drafts visible to the public | The preview route is not protected. Add a login now and rotate the token |
A note on previews and rebuilds
A preview only reads content, so it never changes your live site, and unpublished work appears nowhere else. When you publish, a webhook can rebuild a static site automatically. The two work together: preview while you write, publish when it looks right.
The full request examples are in the Connect your site docs. If you do not have a CMS yet, SharpWrite has a 7-day free trial with draft previews included in both Cloud plans.
Frequently asked questions
How do I preview unpublished content from a headless CMS?
Add a private route on your site that fetches content with a preview token as well as your normal API key. The CMS returns drafts for that request, and your route renders them with your real page template.
What is a preview token?
A preview token is a second secret, separate from your API key, that lets your server read draft entries. In SharpWrite it is sent in the X-Preview-Token header.
Can I preview drafts on a static site?
Not from the static files, because they are built ahead of time. You need a route that renders on the server for each request. The rest of the site can stay static.
Is it safe to put the preview token in my frontend code?
No. Anyone who sees the token can read all your drafts. Keep it in server-side environment variables and never include it in browser JavaScript or a public repository.
Does previewing a draft trigger a rebuild?
No. Previewing only reads content, so it cannot change your live site. Your live pages change when you publish and your site rebuilds.