Use the Alexa HTML SDK

The Alexa HTML SDK is a JavaScript library that your web app uses to communicate with your skill and to respond to events on the device. Review the following information to learn how to load the SDK, create the Alexa client, and use the most common parts of the API.

For the full list of classes, interfaces, properties, and types, see Alexa HTML SDK Reference.

Load the SDK

The Alexa HTML SDK is a single file, alexa-html.js. To load the SDK, add a script tag to the head element of your HTML page.

Copied to clipboard.

<head>
    <script src="https://cdn.html.games.alexa.a2z.com/alexa-html/latest/alexa-html.js"></script>
</head>

After the script loads, the SDK is available through the global Alexa namespace.

Choose a version

The current version of the Alexa HTML SDK API is 1.1. To use version 1.1, pass version: '1.1' to Alexa.create. If you don't specify a version, the SDK uses the latest version. If the device doesn't support the version you request, Alexa.create fails with the no-such-version error code.

Your skill can check the highest version that a device supports before it starts the web app. The device reports the version in the runtime.maxVersion property of Alexa.Presentation.HTML in supportedInterfaces. For more details, see Build Your Alexa Skill for Web App Games.

Create the Alexa client

Your web app communicates with the device and your skill through the Alexa Client object. To prevent your app from using the client before it's ready, create it with the Alexa.create factory function. Call Alexa.create only one time on each page.

The following example creates the client and saves it for later use.

Copied to clipboard.

let alexaClient;

Alexa.create({ version: '1.1' })
    .then(({ alexa, message }) => {
        alexaClient = alexa;
        // message contains the data property from the Start directive
    })
    .catch((error) => {
        console.error(`Alexa client not ready: ${error.code} ${error.message}`);
    });

Alexa.create returns a promise. When the client is ready, the promise resolves with an object that contains the following properties:

  • alexa – The Client object.
  • message – The value of the data property in the Alexa.Presentation.HTML.Start directive, if your skill set it.
  • createExtensionsMessageProvider – A function that you pass to Web API extensions. For more details, see Use extensions.

If Alexa.create fails, the promise rejects with an object that has a code property and a message property. The following table describes the error codes.

Code Description
no-such-version The device doesn't support the requested API version.
unauthorized-access The web app doesn't have access to the Alexa client.
too-many-requests The web app sent too many requests.
unknown An unknown error occurred.

For the full signature, see create and ErrorWithCode.

Alexa client

The Client object groups the SDK features into the following properties.

Property Use it to
skill Send messages to your skill and receive messages from it.
speech Respond when Alexa starts and stops speaking.
voice Respond when the microphone opens and closes.
capabilities Check device features, such as microphone support and available extensions.
performance Check available memory during development.
version Get the API version that the client uses.

For more details, see Client.

Determine device capabilities

Devices differ in how users open the microphone. To decide how to prompt the user to speak, check the capabilities.microphone object.

The following example shows a check for PushToTalk capability using a Microphoneobject.

Copied to clipboard.

const { microphone } = alexaClient.capabilities;

if (microphone.supportsPushToTalk) {
    // Tell the user to press the microphone button on the device or remote
} else if (microphone.supportsWakeWord) {
    // Tell the user to say the wake word
}

To determine whether the device supports a Web API extension, look up the extension in capabilities.extensions. For an example, see Use extensions.

Send messages to your skill

To send a message to your skill from your web app, call alexaClient.skill.sendMessage. Your skill receives the message in an Alexa.Presentation.HTML.Message request. The message can be any JavaScript value that your web app and skill agree on.

The following example sends a message and retries it if the device throttles the request.

Copied to clipboard.

function sendToSkill(message) {
    alexaClient.skill.sendMessage(message, (response) => {
        const { statusCode, reason, rateLimit } = response;
        if (statusCode === 429) {
            setTimeout(() => sendToSkill(message), rateLimit.timeUntilNextRequestMs);
        } else if (statusCode !== 200) {
            console.error(`sendMessage failed: ${statusCode} ${reason}`);
        }
    });
}

sendToSkill({ type: 'gameStarted' });

The callback receives a MessageSendResponse object. The statusCode property uses HTTP status codes, as described in the following table.

Status code Description
200 The message was sent.
401 The web app isn't authorized to send the message.
429 The web app exceeded the rate limit. Wait for the time in rateLimit.timeUntilNextRequestMs, and then try again.
500 An internal or unknown error occurred.

For more details about rate limit values, see RateLimit.

Receive messages from your skill

Your skill sends messages to the web app with the Alexa.Presentation.HTML.HandleMessage directive. To receive these messages, register a listener with alexaClient.skill.onMessage.

Your web app can register only one listener. If your skill sends different kinds of messages, handle each kind in the same listener. The web app doesn't receive messages until you register the listener. To get data from your skill at startup, use the message property that Alexa.create returns.

The following example shows the message listener function registration and implementation.

Copied to clipboard.

alexaClient.skill.onMessage((message) => {
    // Process message from your skill
    switch (message.type) {
        case 'scoreUpdate':
            updateScore(message.score);
            break;
        case 'gameOver':
            showGameOver();
            break;
    }
});

Respond to speech and microphone events

The device notifies your web app when Alexa speaks and when the microphone opens or closes. Use these events to lower game audio, pause animations, or show a listening indicator. Web audio doesn't lower its volume automatically when Alexa speaks or listens.

The device sends microphone events whether the user or your skill opened the microphone.

Copied to clipboard.

alexaClient.speech
    .onStarted(() => lowerGameVolume())
    .onStopped(() => restoreGameVolume());

alexaClient.voice
    .onMicrophoneOpened(() => {
        lowerGameVolume();
        showListeningIndicator();
    })
    .onMicrophoneClosed(() => {
        restoreGameVolume();
        hideListeningIndicator();
    });

For more details, see Speech, Voice, and Add Voice Control and Speech to the Web App.

Play Alexa speech in the web app

When your skill adds a transformer to the Start or HandleMessage directive, the web app receives a URL to an MP3 file of Alexa speech. The file contains speech marks, which give the timing of each word and sentence. To play the audio with Web Audio and sync visuals to the speech, call Alexa.utils.speech.fetchAndDemuxMP3. This function separates the audio from the speech marks.

If the download fails, the promise rejects with an error object. If the error object has a data property, data.audioBuffer contains audio that you can still play without speech marks.

Copied to clipboard.

const audioContext = new AudioContext();

async function playSpeech(url) {
    let audioBuffer;
    let speechMarks = [];
    try {
        ({ audioBuffer, speechMarks } = await Alexa.utils.speech.fetchAndDemuxMP3(url));
    } catch (error) {
        console.error(`fetchAndDemuxMP3 failed: ${error.statusCode} ${error.message}`);
        if (!error.data) {
            return;
        }
        audioBuffer = error.data.audioBuffer;
    }

    const source = audioContext.createBufferSource();
    source.buffer = await audioContext.decodeAudioData(audioBuffer);
    source.connect(audioContext.destination);
    source.start();

    speechMarks
        .filter((mark) => mark.type === 'word')
        .forEach((mark) => setTimeout(() => highlightWord(mark.value), Number(mark.time)));
}

For more details, see SpeechUtils, AudioData, SpeechMark, and FetchAndDemuxError.

Use extensions

Web API extensions add device features, such as smart motion and entity sensing, to your web app. Each extension is a separate library. To use an extension, check that the device supports it in capabilities.extensions, and then pass createExtensionsMessageProvider to the extension's create function. For an example, see About Web API Extensions for Games.

Check memory during development

To see how much memory is available on the device, call alexaClient.performance.getMemoryInfo. Use this information to tune asset sizes and debug memory issues across device types.

Copied to clipboard.

alexaClient.performance.getMemoryInfo()
    .then(({ availableMemoryInMB }) => console.log(`Available memory: ${availableMemoryInMB} MB`))
    .catch(({ message }) => console.warn(`Couldn't get memory info: ${message}`));

For more details, see Performance.

Handle errors

The SDK reports errors in the following ways:

  • Rejected promises – Alexa.create, getMemoryInfo, and fetchAndDemuxMP3 return promises that reject with an error object. Handle them with catch.
  • Status codes – sendMessage reports the result in the statusCode property of the callback response. It doesn't throw.

Was this page helpful?

Last updated: Nov 23, 2026