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.
Before you begin
Section titled “Before you begin”- An API key with the [読み取り](Read) permission. To issue one, see Create API keys and set permissions
- A content type with a reference field. For how to create one, see Create content types
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.
Get referenced entries
Section titled “Get referenced entries”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.
Decide how many levels to get
Section titled “Decide how many levels to get”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=2This works for lists, one entry, and singles.
If you only need the referenced entry IDs, use include=0 to make the response smaller.
Select fields of referenced entries
Section titled “Select fields of referenced entries”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.
Filter by referenced entries
Section titled “Filter by referenced entries”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.
When referenced entries are not returned
Section titled “When referenced entries are not returned”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).
Check the result
Section titled “Check the result”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.