mod.ts

MikroTik Router API Stream Library

Examples

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();

Functions

f
createClient(options: ClientOptions): Client

Creates a MikroTik API client with automatic request multiplexing

Type Aliases

T
ClientOptions = { readable: ReadableStream<Uint8Array>; writable: WritableStream<Uint8Array>; }

Client connection options

encoding/length.ts

Functions

f
encodeLength(length: number): EncodedLength

Encodes a length value into MikroTik API format

Type Aliases

T
DecodedLength = { length: number; bytesRead: number; }

Decoded length value with metadata

T
EncodedLength = Uint8Array

Encoded length value as a Uint8Array

encoding/word.ts

Functions

f
encodeWord(content: string | Uint8Array): EncodedWord

Encodes a word (string or bytes) into MikroTik API format

Type Aliases

T
DecodedWord = { word: string; bytesRead: number; }

Decoded word value with metadata

T
EncodedWord = Uint8Array

Encoded word as a Uint8Array

encoding/sentence.ts

Functions

f
encodeSentence(words: string[]): EncodedSentence

Encodes a sentence (array of strings) into MikroTik API format

Type Aliases

T
DecodedSentence = { words: string[]; bytesRead: number; }

Decoded sentence value with metadata

T
EncodedSentence = Uint8Array

Encoded sentence as a Uint8Array

protocol/reply.ts

Functions

f
isData(reply: Reply): reply is DataReply

Checks if a reply is a data reply

f
isDone(reply: Reply): reply is DoneReply

Checks if a reply is a done reply

f
isFatal(reply: Reply): reply is FatalReply

Checks if a reply is a fatal reply

f
isTrap(reply: Reply): reply is TrapReply

Checks if a reply is a trap (error) reply

f
parseReply(words: string[]): Reply

Parses a reply sentence into a structured Reply object

Type Aliases

T
DataReply = { type: "re"; attributes: Record<string, string>; }

Data reply (!re)

T
DoneReply = { type: "done"; attributes?: Record<string, string>; }

Done reply (!done)

T
FatalReply = { type: "fatal"; message: string; }

Fatal reply (!fatal)

T
TrapReply = { type: "trap"; message: string; category?: number; attributes?: Record<string, string>; }

Trap reply (!trap)

streams/encode.ts

Functions

f
createApiEncodeStream(): TransformStream<Command, Uint8Array>

Creates a TransformStream that encodes Command objects into MikroTik API binary format

streams/decode.ts

Functions

f
createApiDecodeStream(): TransformStream<Uint8Array, Reply>

Creates a TransformStream that decodes MikroTik API binary format into Reply objects

utils/tag.ts

Functions

f
getTag(words: string[]): string | undefined

Extracts the tag value from a sentence (array of words)

f
hasTag(words: string[]): boolean

Checks if a sentence has a tag

f
hasTagValue(
words: string[],
tag: string
): boolean

Checks if a sentence has a specific tag value

f
withoutTag(words: string[]): string[]

Removes the tag from a sentence

f
withTag(
words: string[],
tag: string
): string[]

Adds or replaces a tag in a sentence

client.ts

Functions

f
createClient(options: ClientOptions): Client

Creates a MikroTik API client with automatic request multiplexing

Type Aliases

T
ClientOptions = { readable: ReadableStream<Uint8Array>; writable: WritableStream<Uint8Array>; }

Client connection options