title: How to return a string from a node js api call
body: <p>I have using a code snippet which will return a value  after post rest call to an api.</p>
  
  <p>But where ever i am calling the function its not returning the value and  prints undefined.</p>
  
  <p>when ever i will call any where getAccessToken(), its says undefiuned, but ifi print the value am getting the output.</p>
  
  <p>How do the called will get the return value, do i need to change anything in the below code. 
  Thanks</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var getAccessToken = exports.getAccessToken = function (res) { 
      // body...
   const request = require('request');
   const authKey='EAcEa4o4SkBLo9IpZpW4Y7oDn7d6b30GlouNh28pJ6Q='
   const ContentType='application/x-www-form-urlencoded' ;
   var postData={
     'grant_type':'client_credentials'
   };
   const options = {  
       url: 'https://xyz/v1/login',
       method: 'POST',
       headers: {
           'Content-Type': ContentType,
           'Authorization':authKey 
        },
       body:require('querystring').stringify(postData)
   };
   var token;
   request(options, function(errror, response, body) {  
    //console.log(JSON.parse(body));
         token= JSON.parse(body).access_token;
   });
   return token;
  }</code></pre>
  </div>
  </div>
  </p>