Skip to content

Get referenced entries together

A reference field is returned with the referenced entry placed inside it. include decides how many levels deep to place entries. You can read entries of different content types, such as articles and authors, in one call.

In the examples below, the content type “posts” has a reference field “author” that points to the content type “authors”. Replace <hostname> with the hostname of your environment and <API key> with your API key.

If you read without adding anything, referenced entries are placed up to the first level (the default for include is 1).

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

author is returned with the author’s entry inside it. What the author’s entry references in turn (here, mentor) has only sys’s id and contentType (dates and so on are omitted).

{
"sys": { "id": "hello", "contentType": "posts", "version": 3 },
"title": "こんにちは",
"author": {
"sys": { "id": "kohei", "contentType": "authors", "version": 1 },
"name": "Kohei",
"mentor": { "sys": { "id": "taro", "contentType": "authors" } }
}
}

A multiple references field is returned as an array of the referenced entries.

Add a number from 0 to 3 to include.

include What is returned
0 Referenced entries are not placed. Only { "sys": { "id", "contentType" } }
1 (default) Up to the referenced entries
2 Up to the entries that the referenced entries reference
3 One level further

To get as far as the author’s mentor, add include=2.

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

This works for lists, one entry, and singles.

If you only need the referenced entry IDs, use include=0 to make the response smaller.

With select, you can also select fields of referenced entries by following them with dots.

ターミナルウィンドウ
curl -G https://<hostname>.hakocms.net/api/v1/content/posts/hello \
-H "Authorization: Bearer <API key>" \
--data-urlencode "select=sys.id,title,author.name"

You get a response like this:

{
"sys": { "id": "hello" },
"title": "こんにちは",
"author": { "name": "Kohei" }
}

For a multiple references field, the same selection applies to each element of the array.

You can filter a reference field by the referenced entry ID. Filter to articles whose author is “kohei”.

ターミナルウィンドウ
curl -G https://<hostname>.hakocms.net/api/v1/content/posts \
-H "Authorization: Bearer <API key>" \
--data-urlencode 'where=author = "kohei"'

You cannot filter by fields of referenced entries (such as author.name = "Kohei") yet. You get 400.

If a referenced entry cannot be read, that entry is not returned.

  • This applies when the referenced entry is not published, has been unpublished, or has been deleted
  • For a reference field, the key itself is not returned
  • For a multiple references field, the entries that cannot be read are removed from the array

If referenced entries are not returned, check that they are [公開中](Published).

When you read drafts with preview=true, referenced entries are also read as drafts (including ones that have never been published). For details, see Read drafts (preview).

If author is returned with fields such as name inside it, you are getting referenced entries. If only sys is returned, the include level is not deep enough.