Paginate / Loop with nextCursor. Am I supposed to “count” and offset, when they’re providing a “next” variable?
In the response header, I get the count and the limit, but I don’t understand how to loop.
[
{
"body": {
"list": {
"meta": {
"totalCount": 426,
"limit": 100,
"nextCursor": "?limit=100&hasMoreElements=true&soIndex=426&poIndex=100&partnerId=10001078279&sellerId=101058904&createdStartDate=2023-07-12T05:03:59.721Z&createdEndDate=2023-07-19T05:03:59.721Z&shipNodeType=SellerFulfilled"
},
"elements": {
"order": [
{
"purchaseOrderId": "108820170097219",
...
]
- n8n Version0.234.1:
- Database (default: POSTGRES):
- Running n8n via: Caprover :
Hi @unicornParty, it looks like your API provides all information required to query additional pages in the body.list.meta.nextCursor
field.
You should be able to do something like this:
This workflow appends the body.list.meta.nextCursor
field when present, otherwise it uses an empty string instead. I don’t have access to the Walmart API, but perhaps you can give this a go with your own credentials?
I’ve also removed what seems to be a token from the workflow you have shared, to be safe you might want to rotate/recreate the secrets you have used in this workflow.
1 Like
thank you so much for responding.
In your example, how will the next request be queried?
I “SET” the nextCursor “IF” next is not empty.
I already SET the base URL, so I was trying to combine SET URL and SET NEXT to loop…
In your example, how will the next request be queried?
Hi @unicornParty, the example HTTP Request uses a URL expression of https://marketplace.walmartapis.com{{ $json?.body?.list?.meta?.nextCursor ? $json.body.list.meta.nextCursor : "" }}
The snippet inside the curly braces $json?.body?.list?.meta?.nextCursor ? $json.body.list.meta.nextCursor : ""
uses the ternary operator. So on the first execution (when there is no json.body.list.meta.nextCursor
field) the request would simply go to https://marketplace.walmartapis.com
, but when the field is populated it would be attached to this URL.
I couldn’t test this myself so just used this as a dummy URL, but now revisiting your original workflow you probably want to use an expression like this instead as I’ve missed the /v3/orders
part before:
https://marketplace.walmartapis.com/v3/orders{{ $json?.body?.list?.meta?.nextCursor ? $json.body.list.meta.nextCursor : "" }}