Mikrotik Api Examples
/ip service set www-ssl disabled=no certificate=your_ssl_cert Use code with caution. Example 1: Fetching Interfaces Using curl (GET) curl -k -u admin:your_password https://192.168.88 Use code with caution. Example 2: Creating a Firewall Filter Rule (POST)
// Incorrect: Using relative numbers from CLI // $query->equal('numbers', 0); // This will fail
require 'mtik'
Each command ends with a blank line ( \r\n\r\n ). The router responds with !done , !trap (error), or !re (data reply). You rarely manipulate this raw layer; instead, you use libraries. mikrotik api examples
The API communicates in "sentences." A standard REST API returns a JSON object. The MikroTik API returns a stream of attributes that you have to manually parse into a logical object. If you are used to GET/POST requests returning clean "status": "ok" , the MikroTik API will feel archaic. You have to manually detect the !done or !trap markers to know when a transaction is finished.
func main() // Authenticate to router auth, err := routeros.Auth("192.168.88.1", "admin", "") if err != nil log.Fatal(err)
const RouterOSClient = require('routeros-client'); const client = new RouterOSClient( host: '192.168.88.1', user: 'admin', password: 'your_password' ); async function monitorTraffic() try const api = await client.connect(); // Define target interface const menu = api.menu('/interface'); // Stream interface traffic data every second const stream = menu.exec('monitor-traffic', interface: 'ether1', once: false ); stream.on('data', (data) => Tx: $data['tx-bits-per-second'] bps`); ); catch (err) console.error('Error:', err); monitorTraffic(); Use code with caution. RouterOS v7 REST API Examples The router responds with
If you manage dozens of routers, logging into each one to update a firewall rule is inefficient. The API allows you to push configuration changes across your entire fleet simultaneously. javascript RouterOSClient = 'routeros-client' ).RouterOSClient; updateFirewall() { RouterOSClient( host: '192.168.88.1' , password: 'password' api.connect(); // Block a specific IP address conn.write( '/ip/firewall/filter/add' '=chain=forward' '=action=drop' '=src-address=10.0.0.50' '=comment=Automated block' ]); conn.close();
Change queue max-limit at 9 AM and 6 PM.
connection = routeros_api.RouterOsApiPool('192.168.88.1', 'admin', 'password') api = connection.get_api() The MikroTik API returns a stream of attributes
While generally backward compatible, subtle differences exist between RouterOS v6 and v7. Scripts written for v6 might fail on v7 due to changes in how arguments are passed or how VLANs are structured, requiring maintenance of codebases.
This library provides typed helpers for many RouterOS menus including:
API command names closely follow CLI syntax, with spaces replaced by forward slashes (e.g., /ip/address/print ). Supported operations include:
/ip service enable api-ssl