Build an Alexa endpoint in Express – Tutorial Post

Here at Peck we write voice apps for clients that run on Alexa and Google Assistant devices.

Most of the time our java backend handles the conversion of questions like “What is the weather in Encinitas?” to “Its sunny and 76 degrees” very well.

However sometimes we need to deploy some new ideas that we aren’t sure are going to make it into our production code. Writing a lambda backend is a pretty simple solution that many use, but often we just want to use express.

Recently I had to do this and had the opportunity to use a great node library “alexa-verifier-middleware”. Its pretty straight forward and here is an example if you ever have to do this.

import * as path from 'path';
import * as express from 'express';
import * as bodyParser from 'body-parser';
var cors = require('cors');
var verifier = require('alexa-verifier-middleware')
const app = express();
app.set("port", process.env.PORT || 3000);
let alexaRouter = express.Router();
app.use('/api/v1/alexa', alexaRouter);
alexaRouter.use(verifier);
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(app.get("port"), () => {
  console.log(("running at http://localhost:%d in %s mode"), app.get("port"), app.get("env"));
  console.log("press CTRL-C to stop\n");
});
alexaRouter.post("/query", function (req, res) {
  console.log('/');
  return res.json({
    speech: 'Its so perfect!',
    displayText: 'It is!',
    fulfillmentText: 'Moop',
    source: 'some source somewhere...'
  });
}););
module.exports = app;

Hopefully this helps you work around the inevitable restrictions of the code that’s already there, and makes Peck more accessible. Leave a comment below if you have any questions!

Enjoy,
Jimmy Schwarz – Product/Developer Peck, Inc.

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *