Skip to content

Read drafts (preview)

To check an entry before it is published, with the same look as your live site, read the draft with preview=true. Only an API key with the [プレビュー](Preview) permission can read drafts.

  • An API key with the [読み取り](Read) and [プレビュー](Preview) permissions. For how to set permissions, see Create API keys and set permissions
  • Use this key only on your site’s server. Do not give it to the browser. If you do, anyone can read drafts

In the examples below, replace <hostname> with the hostname of your environment, and <preview API key> with a key that has the [プレビュー](Preview) permission.

Add preview=true to the URL. It works for lists, one entry, and singles.

ターミナルウィンドウ
curl -G https://<hostname>.hakocms.net/api/v1/content/posts/hello \
-H "Authorization: Bearer <preview API key>" \
-d preview=true

You get a response like this (some dates are omitted). This entry has never been published, so publishedAt and firstPublishedAt are null.

{
"sys": {
"id": "hello",
"contentType": "posts",
"publishedAt": null,
"firstPublishedAt": null,
"version": 2
},
"title": "書きかけのタイトル"
}

With preview=true, the response works as follows:

  • For an entry with a draft, the draft values are returned. If there is no draft, the published values are returned
  • Entries that have never been published are also returned
  • Unpublished entries are not returned
  • where, order, and q also filter and sort by the returned values (the draft values)
  • Referenced entries follow the same rules, and their drafts are read

sys.version is the revision number of the returned value. If there is a draft, it is the draft’s revision.

Write the value of preview as true or false. 1 or 0 gives 400.

What protects drafts is the API key’s permission, not preview=true. Even with the same key, only published entries are returned without preview=true. So one key with the [プレビュー](Preview) permission can serve both published pages and draft previews.

What What it decides
API key (with the [プレビュー] permission) Whether drafts may be read. Keep it only on your site’s server
preview=true Whether this call returns drafts
Authentication on your site Whether the viewer may see drafts. Your site decides this itself

Do not build your site so that it passes ?preview=true through whenever the site URL has it. Anyone could see drafts.

On your site’s server, check that the viewer may see drafts before adding preview=true. For example, as with Next.js Draft Mode, give a signed cookie to a person who has proved a secret that only your site knows, and add preview=true only when that cookie is present.

Here is an example that, on your site’s server, decides whether to add preview=true depending on the viewer. isDraftViewer is the result of your site’s check (whether the cookie is present, and so on). Put a key with the [プレビュー] permission in HAKOCMS_API_KEY.

const base = "https://<hostname>.hakocms.net/api/v1/content";
const headers = {
Authorization: `Bearer ${process.env.HAKOCMS_API_KEY}`,
};
async function getPost(id, isDraftViewer) {
const params = new URLSearchParams();
if (isDraftViewer) params.set("preview", "true");
const url = `${base}/posts/${encodeURIComponent(id)}?${params}`;
const res = await fetch(url, { headers });
if (res.status === 404) return null;
return res.json();
}

On the entry edit screen in the admin, [下書きを取得する URL(プレビュー)](URL to get the draft (preview)) under [情報](Info) on the right shows the URL that reads that entry’s draft.

Change an entry in the admin and save it, but do not publish it. Read it with preview=true. If the changed values are returned, you are reading the draft. If you read it without preview=true, the published values are returned.

What you got Cause Fix
403 forbidden The key does not have the [プレビュー](Preview) permission On the [API キー](API keys) screen, check [プレビュー] for the key. [読み取り](Read) is also required
404 not_found The entry is unpublished or does not exist Unpublished entries are not returned even with preview=true
400 bad_request The value of preview is not true or false Write preview=true