Mikrotik Api Examples Jun 2026
// Enable interface by name public function enableInterface($interface_name) // First find interface ID $find_query = (new Query('/interface/print')) ->where('name', $interface_name); $interfaces = $this->client->query($find_query)->read();
MikroTik offers two distinct API interfaces:
To help me tailor more advanced scripts, please let me know: What or framework do you prefer? What RouterOS version are you targeting (v6 or v7)?
If you are using , you may also consider using the REST API ( /ip/service/rest ) for easier JSON interaction, as shown in this Network Automation using Python on MikroTik video. Python3 Example - RouterOS - MikroTik Documentation
The library provides backward compatibility: the comm() method automatically translates legacy CLI syntax into REST requests. mikrotik api examples
// Create a hotspot user (PUT / POST mapping) $api->put('/rest/ip/hotspot/user', [ 'name' => 'guest123', 'password' => 'welcome', 'profile' => 'default' ]);
# Get the ID of a specific interface interfaces = api(cmd='/interface/print', stats=True) for iface in interfaces: if iface['name'] == 'ether1': # Disable the interface api(cmd='/interface/set', **'.id': iface['.id'], 'disabled': True) break
func main() client, _ := routeros.Dial("192.168.88.1:8728", "admin", "password") defer client.Close()
Only use the API over a VPN tunnel or restrict it strictly to input firewall chains from trusted subnets. Python3 Example - RouterOS - MikroTik Documentation The
// Add WireGuard peer const peer = await client.wireguard.peer.add( interface: 'wg0', publicKey: '...', allowedAddress: '10.0.0.2/32', comment: 'VPN Client' );
The API mirrors the CLI path structure ( /interface/print ).
This script connects to a MikroTik router, retrieves hardware utilization details (CPU, memory, uptime), prints them cleanly, and safely closes the connection. Use code with caution. Node.js: Creating a DHCP Server Lease
: .id values change across reboots. Solution : Always read .id from the API before attempting updates. Never hardcode .id values. This script connects to a MikroTik router, retrieves
The community.routeros.api Ansible module allows you to manage RouterOS devices declaratively:
: By default, the API uses port 8728 for unencrypted traffic and 8729 for API over SSL (highly recommended for production).
The MikroTik API is a powerful gateway to RouterOS automation, offering both a mature native binary protocol and a modern REST interface. With comprehensive support across multiple programming languages—Python, PHP, Node.js, Go, Rust, and more—you can build sophisticated network automation solutions tailored to your infrastructure.