メインコンテンツまでスキップ

REST API: ソートとページネーション

REST API のクエリ結果は、ソートとページネーションができます。

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.

ソート

クエリでは sort パラメータで、1 つまたは複数フィールドのソートができます。構文は次のとおりです。

  • 1 フィールド: GET /api/:pluralApiId?sort=value
  • 複数フィールド: GET /api/:pluralApiId?sort[0]=value1&sort[1]=value2(例: 2 フィールド)

並び順は次で指定します。

  • :asc は昇順(既定。省略可)
  • :desc は降順

例: 2 フィールドでソート

sort に複数フィールドを渡して並べ替えます。


リクエスト例: 2 フィールドでソート

GET /api/restaurants?sort[0]=Description&sort[1]=Name

JavaScript のクエリ(qs ライブラリで組み立て):

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({
sort: ['Description', 'Name'],
}, {
encodeValuesOnly: true, // prettify URL
});

await request(`/api/restaurants?${query}`);
レスポンス例
{
"data": [
{
"id": 9,
"documentId": "hgv1vny5cebq2l3czil1rpb3",
"Name": "BMK Paris Bamako",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
{
"id": 8,
"documentId": "flzc8qrarj19ee0luix8knxn",
"Name": "Restaurant D",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
// …
],
"meta": {
// …
}
}

例: 2 フィールドでソートし、昇順・降順を指定

各フィールドに :asc または :desc を付けて、希望の順序にします。


リクエスト例: 2 フィールドでソートし順序を指定

GET /api/restaurants?sort[0]=Description:asc&sort[1]=Name:desc

JavaScript のクエリ(qs ライブラリで組み立て):

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({
sort: ['Description:asc', 'Name:desc'],
}, {
encodeValuesOnly: true, // prettify URL
});

await request(`/api/restaurants?${query}`);
レスポンス例
{
"data": [
{
"id": 8,
"documentId": "flzc8qrarj19ee0luix8knxn",
"Name": "Restaurant D",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
{
"id": 9,
"documentId": "hgv1vny5cebq2l3czil1rpb3",
"Name": "BMK Paris Bamako",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
// …
],
"meta": {
// …
}
}

ページネーション

クエリでは pagination パラメータを使えます。次のいずれかの方法です。

Note

ページネーションの方式は混在させられません。pagepageSizeまたは startlimit のどちらか一方だけを使ってください。

ページ番号によるページネーション

ページ番号で区切るには次のパラメータを使います。

パラメータ説明既定値
pagination[page]Integerページ番号1
pagination[pageSize]Integer1 ページあたりの件数25
pagination[withCount]Booleanレスポンスにエントリ総数とページ数を含めるかTrue
リクエスト例: 1 ページ目だけ 10 件返す

GET /api/articles?pagination[page]=1&pagination[pageSize]=10

JavaScript のクエリ(qs ライブラリで組み立て):

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({
pagination: {
page: 1,
pageSize: 10,
},
}, {
encodeValuesOnly: true, // prettify URL
});

await request(`/api/articles?${query}`);
レスポンス例
{
"data": [
// ...
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 10,
"pageCount": 5,
"total": 48
}
}
}

オフセットによるページネーション

オフセットで区切るには次のパラメータを使います。

パラメータ説明既定値
pagination[start]Integer開始位置(返す最初のエントリのオフセット)0
pagination[limit]Integer返すエントリ数25
pagination[withCount]Booleanレスポンスにエントリ総数を含めるかtrue
Tip

pagination[limit] の既定値と最大値は、./config/api.jsapi.rest.defaultLimitapi.rest.maxLimit で設定できます。

リクエスト例: オフセットで先頭 10 件だけ返す

GET /api/articles?pagination[start]=0&pagination[limit]=10

JavaScript のクエリ(qs ライブラリで組み立て):

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({
pagination: {
start: 0,
limit: 10,
},
}, {
encodeValuesOnly: true, // prettify URL
});

await request(`/api/articles?${query}`);
レスポンス例
{
"data": [
// ...
],
"meta": {
"pagination": {
"start": 0,
"limit": 10,
"total": 42
}
}
}