Send a survey with our API
This guide shows how to send a survey to a specified recipient, you must supply content for all template fields required by your survey.
You will need
- A valid Esendex user (which you can create using our free trial).
- An active survey (contact your account manager to set up a survey and provide you with your survey ID).
- Permission for your Esendex user to access the
SMS
Survey API (your account manager can enable this by request). - An API password. Don't have one yet? You can generate one on your user profile.
Send survey code
var surveysService = new SurveysService("YourUsername", "YourApiPassword");
var templateFields = new Dictionary<string,string>{{"firstName", "John"},{"lastName", "Smith"}};
surveysService.Send("014356bc-790d-4441-a137-89c46c2688a7", "4412345678", templateFields);
$data = array("recipients" => [array(
"phonenumber" => "4412345678",
"templatefields" => array(
"firstname" => "John",
"lastname" => "Doe")
)]
);
$data_string = json_encode($data);
$ch = curl_init('https://surveys.api.esendex.com/v1.0/surveys/95082677-224B-4789-9452-BC8E391D75E1/send');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic am9zaHVhLmhhd3h3ZWxsQGVzZW5kZXguY29tOmVzZW5kZXg=',
'Content-Type: application/json',
'Host: surveys.api.esendex.com')
);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode == 200){
echo "Success!";
}else{
echo $httpCode;
}
UserPassword userPassword = new UserPassword("YourUsername","YourApiPassword");
BasicServiceFactory serviceFactory = ServiceFactory.createBasicAuthenticatingFactory(userPassword);
SurveysService surveysService = serviceFactory.getSurveysService();
RecipientRequest recipient = new RecipientRequest("44123456789");
TemplateField firstNameField = new TemplateField("firstname","John");
TemplateField lastNameField = new TemplateField("secondname","Smith");
List<TemplateField> templateFields = Arrays.asList(firstNameField, lastNameField);
recipient.setTemplateFields(templateFields);
try{
response = surveysService.Send("014356bc-790d-4441-a137-89c46c2688a7", recipient);
}catch(Exception e){
e.getStackTrace();
}
Handling the response
We will send you a HTTP Status Code
, depending on the result of your request:
HTTP Status Code | Reason |
---|---|
200 OK |
Your request was successful. |
400 Bad Request |
Contained malformed XML or JSON, an incorrect mobile number, was missing template fields or the survey was not active. |
401 Not Authorised |
No authentication header provided. |
403 Forbidden |
Failed authentication or not authorised to access feature. |
404 Not Found |
Survey doesn't exist. |
413 Payload Too Large |
Request contains more than one recipient. |
415 Unsupported Media Type |
Content type is not supported or unspecified. |
What you might to do with the response
Our successful response does not return any content, so there are no actions you need to take with this response. If you receive one of our error codes, you should check your request for errors that satisfy the response you received.
What you might want to do next
To check the responses to your survey, you can review the survey results report. Contact your account manager if you wish to change the frequency of this report or for ad hoc requests.