Somewhere in the overlap between software development, process improvement and psychology

How to call a webservice api from Amazon Alexa using Javascript in a Lambda function

This wasn’t trivial and I’ve seen a lot of questions about it online so I thought I’d share how I did this to get Alexa to respond to questions about my kids pocket money 🙂

You can call a URL like this:
(note I’m assuming a JSON response, you’d need to modify for non-JSON)

var http = require('http');

function getWebRequest(url,doWebRequestCallBack) {
    http.get(url, function (res) {
        var webResponseString = '';
        //console.log('Status Code: ' + res.statusCode);

        if (res.statusCode != 200) {
            doWebRequestCallBack(new Error("Non 200 Response"));

        res.on('data', function (data) {
            webResponseString += data;

        res.on('end', function () {
            //console.log('Got some data: '+ webResponseString);
            var webResponseObject = JSON.parse(webResponseString);
            if (webResponseObject.error) {
                //console.log("Web error: " + webResponseObject.error.message);
                doWebRequestCallBack(new Error(webResponseObject.error.message));
            } else {
                //console.log("web success");
                doWebRequestCallBack(null, webResponseObject);
    }).on('error', function (e) {
        //console.log("Communications error: " + e.message);
        doWebRequestCallBack(new Error(e.message));

You can then use it like this from inside an intent handler:

function getPocketMoney(intent, session, callback) {

    let shouldEndSession = false;
    let repromptText = null;
    let speechOutput = '';
    let cardTitle = '';

    let url = "http://mypocketmoneyservice/getpocketmoney?person=mike";

    getWebRequest(url, function webResonseCallback(err, data) {
        if (err) {
            speechOutput = "Sorry I couldn't connect to the server: " + err;
        } else {
            //something like this
            const balance = data.pocketmoney.person.balance;
            speechOutput = `${person} has £${balance} pocket money.`;
            callback({}, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));

5 responses

  1. Andrew Henson

    Thanks for your tutorial. I need to build a webservice that queries a database.

    Are there any special requirements for a webservice thats being called by an alexa skill?

    I am thinking of having the database and web service hosted at amazon which should improve the response performance.



    February 11, 2017 at 10:32 am

    • No special requirements really. If you’re using your own endpoint then it must be https (and not self-signed), otherwise if your in lambda and using AWS then it’s all done for you 🙂

      February 11, 2017 at 5:07 pm

  2. michael budisch

    could you pls share the whole skill code ? i still dont get it to work with the snipped 😦

    September 10, 2017 at 5:26 pm

  3. Pulkit

    thanks, its working perfectly just tell me one thing how can i use post request bcz i am not able to save my data to my server?

    December 30, 2017 at 11:29 am

What do you think?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s