Skip to content

Instantly share code, notes, and snippets.

@FedericoTartarini
Last active February 8, 2024 17:13
Show Gist options
  • Save FedericoTartarini/9496282b4b2f508c0ab2da96f4955397 to your computer and use it in GitHub Desktop.
Save FedericoTartarini/9496282b4b2f508c0ab2da96f4955397 to your computer and use it in GitHub Desktop.
Automatically download Qualtrics survey responses using Python and Qaultrics API.
import requests
import zipfile
import io
def get_qualtrics_survey(dir_save_survey, survey_id):
""" automatically query the qualtrics survey data
guide https://community.alteryx.com/t5/Alteryx-Designer-Discussions/Python-Tool-Downloading-Qualtrics-Survey-Data-using-Python-API/td-p/304898 """
# Setting user Parameters
api_token = "<your token>"
file_format = "csv"
data_center = '<your ID>' # "<Organization ID>.<Datacenter ID>"
# Setting static parameters
request_check_progress = 0
progress_status = "in progress"
base_url = "https://{0}.qualtrics.com/API/v3/responseexports/".format(data_center)
headers = {
"content-type": "application/json",
"x-api-token": api_token,
}
# Step 1: Creating Data Export
download_request_url = base_url
download_request_payload = '{"format":"' + file_format + '","surveyId":"' + survey_id + '"}' # you can set useLabels:True to get responses in text format
download_request_response = requests.request("POST", download_request_url, data=download_request_payload, headers=headers)
progress_id = download_request_response.json()["result"]["id"]
# print(download_request_response.text)
# Step 2: Checking on Data Export Progress and waiting until export is ready
while request_check_progress < 100 and progress_status != "complete":
request_check_url = base_url + progress_id
request_check_response = requests.request("GET", request_check_url, headers=headers)
request_check_progress = request_check_response.json()["result"]["percentComplete"]
# Step 3: Downloading file
request_download_url = base_url + progress_id + '/file'
request_download = requests.request("GET", request_download_url, headers=headers, stream=True)
# Step 4: Unzipping the file
zipfile.ZipFile(io.BytesIO(request_download.content)).extractall(dir_save_survey)
print('Downloaded qualtrics survey')
if __name__ == "__main__":
path = "<Path where to save file>"
get_qualtrics_survey(dir_save_survey = path, survey_id = my_secrets.survey_id)
@leonardoulloa21
Copy link

Hi Federico!

Great solution, have you tried to use the update api also? Could your recommed me any aproach?

@FedericoTartarini
Copy link
Author

No unfortunately I have never tried to use the update API. What are you trying to do? If you share more information I can then look into it.

@erjenvannierop
Copy link

Thanks for this code.

When using it, an important field, duration_in_seconds, is not exported. I read on https://api.qualtrics.com/6b00592b9c013-start-response-export that surveyMetadataIds can be set (not sure how), but by default, it should export them all. I am getting quite a few, but not the duration.

Is there any way I can add this?

@santiagomunozc
Copy link

Hi erjenvannierop,
I'm facing the same issue, not all my fields are being exported. By any chance, have you been able to solve it?

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment