api(cmd='/ip/firewall/filter/enable', numbers='*1')
: API queries do not support regex with ~ symbol. Solution : Use explicit query words ( ?name=x ) or multiple queries.
To get all IP addresses, you can send a GET request to the /rest/ip/address endpoint.
librouteros (recommended) or raw sockets.
For Laravel applications, the RouterOS API client can be integrated as a facade: mikrotik api examples
This script demonstrates how to generate a temporary hotspot client account programmatically.
The REST API is generally easier to implement as it uses standard HTTP methods and JSON. A. Fetching Information (GET)
: API commands mirror CLI commands but use slashes instead of spaces (e.g., /ip/address/print ). Enabling the API on RouterOS
ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE ctx.set_ciphers('ADH') librouteros (recommended) or raw sockets
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
/ip/hotspot/user/print /ip/hotspot/user/add =name=customer01 =password=welcome =profile=default /ip/hotspot/user/remove =.id=*2
curl -k -u api_user:YourSecurePassword123! \ -X PUT https://192.168.88 \ -H "Content-Type: application/json" \ -d '"list": "Spam_Blocker", "address": "203.0.113.50", "comment": "API Blocked"' Use code with caution. 7. Best Practices for Production Environments
class MikroTikAutomation: def (self, router_ip, username, password): self.base_url = f"https://router_ip/rest" self.auth = HTTPBasicAuth(username, password) self.verify_ssl = False logging.basicConfig(level=logging.INFO) REST API Examples (RouterOS v7+)
Python developers have excellent support through the library, available on PyPI.
import RouterOSClient from '@sourceregistry/mikrotik-client';
import librouteros
#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> let device = MikrotikDevice::new("192.168.88.1:8728", "admin", "password").await?;
// Execute commands concurrently const interfaces = await client.send( command: '/interface/print' ); const addresses = await client.send( command: '/ip/address/print' );
MikroTik offers two primary ways to interact with its devices programmatically: the traditional (fast, low-level) and the newer REST API introduced in RouterOS v7 (web-friendly, JSON-based). 1. REST API Examples (RouterOS v7+)