You can add answers to survey via API. This is commonly use with mobile application or web pages where survey form is build with own UI framework but answer must go to Surveypal survey.
Here is an example survey where we add answers later in this article: https://my.surveypal.com/form/preview?sid=1925235371&sh=SkuwZdu2wrZN5FbsHNts5ZJkeOm59Nlt79-PwU3wEOjYw_EDOoZ-fHm7_arVi3-3&showPreviewBar=true
Authentication
Surveypal REST API using Basic Auth for authentication: Read here how to generate credentials
If your Surveypal organization is created before 31.08.2022 then you can use also API key for authentication.
Basic Authentication and API key allow access to all surveys which are made or shared to Surveypal account which created API key. This is why it's good to create a separated integration user account and share surveys to this user.
Create an answer
First you need to create an answer
Documentation: https://developer.surveypal.com/docs/surveyidanswer
Example request:
PUT https://my.surveypal.com/api/rest/survey/1925235371/answer?source=external&state=incomplete&meta=[{"key":"exampledataname","value":"something"},{"key":"otherinformation","value":"123456789"}]
You can add answer background information with meta parameter:
- Key is background information name what you can select freely but avoid special characters.
- Value is background information value.
- You can add these key-value pairs as many as you need.
Above request returns new answer information, for example:
{
"answerId": 1926531445,
"hash": "9w1e4wYTT4cSVP3ByrqF5lCIdr3veb5ittj82_LJBFDzMB3cbabk6qJfIT2f7kBz",
"email": "anonymous@surveypal.com",
"url": "https://q.surveypal.com/Add-answer-to-survey-via-API-example?aid=1926531445&ah=9w1e4wYTT4cSVP3ByrqF5lCIdr3veb5ittj82_LJBFDzMB3cbabk6qJfIT2f7kBz"
}
You need to copy answerId value from this response and use it with next API request.
Add answers to survey questions
Documentation: https://developer.surveypal.com/docs/answer-id-data-put
Example request:
PUT https://my.surveypal.com/api/rest/answer/1926531445/data?elements={p0e0:6,p1e0:[0,2],p2e0:'Cup%20of%20coffee'}&state=complete
Above request adds these answers
- First page first question: value 6
- Second page first question: selects first and third options
- Third page first question: adds text "Cup of coffee"
Element identifier style is p0e0 where p is page index and e is element index. Index starts from zero.
Read how to find element identifier.
Note: If you want to add line break to open text field element answer then add \n without encoding. For example p2e0:'Cup\nof\ncoffee' will add line breaks after "Cup" and "of" words.