title: Nodejs Capture response from API call
body: <p>I am new to node js. I am trying to call an API that will give me a URL, then I will use this URL to do a number of things like send an SMS, email, insert to mySql DB.</p>
  
  <p>API call is working fine, when I log the URL inside request block (see code commented with WORKING FINE)</p>
  
  <p>But when I try to log the URL outside the request block, it does not work. (See code commented with NOT WORKING FINE). This where I want to do all the things with URL.</p>
  
  <p>Please help. 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 body = 
  {
   "mobile": Mobile,
   "policy": Policy_number,
   "name": Policy_Holder_Name,
   "docs": Docs_Pending,
   "target": 'mobile'
  };
     
  var options =
  {
   uri: BaseURL,
   method: 'POST',
   headers: header,
   json: body
  };
           
  var URL = '';
  
  request(options, function (error, response, body) {
    if (!error &amp;&amp; response.statusCode == 200) {
   URL = response.body.url;
   console.log(URL); //WORKING FINE
    }
  });
  
  console.log(URL);    //NOT WORKING FINE</code></pre>
  </div>
  </div>
  </p>