Export OptOut Contacts via api and python | XM Community
Skip to main content

Hello,

I have the following code, but can't seem to figure out why I can't export all my optout customers.
# List Contacts in Mailing List

import time
from datetime import date

import null as null
import pandas as pd
import requests

# Setting user Parameters
from pandas.io import json

rm = []
today = date.today()
filename = f"contactsDowloaded_{today}"
apiToken = ""
dataCenter = ""

directoryId = ""

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

count = 0
while nextPage is not None:
  time.sleep(.1)
  if count % 25 == 0:
    print(count)
  time.sleep(.5)
  response = requests.get(nextPage, headers=headers)
  time.sleep(.1)
  json_data = json.loads(response.text)
  time.sleep(.1)
  nextPage = json_data'result'] 'nextPage']
  time.sleep(.1)
  df = pd.DataFrame(response.json().'result']('elements'])
  time.sleep(.1)
  if 'email' not in df.columns:
    print(count, df.columns)
  else:
    df_list = list(dfr'email'])
    rm += df_list
  count += 1
  time.sleep(.2)



pd.DataFrame({'email': rm}).to_csv(f'FILE_PATH_LOCATION\\\\{filename}.csv', index=False)



I can pull about 40K contacts but need to get close to 200K?


Not sure if this has been answer already but wanted to get some insight on how others might be exporting this data.

If it's a one-time pull, may I recommend downloading the data manually from the directory contact?


RyanMasca
You can create a segment on the condition if the status is opted out. You can use the following API after the segment is refreshed. To specify a specific mailing list check if it includes a unique embedded data for that list.
Hope it helps!


Leave a Reply