@gibme/asterisk-manager-interface
v22.0.0
Published
Asterisk Manager Interface
Readme
Asterisk Manager Interface
A TypeScript client library for the Asterisk Manager Interface (AMI) with automatic reconnection, keep-alive support, and typed responses for common AMI actions.
Requirements
- Node.js >= 22
Installation
yarn add @gibme/asterisk-manager-interface
# or
npm install @gibme/asterisk-manager-interfaceDocumentation
Full API documentation is available at https://gibme-npm.github.io/asterisk-manager-interface/
Quick Start
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();Constructor Options
| 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) |
API
Connection
login(user?, password?)— Authenticate with the AMI serverclose()— Close the connection and clean upping()— Send a keep-alive pingauthenticated— Whether the client is currently authenticated
Channel Driver Detection
has_chan_sip()— Check ifchan_sipis loadedhas_chan_pjsip()— Check ifchan_pjsipis loadedhas_chan_iax2()— Check ifchan_iax2is loadedmoduleCheck(module)— Check if any Asterisk module is loaded
Peer & Endpoint Discovery
sip_peers()— List SIP peerspjsip_endpoints()— List PJSIP endpointspjsip_contacts()— List PJSIP contactsiax2_peers()— List IAX2 peerschannels()— List active channels
Asterisk Database
db_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 tree
Generic Commands
send<ResponseType, RequestType>(payload)— Send any AMI action and await a typed response. Automatically handles authentication if not already logged in.
Events
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 |
License
MIT
