Pagination
Walk a list to the end with limit and cursor, in five languages, and keep a copy up to date with events.
On this page
A list answers one page of items and a nextCursor for the next page.
limit and cursor
| Name | Meaning |
|---|---|
limit | Records per page, from 1 to 500. The default is 100. |
cursor | Opaque text, the nextCursor of the previous page. Leave it out for the first page. |
nextCursor | In the answer: the cursor of the next page, or null after the last page. |
The first page of the movements of one item, two at a time:
First page
curl
curl "$RDC_BASE_URL/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2" \
-H "Authorization: Bearer $RDC_TOKEN" JavaScript
const response = await fetch(`${process.env.RDC_BASE_URL}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2`, {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.RDC_TOKEN}`,
},
});
const result = await response.json(); Python
import os
import requests
response = requests.request(
'GET',
f'{os.environ["RDC_BASE_URL"]}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2',
headers={
'Authorization': f'Bearer {os.environ["RDC_TOKEN"]}',
},
)
result = response.json() PHP
<?php
$curl = curl_init(getenv('RDC_BASE_URL') . '/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2');
curl_setopt_array($curl, [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('RDC_TOKEN'),
],
]);
$result = json_decode(curl_exec($curl), true);
curl_close($curl); C#
using System.Net.Http.Headers;
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"{Environment.GetEnvironmentVariable("RDC_BASE_URL")}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("RDC_TOKEN"));
var response = await client.SendAsync(request);
var result = await response.Content.ReadAsStringAsync(); Answer 200
{
"items": [
{
"movementId": "0d6f3c91-8a25-4b47-b1e0-4c7a9e2d5f18",
"kind": "receipt",
"onHandDelta": "120",
"reservedDelta": "0",
"resultingRevision": 6
},
{
"movementId": "6a1e8d52-3c07-4f94-a2b8-e5d0c71f9a36",
"kind": "receipt",
"onHandDelta": "60",
"reservedDelta": "0",
"resultingRevision": 7
}
],
"nextCursor": "eyJhZnRlciI6IjQ3In0"
} Next page
curl
curl "$RDC_BASE_URL/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2&cursor=eyJhZnRlciI6IjQ3In0" \
-H "Authorization: Bearer $RDC_TOKEN" JavaScript
const response = await fetch(`${process.env.RDC_BASE_URL}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2&cursor=eyJhZnRlciI6IjQ3In0`, {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.RDC_TOKEN}`,
},
});
const result = await response.json(); Python
import os
import requests
response = requests.request(
'GET',
f'{os.environ["RDC_BASE_URL"]}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2&cursor=eyJhZnRlciI6IjQ3In0',
headers={
'Authorization': f'Bearer {os.environ["RDC_TOKEN"]}',
},
)
result = response.json() PHP
<?php
$curl = curl_init(getenv('RDC_BASE_URL') . '/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2&cursor=eyJhZnRlciI6IjQ3In0');
curl_setopt_array($curl, [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('RDC_TOKEN'),
],
]);
$result = json_decode(curl_exec($curl), true);
curl_close($curl); C#
using System.Net.Http.Headers;
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"{Environment.GetEnvironmentVariable("RDC_BASE_URL")}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?limit=2&cursor=eyJhZnRlciI6IjQ3In0");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("RDC_TOKEN"));
var response = await client.SendAsync(request);
var result = await response.Content.ReadAsStringAsync(); Answer 200
{
"items": [
{
"movementId": "f2a95c08-6d41-4e7b-83a0-b7d14e92c5a6",
"kind": "reservation",
"onHandDelta": "0",
"reservedDelta": "24",
"resultingRevision": 8
}
],
"nextCursor": null
} Walking a list to the end
Ask for a page, handle its items, and ask again with nextCursor until it is null. The loop is the same in every language.
Walk every page
curl
cursor=""
while : ; do
# The first page has no cursor: send cursor only when there is one.
set -- --data-urlencode "limit=100"
[ -n "$cursor" ] && set -- "$@" --data-urlencode "cursor=$cursor"
page=$(curl -s -G "$RDC_BASE_URL/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements" \
-H "Authorization: Bearer $RDC_TOKEN" "$@")
echo "$page" | jq -c '.items[]'
cursor=$(echo "$page" | jq -r '.nextCursor // empty')
[ -z "$cursor" ] && break
done JavaScript
let cursor = null;
do {
const query = new URLSearchParams({limit: '100'});
if (cursor) query.set('cursor', cursor);
const response = await fetch(`${process.env.RDC_BASE_URL}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?${query}`, {
headers: {Authorization: `Bearer ${process.env.RDC_TOKEN}`},
});
const page = await response.json();
for (const item of page.items) console.log(item);
cursor = page.nextCursor;
} while (cursor); Python
import os
import requests
cursor = None
while True:
params = {'limit': 100}
if cursor:
params['cursor'] = cursor
page = requests.get(
f'{os.environ["RDC_BASE_URL"]}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements',
headers={'Authorization': f'Bearer {os.environ["RDC_TOKEN"]}'},
params=params,
).json()
for item in page['items']:
print(item)
cursor = page['nextCursor']
if not cursor:
break PHP
<?php
$cursor = null;
do {
$query = ['limit' => 100] + ($cursor ? ['cursor' => $cursor] : []);
$curl = curl_init(getenv('RDC_BASE_URL') . '/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?' . http_build_query($query));
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('RDC_TOKEN')],
]);
$page = json_decode(curl_exec($curl), true);
curl_close($curl);
foreach ($page['items'] as $item) {
print_r($item);
}
$cursor = $page['nextCursor'];
} while ($cursor); C#
using System.Net.Http.Headers;
using System.Text.Json;
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("RDC_TOKEN"));
string? cursor = null;
do
{
var query = cursor is null ? "limit=100" : $"limit=100&cursor={Uri.EscapeDataString(cursor)}";
var json = await client.GetStringAsync($"{Environment.GetEnvironmentVariable("RDC_BASE_URL")}/inventory/v1/items/7d0c4e19-b3a2-4f65-8e91-2a6f5c8d3b40/movements?{query}");
using var page = JsonDocument.Parse(json);
foreach (var item in page.RootElement.GetProperty("items").EnumerateArray()) Console.WriteLine(item);
var next = page.RootElement.GetProperty("nextCursor");
cursor = next.ValueKind == JsonValueKind.Null ? null : next.GetString();
}
while (cursor is not null); A cursor is opaque and belongs to its filters
- Do not build, edit or parse a cursor. It means something only to the request that made it.
- Send the same filters with every page of one walk. If a filter changes, start again without a cursor.
- Do not keep a cursor for later: it belongs to the walk you are in.
Records that change while you read
A list follows a stable order, and a walk does not skip or repeat a record that was there when it began. A record that is added or changed during the walk can appear in it or can wait for the next walk. When you need every change, follow the events as well: Events.
Keeping a copy up to date
Read once with pagination to fill your copy, then follow the events to keep it current. Each event carries the aggregateRevision of its record, so your copy can tell a newer state from an older one.