Any luck using Python to import contacts? | XM Community
Skip to main content
I'm trying to import contacts via v3 of the api. I found some errors in their example script, and made some tweaks to get it to work properly with Python's requests module. When I run my script, I get a 200-OK response, but when I check on my import progress, I get that it is 100% complete, 0 were added, 0 updated, and 0 failed. And when I check my mailing list, nothing has been added.



If I rename one of my contact fields, I get a "400 - Bad Request" with an error message of "Invalid panelMember field provided", so I know I'm talking with the server.



Here's a minimum working sample to reproduce the problem: (just replace Token,MailingListID, and DataCenter)



```

import requests

import json







dict_Contacts = [

{

"id": "MLRP_123456789012345",

"firstName": "ExampleFirstName",

"lastName": "ExampleLastName",

"email": "example@example.com",

"language": "SP",

"unsubscribed":1,

"externalReference":"Example",

"embeddedData": {"ExampleField1":"example_field_val_1", "ExampleField2":"example_field_val_2"}

}

]







Token = 'My_Token'

MailingListID = 'My_Mailing_List'

dataCenter = 'My_Data_Center'



url = "https://{0}.qualtrics.com/API/v3/mailinglists/{1}/contactimports/".format(dataCenter, MailingListID)

headers = {

'x-api-token': Token,

'contentType':'application/json'

}

json_string = json.dumps(dict_Contacts)

files = {'contacts': ('contacts', json_string, 'application/json')}

request = requests.post(url, headers=headers, json={'contacts':dict_Contacts})

print(request.text)

```
I found the solution. If you are importing a new contact, don't include an `id`. If a contact has an `id` that doesn't match any of the `id`s in the mailing list, Qualtrics skips the contact, rather than importing it as a new one.

Leave a Reply