Host a Custom Skill as a Web Service


You can build a custom skill for Alexa by implementing a web service that accepts requests from and sends responses to the Alexa service in the cloud.

The web service must meet certain requirements to handle requests sent by Alexa and adhere to the Alexa Skills Kit interface standards. For more information, see the general documentation.

ASK SDK Express Adapter

The Alexa Skills Kit SDK (ASK SDK) for Node provides boilerplate code for request and timestamp verification through the ask-sdk-express-adapter package. This package provides the verification components by exporting the SkillRequestSignatureVerifier class and TimestampVerifier class. Also this package provides ExpressAdapter class which assembles the verification ability and skill invocation ability, making it easier to register requestHandlers on your express application.

Installation

You can add the ask-sdk-express-adapter package to your skill project through NPM: npm install --save ask-sdk-express-adapter.

For web application with express framework

The ExpressAdapter class registers the skill instance from the SkillBuilder object, and provides a getRequestHandlers method that return an array of request handlers which can be registered to your Express application.

You can enable or disable request or timestamp verification for testing purposes by setting the boolean parameters verifySignature and verifyTimeStamp on the ExpressAdapter instance. You can also provide additional custom verifiers that need to be applied on the input request before skill invocation.

Usage

Copied to clipboard.

const express = require('express');
const { ExpressAdapter } = require('ask-sdk-express-adapter');

const app = express();
const skillBuilder = Alexa.SkillBuilders.custom();
const skill = skillBuilder.create();
const adapter = new ExpressAdapter(skill, true, true);

app.post('/', adapter.getRequestHandlers());
app.listen(3000);

Copied to clipboard.

import express from 'express';
import { ExpressAdapter } from 'ask-sdk-express-adapter';

const app = express();
const skillBuilder = Alexa.SkillBuilders.custom();
const skill = skillBuilder.create();
const adapter = new ExpressAdapter(skill, true, true);

app.post('/', adapter.getRequestHandlers());
app.listen(3000);

For web application without Express framework

If you don't use Express framework, the SkillRequestSignatureVerifier and TimestampVerifier are provided for you to use in your web application.

Usage

Copied to clipboard.

const { SkillRequestSignatureVerifier, TimestampVerifier } = require('ask-sdk-express-adapter');

const skillBuilder = Alexa.SkillBuilders.custom();
const skill = skillBuilder.create();

// This code snippet assumes you have already consumed the request body as text and headers
try {
    await new SkillRequestSignatureVerifier().verify(textBody, requestHeaders);
    await new TimestampVerifier().verify(textBody);
} catch (err) {
    // server return err message
}
const response = skill.invoke(JSON.parse(textBody));
// server send response in Json format

Copied to clipboard.

import { SkillRequestSignatureVerifier, TimestampVerifier } from 'ask-sdk-express-adapter';

const skillBuilder = Alexa.SkillBuilders.custom();
const skill = skillBuilder.create();

// This code snippet assumes you have already consumed the request body as text and headers
try {
    await new SkillRequestSignatureVerifier().verify(textBody, requestHeaders);
    await new TimestampVerifier().verify(textBody);
} catch (err) {
    // server return err message
}
const response = skill.invoke(JSON.parse(textBody));
// server send response in Json format

Was this page helpful?

Last updated: Nov 28, 2023