Mikrotik Api Examples Jun 2026

Mikrotik Api Examples Jun 2026

Queries use the format ? = . You can combine filters using built-in logical operators like -and , -or , and -not . Filtering Example (Python)

return $this->client->query($query)->read();

import routeros_api def add_dhcp_lease(host, username, password, mac, ip, comment): connection = routeros_api.RouterOsApiPool(host, username=username, password=password, plaintext_login=True) api = connection.get_api() dhcp_lease = api.get_resource('/ip/dhcp-server/lease') try: dhcp_lease.add( address=ip, mac_address=mac, comment=comment ) print(f"Successfully added static lease: ip to mac") except Exception as e: print(f"Error adding lease: e") finally: connection.disconnect() # Usage add_dhcp_lease('192.168.88.1', 'admin', 'YourSecurePassword', '00:11:22:33:44:55', '192.168.88.250', 'Automation Test') Use code with caution. ⚡ PHP API Examples (Using RouterOS PHP API) mikrotik api examples

The MikroTik API is the definition of a "mechanical" interface. It is not a modern, RESTful, JSON-formatted playground. It is a raw, direct pipe into the RouterOS kernel. for utility, 5/10 for developer experience.

import routeros_api def get_system_resources(): connection = routeros_api.RouterOsApiPool( '192.168.88.1', username='admin', password='YourSecurePassword', plaintext_login=True ) api = connection.get_api() # Navigate to /system/resource resource_path = api.get_resource('/system/resource') resources = resource_path.get() for stat in resources: print(f"Uptime: stat.get('uptime')") print(f"CPU Load: stat.get('cpu-load')%") print(f"Free Memory: int(stat.get('free-memory')) / 1024 / 1024:.2f MB") print(f"RouterOS Version: stat.get('version')") connection.disconnect() if __name__ == "__main__": get_system_resources() Use code with caution. 2. Adding a Firewall Rule Queries use the format

. By querying the for interface statistics and CPU load, you can display live traffic graphs on a small TFT screen. Setup : The Go to product viewer dialog for this item.

Because the API grants deep administrative control over your network fabric, securing it is paramount. It is a raw, direct pipe into the RouterOS kernel

import routeros_api def get_router_ips(host, username, password): # Establish connection connection = routeros_api.RouterOsApiPool( host, username=username, password=password, plaintext_login=True ) api = connection.get_api() # Navigate to the IP address resource path ip_resource = api.get_resource('/ip/address') addresses = ip_resource.get() # Parse and print results for addr in addresses: print(f"Interface: addr['interface'] | IP: addr['address'] | Network: addr['network']") connection.disconnect() # Usage get_router_ips('192.168.88.1', 'admin', 'YourSecurePassword') Use code with caution. Example 2: Adding a Firewall Rule

interfaces = router.command('/interface/print') interfaces.each do |iface| if iface['running'] == 'true' puts "#iface['name'] is running" end end

You can interact with MikroTik REST API using Python's built-in requests library: