Mikrotik Api Examples | EXCLUSIVE |
$client = new Client($config);
debug = false; // Configuration $router_ip = '192.168.88.1'; $username = 'admin'; $password = 'YourSecurePassword'; if ($API->connect($router_ip, $username, $password)) // Step 1: Check if the secret user exists $API->write('/ppp/secret/print', false); $API->write('?name=client_0412'); $READ = $API->read(); if (count($READ) > 0) // User exists, let's enable them and change their profile to 'Active_Plan' $userId = $READ[0]['.id']; $API->write('/ppp/secret/set', false); $API->write('=.id=' . $userId, false); $API->write('=profile=Active_Plan', false); $API->write('=disabled=no'); $API->read(); echo "Client account 'client_0412' successfully activated."; else // User does not exist, let's create a new one $API->write('/ppp/secret/add', false); $API->write('=name=client_0412', false); $API->write('=password=ClientSecretPass', false); $API->write('=service=pppoe', false); $API->write('=profile=Active_Plan'); $API->read(); echo "Client account 'client_0412' not found. Created fresh account."; $API->disconnect(); else echo "Connection to MikroTik router failed."; ?> Use code with caution. 5. Node.js MikroTik API Examples
Use code with caution. Copied to clipboard Why use the API instead of SSH/CLI? While SSH scripting is popular, the API provides structured data
If you want to customize these code snippets for your network infrastructure, let me know which programming language you intend to deploy, what RouterOS version you are targeting, and the specific configuration task you want to automate. Share public link mikrotik api examples
catch (err) err.message);
- name: Configure IP address on MikroTik community.routeros.api: commands: - /ip/address/add =address=10.0.0.1/24 =interface=ether1
import routeros_api def add_block_rule(host, username, password, target_ip): connection = routeros_api.RouterOsApiPool(host, username=username, password=password, plaintext_login=True) api = connection.get_api() firewall_filter = api.get_resource('/ip/firewall/filter') # Add new rule payload firewall_filter.add( chain='input', src_address=target_ip, action='drop', comment='Blocked via Python API Automation' ) print(f"Successfully blocked IP: target_ip") connection.disconnect() # Usage add_block_rule('192.168.88.1', 'admin', 'YourSecurePassword', '198.51.100.45') Use code with caution. PHP Implementation Examples $client = new Client($config); debug = false; //
For production environments, use the API-SSL service to encrypt communications:
Before running any API examples, you must enable the API service on your router.
This ensures only authorized networks can reach the API. While SSH scripting is popular, the API provides
Example: Activating a PPPoE User or Hotspot User Dynamically
API queries allow you to filter data directly on the router. Query words begin with ? and the order is significant:
When an item is deleted, the reply includes =.dead=yes .