title: Invoking Spotify API from lambda function
body: <p>I need to get data from Spotify API then send the response to the front-end. To avoid CORS issue and to hide key and secret from Spotify, I would like to use Lambda to make the API call then send back the response. To be more precise about my application:</p>
  
  <pre><code>1. FrontEnd &gt; API Gateway
  2. API Gateway &gt; Lambda
  3. Lambda &gt; Spotify API (request their API to get token)
  4. Spotify API &gt; Lambda (token in the response)
  5. Lambda &gt; API Gateway
  6. API Gateway &gt; FrontEnd
  </code></pre>
  
  <p>Spotify endpoint is:</p>
  
  <pre><code>https://accounts.spotify.com/api/token?grant_type=client_credentials 
  </code></pre>
  
  <p>Header is:</p>
  
  <pre><code>Content-Type: 'application/x-www-form-urlencoded'
  Authorization: 'Basic XXX'
  </code></pre>
  
  <p>So far I was able to do this using a Lambda function:</p>
  
  <pre><code>const https = require('https');
  exports.handler = async (event, context) =&gt; {
  
      return new Promise((resolve, reject) =&gt; {
          const options = {
            hostname: 'accounts.spotify.com',
            path: '/api/token?grant_type=client_credentials',
            method: 'POST',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Authorization': 'Basic XXX'
            }
          }
  
          const req = https.request(options, (res) =&gt; {
            res.on('data', function (chunk) {
              console.log('BODY: ' + chunk);
            });
            resolve('Success');
          });
  
          req.on('error', (e) =&gt; {
            reject(e.message);
          });
  
          // send the request
          req.write('');
          req.end();
      });
  };
  </code></pre>
  
  <p>But I can't get the response from the API:</p>
  
  <pre><code>{
      "access_token": "YYY",
      "token_type": "Bearer",
      "expires_in": 3600,
      "scope": ""
  }
  </code></pre>
  
  <p>And I don't know how to send the data back to the front-end. Do you have any guidance to achieve what I'm looking for?</p>
  
  <p>Edit: I also tried using axios as suggested:</p>
  
  <pre><code>const axios = require("axios");
  
  module.exports.handler = (event, context, callback) =&gt; {
      const headers = {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Authorization': 'Basic XXX'
            }
      axios.post('https://accounts.spotify.com/api/token?grant_type=client_credentials', {}, {
        headers: headers
      })
      .then(function(response) {
          console.log(response)
          callback(null, response);
      })
      .catch(function(err) {
         console.error("Error: " + err);
         callback(err);
      });
  };
  </code></pre>
  
  <p>But got the following error:</p>
  
  <pre><code>Response:
  {
    "errorType": "Error",
    "errorMessage": "Request failed with status code 400",
    "trace": [
      "Error: Request failed with status code 400",
      "    at createError (/var/task/node_modules/axios/lib/core/createError.js:16:15)",
      "    at settle (/var/task/node_modules/axios/lib/core/settle.js:17:12)",
      "    at IncomingMessage.handleStreamEnd (/var/task/node_modules/axios/lib/adapters/http.js:237:11)",
      "    at IncomingMessage.emit (events.js:215:7)",
      "    at endReadableNT (_stream_readable.js:1183:12)",
      "    at processTicksAndRejections (internal/process/task_queues.js:80:21)"
    ]
  }
  </code></pre>