title: Alexa smart home skill: problem with discover devices
body: <p>I have problems with the discovery of devices of my Alexa Smart Home skill.</p>
  
  <p>Steps which work:</p>
  
  <ul>
  <li>activate Alexa skill</li>
  <li>OAuth login screen appears. After successful login, the discovery of devices is triggered</li>
  <li>in the lambda function I get the bearer token which I use to call the .../devices endpoint</li>
  <li>I get the devices from the REST endpoint and construct the payload as described in <a href="https://developer.amazon.com/de/docs/smarthome/steps-to-build-a-smart-home-skill.html" rel="nofollow noreferrer">https://developer.amazon.com/de/docs/smarthome/steps-to-build-a-smart-home-skill.html</a></li>
  <li>The payload (same structure as in the example) is provided to context.succeed </li>
  </ul>
  
  <p>My problem:
  <strong>After the Alexa Skill returns from discovery of devices task, no new devices are visible in the Alexa Skill.</strong></p>
  
  <p>When I use the code from the sample (where no request to an external Rest API happens), the device is visible in the Alexa skill after the Alexa discovery task.</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 https = require('https');
  const AWS = require('aws-sdk');
  
  exports.handler = function(request, context) {
  var options = {
   method: 'GET',
   hostname: 'xyz.azurewebsites.net',
   path: '/devices',
   headers: {
    Authorization: 'Bearer ' + request.directive.payload.scope.token,
    'Content-Type': 'application/json'
   }
  };
  
  var req = https.get(options, (response) =&gt; {
   var data = '';
   response.setEncoding('utf8');
   response.on('data', function(x) { data += x; } );
   response.on('error', console.error);
   response.on('end', () =&gt; {
    var dataObj = JSON.parse(data);
    console.log("Retrieved response: " + JSON.stringify(dataObj.items));
  
    const payload = {
      "endpoints": []
    };
    dataObj.items.forEach(item =&gt; {
     const device = {
       "endpointId": item.id,
       "manufacturerName": item.manufacturer,
       "friendlyName": item.displayName,
       "description": item.description,
       "displayCategories": ["SWITCH"],
       "cookie": {
         "key1": "arbitrary key/value pairs for skill to reference this endpoint.",
         "key2": "There can be multiple entries",
         "key3": "but they should only be used for reference purposes.",
         "key4": "This is not a suitable place to maintain current endpoint state."
       },
       "capabilities":
       [
         {
          "type": "AlexaInterface",
          "interface": "Alexa",
          "version": "3"
         },
         {
           "interface": "Alexa.PowerController",
           "version": "3",
           "type": "AlexaInterface",
           "properties": {
             "supported": [{
               "name": "powerState"
             }],
              "retrievable": true
           }
         }
       ]
     };
     payload.endpoints.push(device);
    });
    console.log('payload ' + JSON.stringify(payload));
  
    var header = request.directive.header;
    header.name = "Discover.Response";
    console.log("DEBUG", "Discovery Response: ", JSON.stringify({ header: header, payload: payload }));
    //NEXT LINE IS EXECUTED WITHOUT ANY ERROR
    context.succeed({ event: { header: header, payload: payload } });
   }); 
  });
  req.on('error', (e) =&gt; {
    console.log('problem with request: ' + e.message);
  });
  };</code></pre>
  </div>
  </div>
  </p>