NodeJS code example for calling SurveyMonkey API

Posted By: Matpal - November 10, 2019
Surveymonkey allows you to access all your surveys and associated features like collectors, users, groups, etc via their RESTful API. Before accessing the API you need to make sure followings - 

  1. Create an app in SurveyMonkey 
  2. Get access token 
  3. Call API endpoint via your favorite code library 

Following is an example of Node JS code using https -


Nodejs code

       var https = require('https');
     
       var accessToken='YourAccessToken'

    var options = 
    {
        hostname: 'api.surveymonkey.com',
        path: '/v3/surveys', 
        method: 'GET',
        port: '443',
        headers: {
            'Content-Type' : 'application/json',
            'Authorization' : 'Bearer ' + accessToken
        }
        
    };
       
        var body = '';

    var req = https.request(options, (res) => 
{
       
        //console.log('Response is '+res.statusCode);

        res.on('data', function (chunk) 
        {
               body += chunk;
         });

        res.on('end', function () 
        {
             console.log(body);
             
             callback(null,body);
        });


    
});

req.on('error', (e) => 
{
  console.error("There is some error "+e);
   callback(e);
});

req.end();

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.