
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
asterisk-manager-ts
Advanced tools
A modern TypeScript library for interacting with the Asterisk Manager Interface (AMI). Fully typed, event-driven, with auto-reconnection support.
A modern, fully-typed TypeScript library for interacting with the Asterisk Manager Interface (AMI). Built from the ground up with TypeScript, this library provides a robust, event-driven interface for managing Asterisk PBX systems.
npm install asterisk-manager-ts
Note: This package includes TypeScript definitions built-in. No need to install @types/asterisk-manager.
import Manager from "asterisk-manager-ts";
const ami = new Manager(5038, "localhost", "admin", "secret", true);
ami.on("connect", () => {
console.log("Connected to Asterisk Manager Interface");
});
ami.on("managerevent", (event) => {
console.log("AMI Event:", event);
});
// Execute an action
ami.action(
{
action: "Status",
},
(error, response) => {
if (error) {
console.error("Action failed:", error);
} else {
console.log("Status response:", response);
}
}
);
const Manager = require("asterisk-manager-ts");
const ami = new Manager(5038, "localhost", "admin", "secret", true);
ami.on("connect", () => {
console.log("Connected to Asterisk Manager Interface");
});
new Manager(port?: number, host?: string, username?: string, password?: string, events?: boolean)
port - AMI port (default: 5038)host - Asterisk server hostname or IPusername - AMI usernamepassword - AMI passwordevents - Whether to receive events (default: false)connect(port: number, host: string, callback?: ConnectionCallback): voidEstablishes a connection to the AMI.
ami.connect(5038, "192.168.1.100", (error) => {
if (error) {
console.error("Connection failed:", error);
} else {
console.log("Connected successfully");
}
});
login(callback?: ManagerCallback): voidAuthenticates with the AMI using provided credentials.
ami.login((error, response) => {
if (error) {
console.error("Login failed:", error);
} else {
console.log("Logged in successfully");
}
});
action(action: ManagerAction, callback?: ManagerCallback): stringExecutes an AMI action and returns the action ID.
const actionId = ami.action(
{
action: "Originate",
channel: "SIP/1001",
context: "default",
exten: "1002",
priority: 1,
},
(error, response) => {
if (error) {
console.error("Originate failed:", error);
} else {
console.log("Call originated:", response);
}
}
);
keepConnected(): voidEnables automatic reconnection with exponential backoff.
ami.keepConnected();
disconnect(callback?: () => void): voidDisconnects from the AMI.
ami.disconnect(() => {
console.log("Disconnected from AMI");
});
isConnected(): booleanReturns the current connection status.
if (ami.isConnected()) {
console.log("AMI is connected");
}
The library extends Node.js EventEmitter and emits various events:
ami.on("connect", () => {
console.log("Connected to AMI");
});
ami.on("close", () => {
console.log("Connection closed");
});
ami.on("error", (error) => {
console.error("Connection error:", error);
});
// All AMI events
ami.on("managerevent", (event) => {
console.log("AMI Event:", event.event, event);
});
// Specific events (case-insensitive)
ami.on("newchannel", (event) => {
console.log("New channel:", event.channel);
});
ami.on("hangup", (event) => {
console.log("Channel hung up:", event.channel);
});
// User events
ami.on("userevent-myevent", (event) => {
console.log("Custom user event:", event);
});
// All action responses
ami.on("response", (response) => {
console.log("Action response:", response);
});
// Specific action response by ID
ami.once(actionId, (error, response) => {
if (error) {
console.error("Action failed:", error);
} else {
console.log("Action succeeded:", response);
}
});
The library provides comprehensive TypeScript definitions:
interface ManagerOptions {
port: number;
host: string;
username: string;
password: string;
events: boolean;
}
interface ManagerAction {
action: string;
actionid?: string;
[key: string]: unknown;
}
interface ManagerEvent {
event?: string;
response?: string;
actionid?: string;
content?: string;
[key: string]: unknown;
}
type ManagerCallback = (
error?: ManagerEvent | Error | null,
response?: ManagerEvent
) => void;
interface LoginAction extends ManagerAction {
action: "login";
username: string;
secret: string;
event: "on" | "off";
}
// Define your own action types
interface OriginateAction extends ManagerAction {
action: "Originate";
channel: string;
context: string;
exten: string;
priority: number;
timeout?: number;
callerid?: string;
}
ami.action(
{
action: "Originate",
channel: "SIP/1001",
context: "default",
exten: "1002",
priority: 1,
timeout: 30000,
callerid: "Test Call <1001>",
},
(error, response) => {
if (error) {
console.error("Failed to originate call:", error);
} else {
console.log("Call originated successfully:", response);
}
}
);
ami.on("newchannel", (event) => {
console.log(`New channel created: ${event.channel}`);
});
ami.on("hangup", (event) => {
console.log(`Channel ${event.channel} hung up. Cause: ${event.cause}`);
});
ami.on("newstate", (event) => {
console.log(
`Channel ${event.channel} changed state to ${event.channelstatedesc}`
);
});
ami.action({ action: "Status" }, (error, response) => {
if (error) {
console.error("Failed to get status:", error);
} else {
console.log("System status:", response);
}
});
ami.action({ action: "CoreShowChannels" }, (error, response) => {
if (response?.response === "follows") {
console.log("Active channels:");
console.log(response.content);
}
});
The library provides comprehensive error handling:
ami.on("error", (error) => {
console.error("AMI Error:", error.message);
// Handle different error types
if (error.message.includes("ECONNREFUSED")) {
console.log("Asterisk server is not running or AMI is disabled");
}
});
// Action-specific error handling
ami.action({ action: "InvalidAction" }, (error, response) => {
if (error) {
console.error("Action error:", error);
// error will contain the error response from Asterisk
}
});
Enable automatic reconnection to handle network issues:
const ami = new Manager(5038, "localhost", "admin", "secret", true);
// Enable auto-reconnection
ami.keepConnected();
ami.on("connect", () => {
console.log("Connected to AMI");
});
ami.on("close", () => {
console.log("Connection lost, will attempt to reconnect...");
});
# Clone the repository
git clone https://github.com/uchkunrakhimow/NodeJS-AsteriskManager.git
cd NodeJS-AsteriskManager
# Install dependencies
npm install
# Build the project
npm run build
# The compiled JavaScript and type definitions will be in the dist/ directory
# Build the project
npm run build
# Clean build artifacts
npm run clean
# Build for publishing
npm run prepublishOnly
Ensure AMI is enabled in your Asterisk configuration (/etc/asterisk/manager.conf):
[general]
enabled = yes
port = 5038
bindaddr = 0.0.0.0
[admin]
secret = your-secret-password
deny=0.0.0.0/0.0.0.0
permit=127.0.0.1/255.255.255.0
read = system,call,log,verbose,agent,user,config,dtmf,reporting,cdr,dialplan
write = system,call,agent,user,config,command,reporting,originate
Contributions are welcome! Please feel free to submit a Pull Request.
See the LICENSE file for license information.
This TypeScript library is inspired by and based on the original JavaScript implementations:
However, this version has been completely rewritten from scratch in TypeScript with modern Node.js practices, full type safety, and enhanced features.
Uchkun Rakhimov
Contributions are welcome! Please feel free to submit issues and pull requests.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Built with ❤️ and TypeScript - A modern approach to Asterisk AMI integration with comprehensive type safety and developer experience.
FAQs
A modern TypeScript library for interacting with the Asterisk Manager Interface (AMI). Fully typed, event-driven, with auto-reconnection support.
We found that asterisk-manager-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.