MikroTik Router API Stream Library
Example 1
Example 1
import { createClient } from '@hertzg/routeros-api'; // Connect to router (example uses Deno, but works with any streams) const conn = await Deno.connect({ hostname: '192.168.88.1', port: 8728 }); // Create client const client = createClient({ readable: conn.readable, writable: conn.writable }); // Authenticate first const login = await client.send({ command: '/login', attributes: { name: 'admin', password: '' } }); const loginTrap = login.find(r => r.type === 'trap'); if(loginTrap) { throw new Error(loginTrap.message || 'Login failed'); } // check if login was successful // Execute commands (they can be sent concurrently as well) const interfaces = await client.send({ command: '/interface/print' }); const addresses = await client.send({ command: '/ip/address/print' }); // Close session await client.quit(); // Close connection await conn.close();
Creates a MikroTik API client with automatic request multiplexing
MikroTik API client interface
-
quit: () => Promise<void>
Close the connection gracefully
-
send: (command: Command) => Promise<Reply[]>
Execute a command and await all replies
Client connection options
-
readable: ReadableStream<Uint8Array>
Input stream for receiving binary data from the router
-
writable: WritableStream<Uint8Array>
Output stream for sending binary data to the router
Decodes a length value from MikroTik API format
Encodes a length value into MikroTik API format
Decoded length value with metadata
-
bytesRead: number
Number of bytes read from the buffer
-
length: number
The decoded length value
Encoded length value as a Uint8Array
Decodes a word from MikroTik API format
Encodes a word (string or bytes) into MikroTik API format
Decoded word value with metadata
-
bytesRead: number
Total bytes read (length prefix + content)
-
word: string
The decoded UTF-8 string
Encoded word as a Uint8Array
Decodes a sentence from MikroTik API format
Encodes a sentence (array of strings) into MikroTik API format
Decoded sentence value with metadata
-
bytesRead: number
Total bytes read (all words + terminator)
-
words: string[]
Array of decoded word strings
Encoded sentence as a Uint8Array
Builds a command sentence from structured data
Builds a command sentence from a Command object
MikroTik API command structure
-
attributes: Record<string, string | number | boolean>
Optional attributes (e.g., {".id": "ether1", "disabled": "yes"})
-
command: string
The command path (e.g., "/interface/print", "/login")
-
queries: Record<string, string | number | boolean>
Optional query parameters for filtering results
Checks if a reply is a data reply
Checks if a reply is a done reply
Checks if a reply is a fatal reply
Checks if a reply is a trap (error) reply
Parses a reply sentence into a structured Reply object
Data reply (!re)
-
attributes: Record<string, string>
Attributes containing the reply data
-
type: "re"
Reply type discriminator
Done reply (!done)
-
attributes: Record<string, string>
Optional attributes returned with the done reply
-
type: "done"
Reply type discriminator
Fatal reply (!fatal)
-
message: string
Human-readable error message
-
type: "fatal"
Reply type discriminator
Union of all possible reply types
Trap reply (!trap)
-
attributes: Record<string, string>
Optional additional attributes
-
category: number
Optional numeric error category
-
message: string
Human-readable error message
-
type: "trap"
Reply type discriminator
Creates a TransformStream that encodes Command objects into MikroTik API binary format
Creates a TransformStream that decodes MikroTik API binary format into Reply objects
Extracts the tag value from a sentence (array of words)
Checks if a sentence has a tag
Checks if a sentence has a specific tag value
Removes the tag from a sentence
Adds or replaces a tag in a sentence
Creates a MikroTik API client with automatic request multiplexing
MikroTik API client interface
-
quit: () => Promise<void>
Close the connection gracefully
-
send: (command: Command) => Promise<Reply[]>
Execute a command and await all replies
Client connection options
-
readable: ReadableStream<Uint8Array>
Input stream for receiving binary data from the router
-
writable: WritableStream<Uint8Array>
Output stream for sending binary data to the router