Update 12/22/17 Survey Monkey requires you to alter the OAuth redirect url and update the scopes before you can deploy your app. I’ve updated the post to reflect these changes.
Update 3/31/17: Survey Monkey now provides an access token for 90 days when you create an app. So unless you need to use the API for more than 90 days and you have a Platinum plan, then you can just read these long story short instructions.
Long Story Short
When we talk about an “app” in our case, we’re just sort of faking it so that we can get an access token.
Instead of generating a “long lived” token (i.e., lasts more than 90 days), I would recommend just following these simple steps:
1. Go to My Apps and click Add a New App
https://developer.surveymonkey.com/apps/
2. Give your app a nickname
3. Set it to Public App
4. Click Create App
5. Click on Settings
6. Change the OAuth Redirect URL to anything (e.g., your personal site, https://google.com, etc.)
7. Click Submit Changes
8. Modify the scopes (e.g., set them to “Optional”>
9. Click Update Scopes
10. Click Deploy
11. Copy your access token and use it for authorization in your request
That’s it. You don’t need to do anything else in terms of the app set up and configuration for our purposes.
Now that you have your access token you can request your survey data.
If you like Python you could use this:
import requests
import json
client = requests.session()
headers = {
"Authorization": "bearer %s" % "YOUR_ACCESS_TOKEN",
"Content-Type": "application/json"
}
data = {}
HOST = "https://api.surveymonkey.net"
SURVEY_LIST_ENDPOINT = "/v3/surveys"
uri = "%s%s" % (HOST, SURVEY_LIST_ENDPOINT)
response = client.get(uri, headers=headers)
response_json = response.json()
#survey_list = response_json["data"]["surveys"]
print(response_json)
and replace “YOUR_ACCESS_TOKEN” with the access token in your app.
For cURL
curl -i -X GET -H "Authorization:bearer YOUR_ACCESS_TOKEN" -H "Content-Type": "application/json" https://api.surveymonkey.net/v3/surveys
Similarly, if you were using this in Power BI or Excel’s Get & Transform, you could
1. Get Data -> Web
2. Paste https://api.surveymonkey.net/v3/surveys into the web source field
3. Click Advanced
4. Select Authorization in the dropdown and then in the field enter Bearer YOUR_ACCESS_TOKEN
Recently Survey Monkey updated their REST API to version 3, which increases the difficulty of requesting data for use in your analysis tools (e.g., R, Power BI, etc.). The language and instructions used in this post should be easy enough for a non-developer (with patience) to understand. The goal is to allow Survey Monkey account administrators to create Survey Monkey apps that then allow authenticating and getting data from their Survey Monkey account.
Why would you want to do this? Well there could be several reasons but, for most people, the main benefit is to get real-time, updated data while eliminating the need to login to Survey Monkey to perform several manual steps to get your data.
There are two parts of this process: 1. Authentication, and 2. Getting the data
You only need to authenticate occasionally since you can use a long lived token. For most people, the authentication part is the most difficult. Once we generate a token, getting data is pretty straightforward.
High-Level Workflow
- Survey Monkey account administrator creates a Survey Monkey app (or uses an existing app)
- Using the information provided with that app, get an access token to use for authentication with a data analysis tool
- Once access token has been acquired, use data analysis tool to authenticate into Survey Monkey REST API
- Retrieve Survey list to get survey ID
- Retrieve survey responses
Authenticate
Note: for the purposes of this guide, we will use Postman. You could choose a number of other routes but I think this is one of the more simple approaches for data analysts.
- Log in to Survey Monkey
https://www.surveymonkey.com - Go to My Apps in the Developer area
https://developer.surveymonkey.com/apps/
- Either create a new app or use an existing app
- Click on Settings for your app and use this OAuth Redirect URL
https://www.getpostman.com/oauth2/callback
- Install Postman on your computer
https://www.getpostman.com/
Okay, this is where it gets complicated. I won’t go into details about why you have to use Postman. You could actually create a real app to interface with Survey Monkey but, since we just need to get data, this would be a lot of unnecessary work. So we fake it in order to get our access token using Postman. - Open Postman and enter basic info
a. Add this URL to GET
https://api.surveymonkey.net/oauth/authorizeb. Select OAuth 2.0
c. click Get New Access Token
- Now use the information from your Survey Monkey app in My Apps to complete the info and click Request Token
- It will prompt you to login using your Survey Monkey admin credentials and ask you to complete a captcha in order to Authorize
- At long last, we will get our token. Use access_token to authenticate in your data analysis tool
Note: you can create tokens that expire at a given date but that’s for another dayWhew! So with that out of the way, we can reuse this forever. You only have to do this once. Onward!
Examples
Power BI Authentication Example
- In Power BI Desktop, click on Get Data -> Blank Query
- Click on Advanced Editor and use this to get a list of surveys from your Survey Monkey account. The two parts of this that are important are the Survey Monkey API URL and you will replace “<<API-TOKEN>>” with the access token you generated using Postman.
let Source = Web.Contents("https://api.surveymonkey.net/v3/surveys", [Headers=[#"Authorization"="bearer <<API-TOKEN>>"]]), convertToJson = Json.Document(Source), toTable = Table.FromRecords({convertToJson}) in toTable
R Authentication Example
You will enter your access token you painstakingly generated in Postman in the space where it says “Enter your access token for SurveyMonkey” below:
sm_get_surveys <- function(auth_token, page = 1, per_page = 250) { if (missing(auth_token)) { auth_token <- readline('Enter your access token for SurveyMonkey: ') } auth <- paste("bearer", auth_token, sep=" "); url <- paste('https://api.surveymonkey.net/v3/surveys?page=', page, '&per_page=', per_page, sep='') survey_list_response <- httr::GET(url=url, add_headers("Content-Type" = "application/json", "Authorization" = auth )) if (survey_list_response$status_code != 200) { stop(paste('Bad response from server: ', httr::http_status(survey_list_response))) } json <- httr::content(survey_list_response, as = 'text') survey_list <- jsonlite::fromJSON(json) survey_list }
Get the Data
So after you authenticate, you have to request the data. I’ll show you how to get all of the responses for a survey but there are a lot of options available:
https://developer.surveymonkey.com/api/v3/
- Get survey list (list of survey ids)
https://api.surveymonkey.net/v3/surveys - Use a specific survey id from the list retrieved in #1 to get responses in bulk
https://api.surveymonkey.net/v3/surveys/{survey_id}/responses/bulk
Leave a Reply