Api Examples ^hot^ — Mikrotik
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)
Thankfully, libraries abstract the raw pain away.
Create or remove hotspot users automatically based on payment systems. mikrotik api examples
Use specific API users with limited permissions rather than the full 'admin' account. 4. Advanced: REST API vs. Binary API
# Get hotspot users resource users = api.get_resource('/ip/hotspot/user') # Find user and update user = users.get(name='customer1') if user: users.set(id=user[0]['id'], profile='premium-profile') print("User profile updated.") Use code with caution. E. Example 4: Monitoring Traffic (Interface Print) Retrieve real-time traffic statistics. Use specific API users with limited permissions rather
If your ISP changes your IP, use the API to update a DNS record.
RouterOS v7 introduced a native REST API running over HTTPS, allowing standard HTTPS tools ( curl , Postman, Python requests ) to interact with the device without specialized language libraries. 1. Fetching Interfaces via cURL curl -k -u admin:YourSecurePassword https://192.168.88 Use code with caution. 2. Creating a New IP Address via Python REST Binary API # Get hotspot users resource users = api
# Authenticate and retrieve device information auth = (username, password) response = requests.get(f'api_url/system/info', auth=auth)
All parameters must be prefixed with an equal sign ( =name=value ).
