Skip to main content

REST API: Population & Field Selection

The REST API by default does not populate any relations, media fields, components, or dynamic zones. Use the populate parameter to populate specific fields and the select parameter to return only specific fields with the query results. Ensure that the find permission is given to the field(s) for the relation(s) you populate.

💡 Tip

Strapi takes advantage of the ability of the qs library to parse nested objects to create more complex queries.

Use qs directly to generate complex queries instead of creating them manually. Examples in this documentation showcase how you can use qs.

You can also use the interactive query builder if you prefer playing with our online tool instead of generating queries with qs on your machine.

Field selection

Queries can accept a fields parameter to select only some fields. By default, only the following types of fields are returned:

  • string types: string, text, richtext, enumeration, email, password, and uid,
  • date types: date, time, datetime, and timestamp,
  • number types: integer, biginteger, float, and decimal,
  • generic types: boolean, array, and JSON.

Field selection does not work on relational, media, component, or dynamic zone fields. To populate these fields, use the populate parameter.

💡 Tip

By default, fields are selected except relations, media, dynamic zones, and components, but you can specify a wildcard * instead of an array.



Example request: Return only title and body fields

GET /api/users?fields[0]=title&fields[1]=body

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "test1",
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
}
}
],
"meta": {
// ...
}
}

JavaScript query (built with the qs library):

The query URL above was built using the qs library. qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.

const qs = require('qs');
const query = qs.stringify(
{
fields: ['title', 'body'],
},
{
encodeValuesOnly: true, // prettify URL
}
);

await request(`/api/users?${query}`);

Population

The REST API by default does not populate any type of fields, so it will not populate relations, media fields, components, or dynamic zones unless you pass a populate parameter to populate various field types.

The populate parameter can be used alone or in combination with with multiple operators to have much more control over the population.

Caution

The find permission must be enabled for the content-types that are being populated. If a role doesn't have access to a content-type it will not be populated (see User Guide for additional information on how to enable find permissions for content-types).

✏️ Note

It's currently not possible to return just an array of ids with a request.

🤓 Populating guides

The REST API guides section includes more detailed information about various possible use cases for the populate parameter:

  • The Understanding populate guide explains in details how populate works, with diagrams, comparisons, and real-world examples.
  • The How to populate creator fields guide provides step-by-step instructions on how to add createdBy and updatedBy fields to your queries responses.

The Strapi Blog also includes a tutorial on how to populate and filter data with your queries.

The following table sums up possible populate use cases and their associated parameter syntaxes, and links to sections of the Understanding populate guide which includes more detailed explanations:

Use caseExample parameter syntaxDetailed explanations to read
Populate everything, 1 level deep, including media fields, relations, components, and dynamic zonespopulate=*Populate all relations and fields, 1 level deep
Populate one relation,
1 level deep
populate[0]=a-relation-namePopulate 1 level deep for specific relations
Populate several relations,
1 level deep
populate[0]=relation-name&populate[1]=another-relation-name&populate[2]=yet-another-relation-namePopulate 1 level deep for specific relations
Populate some relations, several levels deeppopulate[first-level-relation-to-populate][populate][0]=second-level-relation-to-populatePopulate several levels deep for specific relations
Populate a componentpopulate[0]=component-namePopulate components
Populate a component and one of its nested componentspopulate[0]=component-name&populate[1]=component-name.nested-component-namePopulate components
Populate a dynamic zone (only its first-level elements)populate[0]=dynamic-zone-namePopulate dynamic zones
Populate a dynamic zone and its nested elements and relations, using a unique, shared population strategypopulate[dynamic-zone-name][populate]=*Populate dynamic zones
Populate a dynamic zone and its nested elements and relations, using a precisely defined, detailed population strategypopulate[dynamic-zone-name][on][dynamic-zone-name.component-name][populate][relation-name][populate][0]=field-namePopulate dynamic zones
💡 Tip

The easiest way to build complex queries with multiple-level population is to use our interactive query builder tool.

Combining Population with other operators

By utilizing the populate operator it is possible to combine other operators such as field selection, filters, and sort in the population queries.

Caution

The population and pagination operators cannot be combined.

Populate with field selection

fields and populate can be combined.

Example request

GET /api/articles?fields[0]=title&fields[1]=slug&populate[headerImage][fields][0]=name&populate[headerImage][fields][1]=url

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "Test Article",
"slug": "test-article",
"headerImage": {
"data": {
"id": 1,
"attributes": {
"name": "17520.jpg",
"url": "/uploads/17520_73c601c014.jpg"
}
}
}
}
}
],
"meta": {
// ...
}
}

JavaScript query (built with the qs library):

The query URL above was built using the qs library. qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.

const qs = require('qs');
const query = qs.stringify(
{
fields: ['title', 'slug'],
populate: {
headerImage: {
fields: ['name', 'url'],
},
},
},
{
encodeValuesOnly: true, // prettify URL
}
);

await request(`/api/articles?${query}`);

Populate with filtering

filters and populate can be combined.

Example request

GET /api/articles?populate[categories][sort][0]=name%3Aasc&populate[categories][filters][name][$eq]=Cars

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "Test Article",
// ...
"categories": {
"data": [
{
"id": 2,
"attributes": {
"name": "Cars"
// ...
}
}
]
}
}
}
],
"meta": {
// ...
}
}

JavaScript query (built with the qs library):

The query URL above was built using the qs library. qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.

const qs = require('qs');
const query = qs.stringify(
{
populate: {
categories: {
sort: ['name:asc'],
filters: {
name: {
$eq: 'Cars',
},
},
},
},
},
{
encodeValuesOnly: true, // prettify URL
}
);

await request(`/api/articles?${query}`);