Getting more than 100 contacts with Qualtrics API using the nextPage URL

659 Views Asked by At

I successfully setup the Qualtrics API for exporting contacts from working with the example script from their website. My problem is the API only exports 100 contacts at a time. It seems if I use the the url from the nextPage field in my initial json export to do another API call I can get another 100 but that isn't ideal. I sometimes have lists with over 10,000 people and cannot manually work this.

I am a Python noob and would like to know how its possible to use the nextPage URL function to receive more than 100 responses at a time.

The API Call looks like this

# List Contacts in Mailing List

import requests

# Setting user Parameters

apiToken = "YOUR API TOKEN"
dataCenter = "YOUR DATACENTER"

directoryId = "POOL_123456"
mailingListId = "CG_123456"

baseUrl = "https://{0}.qualtrics.com/API/v3/directories/{1}/mailinglists/{2}/contacts".format(dataCenter, directoryId, mailingListId)
headers = {
    "x-api-token": apiToken,
    }

response = requests.get(baseUrl, headers=headers)
print(response.text)

And I receive a similar results to this with only 100 responses:

{
    "meta": {
        "httpStatus": "200 - OK",
        "requestId": "7de14d38-f5ed-49d0-9ff0-773e12b896b8"
    },
    "result": {
        "elements": [
            {
                "contactId": "CID_123456",
                "email": "[email protected]",
                "extRef": "1234567",
                "firstName": "James",
                "language": "en",
                "lastName": "Smith",
                "phone": "8005552000",
                "unsubscribed": false
            },
            {
                "contactId": "CID_3456789",
                "email": "[email protected]",
                "extRef": "12345678",
                "firstName": "John",
                "language": "en",
                "lastName": "Smith",
                "phone": "8005551212",
                "unsubscribed": true
            }
        ],
        "nextPage": null
    }
}

Does anyone have an idea how I can loop the nextPage information to get an entire list of contacts, no matter how many sets of 100 are contained? I have some lists where there are tens, hundreds, and thousands of contacts and would like it to work for all.

Appreciate all input! Thanks!

1

There are 1 best solutions below

1
On

Use a while loop and rename baseUrl to nextPage:

nextPage = "https://{0}.qualtrics.com/API/v3/directories/{1}/mailinglists/{2}/contacts".format(dataCenter, directoryId, mailingListId)
 
while nextPage is not None:
      response = requests.get(nextPage, headers=headers)
      json_data = json.loads(response.text)
      #process json_data  
      nextPage = json_data['result']['nextPage']