Skip to content

Create API keys and set permissions

An API key is the key for using the Content API. For each key, you can decide what it may do (its permissions). This page explains how to issue a key, set its permissions, and stop a key you no longer need.

  • You need a role that can manage API keys in the space. Among the built-in roles, these are [オーナー](Owner) and [管理者](Administrator)
  • You issue API keys per space. A key can only call the entries and media of its own space

To issue an API key, do the following:

  1. In the left menu of the admin, click [API キー](API keys).
  2. In [名前](Name), enter a name to tell the key apart (for example, “本番サイト”). The name is 1–100 characters.
  3. Click [発行](Issue).

“API キーを発行しました。このキーは今しか表示されません。安全な場所に控えてください” (API key issued. This key is shown only now. Keep it in a safe place) appears, and a key starting with hako_ is shown.

The key is shown only once, at this time. Hako CMS does not keep the key itself. If you lose it, issue a new key.

A newly issued key has only the [読み取り](Read) permission. It can only read published entries.

You set permissions for each entry operation.

Permission What it allows
[読み取り](Read) Read published entries
[プレビュー](Preview) Also read drafts, with preview=true. [読み取り] is also required
[作成](Create) Create entries. Created entries are drafts
[更新](Update) Change or discard drafts
[公開](Publish) Publish, unpublish, and schedule publishing
[削除](Delete) Delete entries

To change permissions, do the following:

  1. On the [API キー](API keys) screen, click the name of the key whose permissions you want to change.
  2. In the [権限](Permissions) table, on the [既定(すべてのコンテンツタイプ)](Default (all content types)) row, check the operations to allow.
  3. (Optional) To change them per content type, check [個別に設定](Set individually) on that content type’s row, and check the operations to allow.
  4. (Optional) To use the media API, under [メディア](Media), check the operations to allow among [読み取り](Read), [アップロード](Upload), [編集](Edit), and [削除](Delete).
  5. Click [保存](Save).

“保存しました” (Saved) appears. The changed permissions take effect from the next request.

For a content type with [個別に設定](Set individually) checked, the default permissions are not used. Only the individual permissions apply. They are not added to the default permissions. For example, even if the default allows [読み取り](Read), you cannot read a content type whose individual permissions do not include [読み取り].

You set media permissions separately from entry permissions.

If you put an API key in code that runs in the browser, anyone who visits the site can see the key.

  • You may give a key with only [読み取り](Read) to the browser. Published entries are meant to be seen by anyone in the first place
  • Do not give the browser a key with [プレビュー](Preview), write permissions ([作成][更新][公開][削除]), or media permissions. Use such keys only on your site’s server

It is safer to use separate keys for published pages and for draft previews or writing.

If a key leaks or you no longer use it, revoke the key.

  1. On the [API キー](API keys) screen, click [失効](Revoke) on the row of the key you want to stop.
  2. On the confirmation screen, click [失効] again.

The key stops working right away. Calls with that key return 401. A revoked key moves to the [失効したキー](Revoked keys) list.

To use a stopped key again, click [再び有効にする](Re-enable) for that key in the [失効したキー](Revoked keys) list, and click [再び有効にする] again on the confirmation screen. The same key works again, with the permissions it had when it was revoked. If you stopped a key because it leaked, do not re-enable it. Issue a new key instead.

Read published entries with the key you issued. Replace <hostname> with the hostname of your environment, <content type ID> with the value of the content type’s [エンドポイント](Endpoint), and <API key> with the key you issued.

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

If you get JSON with items and total, the key works. If you get 401, the key is wrong or revoked. If you get 403, the key does not have permission for that operation.