A TypeScript client library for the Asterisk Manager Interface (AMI) with automatic reconnection, keep-alive support, and typed responses for common AMI actions.
yarn add @gibme/asterisk-manager-interface
# or
npm install @gibme/asterisk-manager-interface
Full API documentation is available at https://gibme-npm.github.io/asterisk-manager-interface/
import AMI from '@gibme/asterisk-manager-interface';
const ami = new AMI({
user: 'amiuser',
password: 'amipassword',
host: '127.0.0.1'
});
await ami.login();
// Ping the server
await ami.ping();
// List active channels
const channels = await ami.channels();
// Detect available channel drivers
const hasPJSIP = await ami.has_chan_pjsip();
const hasSIP = await ami.has_chan_sip();
const hasIAX2 = await ami.has_chan_iax2();
// Query PJSIP endpoints
if (hasPJSIP) {
const endpoints = await ami.pjsip_endpoints();
const contacts = await ami.pjsip_contacts();
}
// Send any arbitrary AMI action
const response = await ami.send({ Action: 'Status' });
await ami.close();
| Option | Type | Default | Description |
|---|---|---|---|
user |
string |
— | AMI username (required) |
password |
string |
— | AMI password (required) |
host |
string |
'127.0.0.1' |
AMI server hostname or IP |
port |
number |
5038 |
AMI server port |
autoReconnect |
boolean |
true |
Automatically reconnect on disconnect |
keepAlive |
boolean |
true |
Send periodic ping commands |
keepAliveInterval |
number |
30000 |
Keep-alive ping interval (ms) |
connectionTimeout |
number |
5000 |
Socket connection timeout (ms) |
readInterval |
number |
500 |
Payload read interval (ms) |
login(user?, password?) — Authenticate with the AMI serverclose() — Close the connection and clean upping() — Send a keep-alive pingauthenticated — Whether the client is currently authenticatedhas_chan_sip() — Check if chan_sip is loadedhas_chan_pjsip() — Check if chan_pjsip is loadedhas_chan_iax2() — Check if chan_iax2 is loadedmoduleCheck(module) — Check if any Asterisk module is loadedsip_peers() — List SIP peerspjsip_endpoints() — List PJSIP endpointspjsip_contacts() — List PJSIP contactsiax2_peers() — List IAX2 peerschannels() — List active channelsdb_get(family, key) — Retrieve a database entrydb_get_tree(family?, key?) — Retrieve a database treedb_put(family, key, value) — Store a database entrydb_del(family, key) — Delete a database entrydb_del_tree(family, key) — Delete a database treesend<ResponseType, RequestType>(payload) — Send any AMI action and await a typed response. Automatically handles authentication if not already logged in.The client extends EventEmitter and emits:
| Event | Payload | Description |
|---|---|---|
connect |
— | Socket connected to the server |
close |
hadError?: boolean |
Socket closed |
error |
Error |
Connection error |
response |
T |
AMI response packet received |
MIT