Mikrotik Api Examples 🆕 Pro

If you are running RouterOS v7, you can interact with the router using standard HTTP methods.

import time

import routeros_api # Configuration HOST = '192.168.88.1' USERNAME = 'admin' PASSWORD = 'your_password' # Establish Connection (use plaintext_login=True for ROS 6.43+) connection = routeros_api.RouterOsApiPool(HOST, username=USERNAME, password=PASSWORD, plaintext_login=True) api = connection.get_api() # Get interface data interface_resource = api.get_resource('/interface') interfaces = interface_resource.get() # Print results for iface in interfaces: print(f"Name: iface['name'], Status: iface['running']") connection.disconnect() Use code with caution. Example B: Creating a New DHCP Lease (Static)

$API->write('/ip/address/print', false); $API->write('?interface=ether1'); Prototyping API Calls

PHP is widely used for web-based network management interfaces. mikrotik api examples

# 3. Add simple queue with bandwidth limit api('/queue/simple/add', 'name': client_name, 'target': f"client_ip/32", 'max-limit': '10M/2M', 'comment': client_name ) print("Queue limit set to 10M down / 2M up")

The Mikrotik API is a set of programming interfaces that allow developers to interact with Mikrotik devices, such as routers, switches, and firewalls. The API provides a way to access and manipulate the device's configuration, monitor its performance, and automate tasks. The Mikrotik API is available on most Mikrotik devices, including the popular RouterOS.

Before any code runs, ensure the API service is active on your MikroTik router.

The legacy binary API is more efficient for real-time monitoring and high-frequency data exchange because it uses a custom socket-based protocol. If you are running RouterOS v7, you can

import requests from requests.auth import HTTPBasicAuth # Example: Get all interfaces via REST API url = "http://192.168.88" response = requests.get(url, auth=HTTPBasicAuth('admin', 'password'), verify=False) if response.status_code == 200: print(response.json()) Use code with caution.

fmt.Printf("CPU Load: %s%%\n", resource["cpu-load"]) fmt.Printf("Uptime: %s\n", resource["uptime"])

connection.path('ip', 'firewall', 'filter').add( chain='forward', src_address='192.168.100.0/24', action='drop' )

registrations = api.path('interface', 'wireless', 'registration-table') for client in registrations: print(f"MAC: client['mac-address'], Signal: client['signal-strength'] dBm, TX Rate: client['tx-rate']") The Mikrotik API is available on most Mikrotik

Always use api-ssl or the REST API over HTTPS ( 443 ).

If you are using RouterOS v7, Mikrotik has introduced a REST API which is generally preferred over the binary API for modern applications.

This script demonstrates how to generate a temporary hotspot client account programmatically.

Connect to a router, add a simple queue, and print system identity.

Using the Mikrotik API, you can configure a VLAN as follows: