Asterisk REST Interface (ARI) Client

2.2.0 · active · verified Tue Apr 21

The `ari-client` package provides a JavaScript client library for interacting with the Asterisk REST Interface (ARI). It offers a higher-level, Asterisk-specific API built upon the underlying `swagger-js` library, simplifying interaction with Asterisk resources such as bridges, channels, endpoints, and playback objects. The library supports both callback-based and Promise-based asynchronous operations for connecting to ARI, listing resources, performing actions (like adding channels to bridges), and creating new resource instances (e.g., `ari.Bridge()`, `ari.Channel()`). It is currently at version 2.2.0, indicating a stable release. While a strict release cadence isn't explicitly stated, its active development suggests ongoing maintenance. Its key differentiator is abstracting the raw Swagger API into an intuitive, resource-oriented interface tailored specifically for Asterisk development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to Asterisk ARI, listing bridges, creating a new channel, and handling Stasis events using Promises.

const client = require('ari-client');

const url = process.env.ARI_URL ?? 'http://localhost:8088/ari';
const username = process.env.ARI_USERNAME ?? 'asterisk';
const password = process.env.ARI_PASSWORD ?? 'asterisk';

async function main() {
  try {
    console.log(`Connecting to ARI at ${url}...`);
    const ari = await client.connect(url, username, password);
    console.log('Successfully connected to ARI.');

    // List all active bridges
    console.log('Listing existing bridges...');
    const bridges = await ari.bridges.list();
    console.log(`Found ${bridges.length} bridge(s).`);
    bridges.forEach(b => console.log(`  Bridge ID: ${b.id}, Type: ${b.bridge_type}`));

    // Create a new channel and listen for events
    const channel = ari.Channel();
    channel.on('StasisStart', (event, channelInstance) => {
      console.log(`Channel ${channelInstance.id} entered Stasis application.`);
      // Perform actions with the channel, e.g., answer, play media
      channelInstance.answer()
        .then(() => channelInstance.play({media: 'sound:hello-world'}))
        .then(playback => console.log(`Playing sound: ${playback.id}`))
        .catch(err => console.error(`Error playing sound: ${err.message}`));
    });
    channel.on('ChannelDtmfReceived', (event, channelInstance) => {
      console.log(`DTMF received on channel ${channelInstance.id}: ${event.digit}`);
    });
    channel.on('StasisEnd', (event, channelInstance) => {
      console.log(`Channel ${channelInstance.id} left Stasis application.`);
    });

    console.log('Originating a new channel...');
    const originatedChannel = await channel.originate({
      endpoint: 'PJSIP/1000',
      app: 'my-stasis-app',
      appArgs: 'dialed'
    });
    console.log(`Channel ${originatedChannel.id} originated. Waiting for events...`);

    // Keep the process alive to receive events (in a real app, use a proper event loop)
    // setTimeout(() => { console.log('Exiting after 60 seconds.'); process.exit(0); }, 60000);

  } catch (err) {
    console.error(`Failed to connect or interact with ARI: ${err.message}`);
    process.exit(1);
  }
}

main();

view raw JSON →