Skip to content

Quickstart: read entries with the API

On this page, you sign up for Hako CMS, write and publish one article, and retrieve that article with the API. It takes about 5 minutes.

At the end, you get JSON like the following back.

{
"items": [
{
"sys": { "id": "hello", "contentType": "posts", "version": 1 },
"title": "はじめての記事"
}
],
"total": 1
}
  • An email address that can receive email
  • A terminal where you can use curl, or a browser

Sign up on the free plan. It’s free.

  1. Open the sign-up screen.
  2. Enter [メールアドレス](Email address), [お名前](Name), and [パスワード(契約・お支払いの画面用)](Password for the account and billing screen).
  3. In [CMS のアドレス](CMS address), enter a name using lowercase letters, digits, and hyphens (for example, “my-site”). This name becomes your environment’s address, https://<hostname>.hakocms.net.
  4. Select [利用規約に同意します](I agree to the Terms of Service).
  5. Click [申し込む](Sign up).

The sign-up screen. The email address, name, and password are entered, “my-site” is entered in CMS address, and “使えます” (Available) is shown. The Terms of Service checkbox is selected

If “ボットでないことを確認しています” (Checking that you’re not a bot) is shown below [申し込む], you’re signed up automatically when the check finishes. The check can take about 10 seconds.

You receive an email at the address you entered for setting your admin password.

  1. Open the link in the email you received.
  2. In [パスワード](Password) and [パスワード(確認)](Password (confirm)), enter the password to use in the admin.
  3. Click [パスワードを設定してログイン](Set password and log in).

The password setting screen. The email address field contains the address you signed up with, and the password has been entered twice

The admin (https://<hostname>.hakocms.net/admin) opens and shows “まだスペースがありません” (No spaces yet).

The password you entered on the sign-up screen (for the account and billing screen) and the admin password are different. Save both.

A space is the unit that groups the entries of one site or app.

  1. Click [スペースを作成](Create space).
  2. Enter “ブログ” in [スペース名](Space name) and “blog” in [スペース ID](Space ID).
  3. Click [作成](Create).

The Create space screen, with “ブログ” entered in Space name and “blog” in Space ID

“まだコンテンツタイプがありません” (No content types yet) is shown.

A content type is a box that holds entries. Here, you use a template to create “記事” (Article).

  1. Click [コンテンツタイプを作成](Create content type).
  2. Check that [コレクション](Collection) is selected, then click [次へ](Next).
  3. Enter “記事” in [コンテンツタイプ名](Content type name) and “posts” in [エンドポイント](Endpoint), then click [次へ].
  4. Choose [ブログ記事](Blog post), then click [作成](Create).

Step 2 of 3 of Create content type. When “記事” is entered in Content type name and “posts” in Endpoint, the API URL appears below

“記事” is created with the fields title, description, featured image, and body, and the article list screen opens. The “posts” you entered in [エンドポイント] goes into the API URL.

  1. Click [新規作成](New).
  2. Enter “はじめての記事” in [タイトル](Title) and any text you like in [本文](Body).
  3. On the right, enter “hello” in [エントリー ID(省略すると自動で付ける)](Entry ID (assigned automatically if omitted)). If you leave it empty, an ID is assigned automatically.
  4. Click [公開](Publish).

“公開しました” (Published) is shown, and the article status becomes [公開中](Published).

The article screen after publishing. “公開中” (Published) is shown below the title and in [ステータス] (Status) on the right

You need an API key to retrieve entries with the API.

  1. Click [API キー](API keys) in the left menu.
  2. Enter “quickstart” in [名前](Name), then click [発行](Issue).

An API key starting with hako_ is shown.

The API keys screen. A key starting with hako_ is shown below “API キーを発行しました” (API key issued), and the quickstart key is listed with [読み取り] (Read)

The key is shown only this once. Copy it and save it somewhere safe.

The permission of the key you issued is [読み取り](Read). It can only read published entries.

Retrieve them with curl. Replace <hostname> with the name you chose in step 1, and <API key> with the key you saved in step 6.

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

You get the following back (dates and times are omitted).

{
"items": [
{
"sys": {
"id": "hello",
"contentType": "posts",
"version": 1
},
"title": "はじめての記事",
"body": {
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [{ "type": "text", "text": "Hako CMS から配信しています。" }]
}
]
}
}
],
"total": 1,
"limit": 20,
"offset": 0
}

In JavaScript, you can retrieve them the same way with fetch. You can call it from any origin.

const res = await fetch("https://<hostname>.hakocms.net/api/v1/content/posts", {
headers: { Authorization: "Bearer <API key>" },
});
const { items } = await res.json();
console.log(items[0].title); // はじめての記事

To retrieve a single entry, add the entry ID to the end of the URL (/api/v1/content/posts/hello).

You’ve now retrieved your first entry with the API.

What you got back Cause Fix
401 unauthorized The API key is missing or wrong Check that you put the key starting with hako_ as is after Authorization: Bearer . If you lost the key, issue a new one on the [API キー](API keys) screen
404 not_found The content type or entry isn’t found Check that posts in the URL matches [エンドポイント](Endpoint) in step 4. Check that the entry is [公開中](Published). Draft entries aren’t returned with a read key
Clicking [申し込む](Sign up) doesn’t move forward The Terms of Service checkbox isn’t selected, or the check hasn’t finished If “申し込むには、利用規約への同意が要ります” (You must agree to the Terms of Service to sign up) is shown, select [利用規約に同意します](I agree to the Terms of Service). If “ボットでないことを確認しています” (Checking that you’re not a bot) is shown, just wait

If it still doesn’t work, contact us with the value of the X-Request-Id response header.