title: How to get an Alexa userId?
body: <p>I'm building an Alexa Skill, and it requires that I store the userId of a user. I've tried to retrieve it with <code>event.session.user.userId</code>. However, when I call <code>console.log(event.session.user.userId)</code> the output is literally <code>amzn1.ask.account.[unique-value-here]</code>. I've looked at several similar questions, and none of them provide a clear enough answer for me.</p>
  <p>I'm not sure if this is a bug, a developer-only thing, or if the userId is simply anonymized. If so, is there a way to get the actual userId? I imagine there would be, since Amazon has written an entire guide on it here:</p>
  <blockquote>
  <p><a href="https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/linking-an-alexa-user-with-a-user-in-your-system" rel="nofollow noreferrer">https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/linking-an-alexa-user-with-a-user-in-your-system</a>.</p>
  </blockquote>
  <p>However, after a long day of debugging, I'm not sure what's real and what's not anymore.</p>
  <pre><code>var request = require('request');
  var firebase = require('firebase');
  var config = {
      apiKey: &quot;my-api-key&quot;,
      authDomain: &quot;stuff...&quot;,
      databaseURL: &quot;more stuff...&quot;,
      storageBucket: &quot;even more stuff...&quot;,
  };
  firebase.initializeApp(config);
  // Get a reference to the database
  var database = firebase.database();
  
  exports.handler = (event, context) =&gt; {
      try {
          // New session
          if (event.session.new) {
              // New Session
              console.log(&quot;NEW SESSION&quot;);
          }
  
          // Launch Request
          switch (event.request.type) {
              case &quot;LaunchRequest&quot;:
                  var url = &quot;https://api.random.org/json-rpc/1/invoke&quot;;
                  var myRequest = {
                      &quot;jsonrpc&quot;: &quot;2.0&quot;,
                      &quot;method&quot;: &quot;generateStrings&quot;,
                      &quot;params&quot;: {
                          &quot;apiKey&quot;: &quot;another-api-key&quot;,
                          &quot;n&quot;: &quot;1&quot;,
                          &quot;length&quot;: &quot;3&quot;,
                          &quot;characters&quot;: &quot;abcdefghijklmnopqrstuvwxyz0123456789&quot;
                      },
                      &quot;id&quot;: 24
                  }
                  var pin;
                  request.post(
                      url,
                      {json: myRequest},
                      function (error, response, body) {
                          if (!error &amp;&amp; response.statusCode == 200) {
                              console.log(event.session.user.userId); // **Here**, output is literally amzn1.ask.account.[unique-value-here] 
                              pin = body.result.random.data[0];
                              writeUserPin(pin);
                              var welcome = &quot;Welcome&quot;;
                              var pinStatement = &quot;Your 3 letter or number pin is: &quot; + processPinForSpeech(pin);
                              context.succeed(
                                  generateResponse(
                                      buildSpeechletReponse(welcome + pinStatement, true),
                                      {}
                                  )
                              );
                              console.log(pin);
                          }
                          else {
                              console.log(error);
                          }
                      }
                  );
                  console.log(&quot;LAUNCH REQUEST&quot;);
                  break;
              // Intent Request
              case &quot;IntentRequest&quot;:
                  console.log(&quot;INTENT REQUEST&quot;);
                  break;
  
              // Session Ended Request
              case &quot;SessionEndedRequest&quot;:
                  console.log(&quot;SESSION ENDED REQUEST&quot;);
                  break;
  
              default:
                  context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);
          }
      }
      catch (error) {
          context.fail(`Exception: ${error}`);
      }
  
  }
      // Helpers
  buildSpeechletReponse = (outputText, shouldEndSession) =&gt; {
      return {
          outputSpeech : {
              type: &quot;PlainText&quot;,
              text: outputText
          },
          shouldEndSession: shouldEndSession
      };
  }
  
  generateResponse = (speechletResponse, sessionAttributes) =&gt; {
      return {
          version: &quot;1.0&quot;,
          sessionAttributes: sessionAttributes,
          response: speechletResponse
      };
  }
  
  function writeUserPin(pin) {
      console.log(&quot;writing stuff&quot;);
      firebase.database().ref('newPins/' + pin).set({
          num : &quot;&quot;
      });
  }
  
  function processPinForSpeech(pin) {
      var wordNumArr = [&quot;zero&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;,
       &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;];
      processedPin = &quot;&quot;;
      for (i = 0; i &lt; pin.length; i++){
          var currentChar = pin.charAt(i);
          if (isNaN(Number(currentChar))){
              processedPin += currentChar + &quot;. &quot;;
          }
          else {
              processedPin += wordNumArr[Number(currentChar)] + &quot;. &quot;;
          }
      }
      return processedPin
  }
  </code></pre>
  <p>The following is the output on the CloudWatch logs:</p>
  <pre><code>
  16:16:19
  START RequestId: 48e335c5-d819-11e6-bc01-a939911adc24 Version: $LATEST
  
  16:16:19
  2017-01-11T16:16:19.639Z    48e335c5-d819-11e6-bc01-a939911adc24    NEW SESSION
  
  16:16:19
  2017-01-11T16:16:19.758Z    48e335c5-d819-11e6-bc01-a939911adc24    LAUNCH REQUEST
  
  16:16:20
  2017-01-11T16:16:20.457Z    48e335c5-d819-11e6-bc01-a939911adc24    amzn1.ask.account.[unique-value-here]
  
  16:16:20
  2017-01-11T16:16:20.457Z    48e335c5-d819-11e6-bc01-a939911adc24    writing stuff
  
  16:16:20
  2017-01-11T16:16:20.520Z    48e335c5-d819-11e6-bc01-a939911adc24    dd2
  
  16:16:20
  END RequestId: 48e335c5-d819-11e6-bc01-a939911adc24
  
  16:16:20
  REPORT RequestId: 48e335c5-d819-11e6-bc01-a939911adc24  Duration: 1005.48 ms    Billed Duration: 1100 ms Memory Size: 128 MB    Max Memory Used: 38 MB
  </code></pre>