How to store data into a downloadable csv-file? | XM Community
Question

How to store data into a downloadable csv-file?

  • 2 July 2020
  • 3 replies
  • 185 views

Userlevel 3
Badge +5

Hello,
After playing around for a few days I got the below code working now. I want to use API to download responses. This is done in three steps. The only thing I can't get working now is: where do I find the file containing the csv data. Or how can I export and download it to my local harddrive? What changes do I need to make to the code: (the API-token is not my real token and the surveyID is not my real SurveyID)
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var apikey = "mYaPiKeYfOrQuAlTrIcS0123";
var sId = "SV_8vrnABcdeFGH06i"

//Step 1
var data = JSON.stringify({
 "format": "csv",
 "compress": false,
 "exportResponsesInProgress": false
});

var xhr1 = new XMLHttpRequest();
xhr1.withCredentials = true;

xhr1.addEventListener("readystatechange", function () {
 if (this.readyState === this.DONE) {
var rs = JSON.parse(this.responseText);
var pId = rs.result.progressId;
var URL2 = "https://fra1.qualtrics.com/API/v3/surveys/"+sId+"/export-responses/"+pId;
Qualtrics.SurveyEngine.setEmbeddedData("progressId", rs.result.progressId);
  
//Step 2
var data = null;
var xhr2 = new XMLHttpRequest();

xhr2.withCredentials = true;
xhr2.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
var rs2 = JSON.parse(this.responseText);
var fId = rs2.result.fileId;
Qualtrics.SurveyEngine.setEmbeddedData("fileId", rs2.result.fileId);
  
//Step 3
var data = null;
var URL3 = "https://fra1.qualtrics.com/API/v3/surveys/"+sId+"/export-responses/"+fId+"/file";
var xhr3 = new XMLHttpRequest();

xhr3.withCredentials = true;
xhr3.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
var file = this.responseText;
}
Qualtrics.SurveyEngine.setEmbeddedData("URL3", URL3);
Qualtrics.SurveyEngine.setEmbeddedData("file", file);
 });
  
//request for step 3
xhr3.open("GET", URL3);
xhr3.setRequestHeader("x-api-token", apikey);
xhr3.send(data);
 //
 }
});
// request for step 2
  xhr2.open("GET", URL2);
xhr2.setRequestHeader("x-api-token", apikey);
xhr2.send(data);
 }
});

//request for step 1
xhr1.open("POST", "https://fra1.qualtrics.com/API/v3/surveys/"+sId+"/export-responses");
xhr1.setRequestHeader("x-api-token", apikey);
xhr1.setRequestHeader("content-type", "application/json");
xhr1.send(data);
});

Many thanks in advance.
Kind regards, Nico


3 replies

Userlevel 4
Badge +18

Qualtrics has its own API to download responses in CSV format
Have you tried that?

Userlevel 3
Badge +5

Hello Mishraji,
Thank you for your response.
In my opinion for export to a .csv file I have to take 3 steps. regarding the API documentation. The information I found using the link below:
'https://api.qualtrics.com/guides/reference/responseImportsExports.json'
Up to now I can't get it to work in my environment to get a csv-file containing all the responses of a project.
So if you have a more detailed instruction this would be welcome.

Userlevel 4
Badge +18

Correct. However you have got the link wrong.
There are three steps(API's):

  1. Start Response Export: (In the API body, pass format as CSV. On successful execution, this will give you progress ID to be used in next API) https://api.qualtrics.com/instructions/reference/responseImportsExports.json/paths/~1surveys~1%7BsurveyId%7D~1export-responses/post

  2. Get Response Export Progress: (This will give you file ID to be used in next API) https://api.qualtrics.com/instructions/reference/responseImportsExports.json/paths/~1surveys~1%7BsurveyId%7D~1export-responses~1%7BexportProgressId%7D/get

  3. Get Response Export File: (On successful execution, save response to a file. Don't change the file name) https://api.qualtrics.com/instructions/reference/responseImportsExports.json/paths/~1surveys~1%7BsurveyId%7D~1export-responses~1%7BfileId%7D~1file/get

Leave a Reply