Neil Cho Neil Cho - 1 month ago 32
JSON Question

Survey Monkey API Query Parameters not working on Excel VBA

I have a wrapper function that calls Survey Monkey methods on Excel VBA:

Function SM_Method(https As String, method As String, query As Variant) As String
Dim objSM As Object
Set objSM = CreateObject("MSXML2.XMLHTTP.6.0")

With objSM
.Open https, charSMAPI & method & "?api_key=" & charAPIKey
.setRequestHeader "Authorization", "Bearer " & charToken
.setRequestHeader "Content-Type", "application/json"
.Send query
SM_Method = .responseText
End With
End Function


I call this function as below which specifies the API query to print out certain fields:

Dim Request As Variant
Request = "{""fields"":[""per_page""]" & "}"
MsgBox (SM_Method("GET", "/surveys", Request))


But the above message box also prints out other fields such as "total", "data", "page", "links" etc...

I would love to get the query parameters working and was wondering what was wrong with my code above. Your help is greatly appreciated!

Answer

Is the request you're trying to do:

/v3/surveys?api_key=<key>&fields=per_page

And expecting the fields returned to only be per_page (no data, or anything else)?

I don't think that'll work, I think the fields url parameter only works for a specific resource, not a resource list.

So if you wanted to filter out a specific survey, this would work:

/v3/surveys/<survey_id>?api_key=<key>&fields=title

But not for a resource list, it'll always return the total amount of items available, which page you're currently on, how many items per page, the links for pagination, and all the resource items under data.

EDIT: Explaining how to get survey responses

The docs for fetching survey responses are here. What you'd want to do based on your comment is:

GET /v3/surveys/<id>/responses?api_key=<key>&email=<email>&start_created_at=<begin_range>&end_created_at=<end_range>

This will get you all the responses (by ID) for a respondent with the provided email, and within the date range specified by and .

You can then fetch all the details of that response (every answer for every page) here:

GET /v3/surveys/<id>/responses/<response_id>?api_key=<key>

Given that you are using an email collector (that's how you have their email) and there should really only be one response ID returned in the list for that survey, unless they answered the survey more than once. If that's the case then you can do

GET /v3/surveys/<id>/responses/bulk?api_key=<key>&email=<email>&start_created_at=<begin_range>&end_created_at=<end_range>

Only, which with one request you can get all the responses including the answers for that given email in one request.

Comments