|
What is an API Key? Where can I get an API Key?
An application programing interface key (API key) is a code generated by websites that allow users to access
their application programming interface.
To generate an API key, go to:
Login >> Surveys >> Reports >> Export Data
Click on Get API Key and the key will be generated. Use this key for setting up API integration.

How does API integration work?
Based on parameters received through a JSON request body, the API will fetch responses stored.
These responses will be returned in the form of JSON. To access the API we need to pass the access key and the specific API call to be made.
We also need to pass attributes of the request as a JSON body which will contain at the least the survey ID for which we want to fetch responses.
We can add additional attributes to the JSON body to further refine the request.(eg. Start Date and End Date etc)
API Calls available:
- questionpro.survey.surveyResponses - To retrieve responses based on the filtering criteria
- questionpro.survey.responseCount - To get total response count for the specified criteria
- Base URL : http://api.questionpro.com/a/api/
- Access key parameter name : accessKey
- Example API call URL:
http://api.questionpro.com/a/api/questionpro.survey.surveyResponses?accessKey=qthqs
We serve only 100 responses per API call
- Parameters to specify as criteria in the JSON body:
Sample Java Code:
package com.apicall;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
public class QuestionProAPIExample {
public static final String API_ACCESS_KEY = "wvcfq";
public static final String API_URL = "http://api.questionpro.com/a/api/";
public static final String RESPONSE_COUNT_METHOD_NAME = "questionpro.survey.responseCount";
public static final String SURVEY_RESPONSES_METHOD_NAME = "questionpro.survey.surveyResponses";
public long surveyID = 3190054;
public long getResponseCount() throws Exception {
String path = API_URL + RESPONSE_COUNT_METHOD_NAME + "?accessKey=" + API_ACCESS_KEY;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);
String data = "{\"id\":\"" + surveyID + "\",\"resultMode\":\"0\"}";
StringEntity se = new StringEntity(data);
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
Object resp = httpclient.execute(httpost, responseHandler);
String json = resp.toString();
JSONObject obj = new JSONObject(json);
obj = obj.getJSONObject("response");
return Long.parseLong(obj.getString("partialCount"));
}
public String getResponses(int startingResponseCounter) throws Exception {
String path = API_URL + SURVEY_RESPONSES_METHOD_NAME + "?accessKey=" + API_ACCESS_KEY;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);
String data = "{\"id\":\"" + surveyID + "\",\"resultMode\":\"0\",\"startDate\":\"08/15/2012\"," +
"\"endDate\":\"09/15/2012\",\"startingResponseCounter\":\""+ startingResponseCounter +"\"}";
StringEntity se = new StringEntity(data);
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
Object resp = httpclient.execute(httpost, responseHandler);
return resp.toString();
}
public void printResponses() throws Exception {
long totalResponses = getResponseCount();
int count = 0;
int pageIndex = 0;
do {
String responsesJson = getResponses(pageIndex);
JSONObject jr = new JSONObject(responsesJson);
jr = jr.getJSONObject("response");
if (pageIndex == 0) {
JSONObject stats = jr.getJSONObject("surveyStatistics");
} else {
}
JSONArray responses = jr.getJSONArray("responses");
for (int i=0; i < responses.length();i++) {
JSONObject obj = (JSONObject)responses.get(i);
JSONArray responseSet = obj.getJSONArray("responseSet");
for (int j=0;j < responseSet.length();j++) {
JSONObject questionResponse = (JSONObject)responseSet.get(j);
JSONArray answerValues = questionResponse.getJSONArray("values");
for (int k=0; k < answerValues.length();k++) {
JSONObject value = (JSONObject)answerValues.get(k);
JSONObject ansVal = value.getJSONObject("value");
String scale = (String) (ansVal != null ? ansVal.has("scale")
? ansVal.get("scale") : null : null);
String otherText = (String) (ansVal != null ? ansVal.has("other")
? ansVal.get("other") : null : null) ;
String dynamicText = (String) (ansVal != null ? ansVal.has("dynamic")
? ansVal.get("dynamic") : null :null);
String valTxt = (String) (ansVal != null ? ansVal.has("text")
? ansVal.get("text") : null : null);
String dateTimeText = (String) (ansVal != null ? ansVal.has("dateTime")
? ansVal.get("dateTime") : null : null);
String resultText = (String) (ansVal != null ? ansVal.has("result")
? ansVal.get("result") : null : null);
String fileLinkText = (String) (ansVal != null ? ansVal.has("fileLink")
? ansVal.get("fileLink") : null : null);
if (scale != null)
//Incase the answer options included Other Option
if (otherText != null)
//Incase of dynamic textbox
if (dynamicText != null)
//Stored Answer Text
if (valTxt != null)
//for date time questions
if (dateTimeText != null)
//For Constant Sum and Rank Order questions
if (resultText != null)
// For File Upload questions
if (fileLinkText != null)
}
}
}
pageIndex++;
count = pageIndex * 100;
} while (count < totalResponses);
}
}
|