# How to setup API call for paginated response with an collection?

**URL:** <https://community.postman.com/t/how-to-setup-api-call-for-paginated-response-with-an-collection/49457>\
**Category:** Help Hub\
**Tags:** collections\
**Created:** [July 12, 2023, 9:37am UTC](https://community.postman.com/t/how-to-setup-api-call-for-paginated-response-with-an-collection/49457 "2023-07-12T09:37:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdamaschke](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mdamaschke](https://community.postman.com/u/mdamaschke)\
**Post date:** [July 12, 2023, 9:37am UTC](https://community.postman.com/t/how-to-setup-api-call-for-paginated-response-with-an-collection/49457/1 "2023-07-12T09:37:04Z")

</div>

Dear all,

I am faced with a challenge that ma API returns 20 values per response page and I have also instances, where more results exist. The API response body contains an URL for the next result page.

The URL is stated as “”\_\_next": “Groups(‘M1XMSeeA6kFuCScyI7mdtO’)/Memberships?%24skiptoken=1yhyr&…” at the end of the response body.

I have tried to work with postman.setNextRequest etc. but as I am a newbie have not been able to get this work.

Are there some hints how I can use the collection feature and within the Test or re-request script to make this work?

Thanks for your support!  
Mathias

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 12, 2023, 11:00am UTC](https://community.postman.com/t/how-to-setup-api-call-for-paginated-response-with-an-collection/49457/2 "2023-07-12T11:00:06Z")

</div>

Can you post the whole response body, and I’m sure we can help you with the logic.

This is covered in the fifteen days of Postman training. So it will be similar to that exercise.

```auto
let currentPageNumber = pm.collectionVariables.get("pageNumber");

if (response.next != null) {
    currentPageNumber++;
    console.log("current page number : " + currentPageNumber);
    pm.collectionVariables.set("pageNumber", currentPageNumber);
    postman.setNextRequest("get starships");
} 

```

---

<div class="post-metadata">

**Author:** ![mdamaschke](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mdamaschke](https://community.postman.com/u/mdamaschke)\
**Post date:** [July 13, 2023, 2:13pm UTC](https://community.postman.com/t/how-to-setup-api-call-for-paginated-response-with-an-collection/49457/3 "2023-07-13T14:13:20Z")

</div>

Hi Mike,

thanks for your feedback and the proposed code.  
The call I am making is

[https://jam12.sapjam.com/api/v1/OData/Groups(‘M1XMSeeA6kFuCScyI7mdtO’)/Memberships](https://jam12.sapjam.com/api/v1/OData/Groups('M1XMSeeA6kFuCScyI7mdtO')/Memberships)

and the response is as follows:

```auto
{
    "d": {
        "results": [
            {
                "__metadata": {
                    "uri": "GroupMemberships(GroupId='M1XMSeeA6kFuCScyI7mdtO',MemberId='AwlNGhgE724shZvUsICssv')",
                    "type": "SAPJam.GroupMembership"
                },
                "GroupId": "M1XMSeeA6kFuCScyI7mdtO",
                "MemberId": "AwlNGhgE724shZvUsICssv",
                "MemberType": "admin",
                "CanCancelInvite": false,
                "IsFeatured": false,
                "FeaturedReason": null,
                "IsDynamicFollower": false,
                "Member": {
                    "__deferred": {
                        "uri": "GroupMemberships(GroupId='M1XMSeeA6kFuCScyI7mdtO',MemberId='AwlNGhgE724shZvUsICssv')/Member"
                    }
                }
            },
			[.. omitted]
            {
                "__metadata": {
                    "uri": "GroupMemberships(GroupId='M1XMSeeA6kFuCScyI7mdtO',MemberId='MKIF7wPLQ4FVVJlirI9OU3')",
                    "type": "SAPJam.GroupMembership"
                },
                "GroupId": "M1XMSeeA6kFuCScyI7mdtO",
                "MemberId": "MKIF7wPLQ4FVVJlirI9OU3",
                "MemberType": "member",
                "CanCancelInvite": false,
                "IsFeatured": false,
                "FeaturedReason": null,
                "IsDynamicFollower": false,
                "Member": {
                    "__deferred": {
                        "uri": "GroupMemberships(GroupId='M1XMSeeA6kFuCScyI7mdtO',MemberId='MKIF7wPLQ4FVVJlirI9OU3')/Member"
                    }
                }
            }
        ],
        "__next": "Groups('M1XMSeeA6kFuCScyI7mdtO')/Memberships?%24skiptoken=1yhyr&oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_callback=oob&oauth_consumer_key=zUN7VGKgFk7HfEiAANoh&oauth_nonce=VfyR9cF0HKI&oauth_signature=Mm0tv0vxWiUjpvsHGKSz44pstMk%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1689257289&oauth_token=KKUttqLiC6CtUFOAuH6tBvK16OhSyuOMq3fYxx43&oauth_verifier=VK3uu8oieomIZpYIB1AT&oauth_version=1.0"
    }
}

Thanks
Mathias
```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 13, 2023, 3:11pm UTC](https://community.postman.com/t/how-to-setup-api-call-for-paginated-response-with-an-collection/49457/4 "2023-07-13T15:11:58Z")

</div>

It looks like you need to include a collection (or environment) variable in the URL starting at groups.

> [https://jam12.sapjam.com/api/v1/OData/{{Groups}](https://jam12.sapjam.com/api/v1/OData/%7B%7BGroups%7D)}

With the initial value being

> Groups(‘M1XMSeeA6kFuCScyI7mdtO’)/Memberships

The script in the tests tab would then look something like…

```auto
const response = pm.response.json();
console.log(response);

let requestName = pm.info.requestName

if (response.d.__next != null) {
    pm.collectionVariables.set("Groups", response.d.__next);
    postman.setNextRequest(requestName);
} 

```
