Summary

Application Programmable Interface (API) allows users to create custom software solutions to communicate with RouterOS to gather information, adjust configuration and manage router. API closely follows syntax from command line interface (CLI). It can be used to create translated or custom configuration tools to aid ease of use running and managing routers with RouterOS.

To use API RouterOS version 3.x or newer is required.

By default API uses port #8728 and service is disabled. More on service management see in corresponding manual section. Corresponding service name is api

Protocol

Communication with router is done by sending sentences to the router and receiving one or more sentences in return. Sentence is sequence of words terminated by zero length word. Word is part of sentence encoded in certain way - encoded length and data. Communication happen by sending sentences to the router and receiving replies to sent sentences. Each sentence sent to router using API should contain command as a first word followed by words in no particular order, end of sentence is marked by zero length word. When router receives full sentence (command word, no or more attribute words and zero length word) it is evaluated and executed, then reply is formed and returned.

API words

Words are part of sentence. Each word has to be encoded in certain way - length of the word followed by word content. Length of the word should be given as count of bytes that are going to be sent.

Length of the word is encoded as follows:

Value of length

# of bytes

Encoding

0 <= len <= 0x7F

1

len, lowest byte

0x80 <= len <= 0x3FFF

2

len | 0x8000, two lower bytes

0x4000 <= len <= 0x1FFFFF

3

len | 0xC00000, three lower bytes

0x200000 <= len <= 0xFFFFFFF

4

len | 0xE0000000

len >= 0x10000000

5

0xF0 and len as four bytes

Each word is encoded as length, followed by that many bytes of content;

Words are grouped into sentences. End of sentence is terminated by zero length word;

Scheme allows encoding of length up to 0x7FFFFFFFFF, only four byte length is supported;

Bytes of len are sent most significant first (network order);

If first byte of word is >= 0xF8, then it is a reserved control byte. After receiving unknown control byte API client cannot proceed, because it cannot know how to interpret following bytes;

Command word

First word in sentence has to be command followed by attribute words and zero length word or terminating word. Name of command word should begin with '/'. Names of commands closely follow CLI, with spaces replaced with '/'. There are commands that are specific to API;

Command word structure in strict order:

encoded length

content prefix /

CLI converted command

API specific commands:

login
cancel

Command word concent examples:

/login

/user/active/listen

/interface/vlan/remove

/system/reboot

Attribute word

Each command wordhave its own list of attribute words depending on content.

Atribute word structure consists of 5 parts in this order:

encoded length

content prefix equals sigh - =

attribute name

separating equals sign - =

value of attribute if there is one. It is possible that attribute does not have a value

Note: Value can hold multiple equal signs in the value of attribute word since the way word is encoded

Note: Value can be empty

Examples without encoded length prefix:

=address=10.0.0.1

=name=iu=c3Eeg

=disable-running-check=yes

Warning: Order of attribute words and API parameters is not important and should not be relied on

API attribute word

API attribute word structure is in strict order:

encoded length

content prefix with dot .

attribute name

name postfixed with equals =sign

attribute value

Currently the only such API attribute is tag.

Note: If sentence contain API attribute word tag then each returned sentence in reply from router to that tagged sentence will be tagged with same tag.

Query word

Senteces can have additional query paramteres that restrict their scope. They are explained in detail in separate section.

Example of sentence using query word attributes:

/interface/print
?type=ether
?type=vlan
?#|!

Query words begin with '?'.

Currently only print command handles query words.

Warning: Order of query words is significant

Reply word

It is sent only by the router. It is only sent in response to full sentence send by the client.

First word of reply begins with '!';

Each sentence sent generates at least one reply (if connection does not get terminated);

Last reply for every sentence is reply that has first word !done;

Errors and exceptional conditions begin with !trap;

Data replies begin with !re

If API connection is closed, RouterOS sends !fatal with reason as reply and then closes the connection;

API sentences

API sentence is main object of communication using API.

Empty sentences are ignored.

Sentence is processed after receiving zero length word.

There is a limit on number and size of sentences client can send before it has logged in.

Order of attribute words should not be relied on. As order and count is changeable by .proplist attribute.

Sentence structure is as follows:

First word should contain command word;

Should contain zero length word to terminate the sentence;

Can contain none or several attribute words. There is no particular order at what attribute words has to be sent in the sentence, order is not important for attribute words;

Can contain none or several query words. Order of query words in the sentence is important.

Note:Zero length word terminates the sentence. If it is not provided router will not start to evaluate sent words and will consider all the input as part of the same sentence.

Initial login

Login method pre-v6.43:

/login

!done

=ret=ebddd18303a54111e2dea05a92ab46b4

/login

=name=admin

=response=001ea726ed53ae38520c8334f82d44c9f2

!done

Note: that each command and response ends with an empty word.

First, clients sends /login command.

Reply contains =ret=challenge argument.

Client sends second /login command, with =name=username and =response=response.

In case of error, reply contains =ret=error message.

In case of successful login client can start to issue commands.

Login method post-v6.43:

/login

=name=admin

=password=

!done

Now client sends username and password in first message.

Password is sent in plain text.

in case of error, reply contains =message=error message.

In case of successful login client can start to issue commands.

Tags

It is possible to run several commands simultaneously, without waiting for previous one to complete. If API client is doing this and needs to differentiate command responses, it can use 'tag' API parameter in command sentences.

If you include 'tag' parameter with non-empty value in command sentence, then 'tag' parameter with exactly the same value will be included in all responses generated by this command.

If you do not include 'tag' parameter or it's value is empty, then all responses for this command will not have 'tag' parameter.

Command description

/cancel

optional argument: =tag=tag of command to cancel, without it cancels all running commands

does not cancel itself

all canceled commands are interruped and in the usual case generate '!trap' and '!done' responses

please note that /cancel is separate command and can have it's own unique '.tag' parameter, that is not related to '=tag' argument of this command

listen

listen command is avaliable where console print command is available, but it does not have expected effect everywhere (i.e. may not work)

when item is deleted or dissapears in any other way, the '!re' sentence includes value '=.dead=yes'

This command does not terminate. To terminate it use /cancel command.

getall

getall command is available where console print command is available. Since version 3.21 getall is an alias for print.

replies contain =.id=Item internal number property.

print

API print command differs from the console counterpart in the following ways:

where argument is not supported. Items can be filtered using query words (see below).

.proplist argument is a comma separated list of property names that should be included for the returned items.

returned items may have additional properties.

order of returned properties is not defined.

if list contains duplicate entries, handling of such entries is not defined.

if propery is present in .proplist, but absent from the item, then that item does not have this property value (?name will evaluate to false for that item).

if .proplist is absent, all properties are included as requested by print command, even those that have slow access time (such as file contents and perfomance counters). Thus use of .proplist is encouraged. Omission of .proplist may have high perfomance penalty if =detail= argument is set.

Queries

print command accepts query words that limit set of returned sentences. This feature is available since RouterOS 3.21.

Query words begin with '?'.

Order of query words is significant. Query is evaluated starting from the first word.

Query is evaluated for each item in the list. If query succeeds, item is processed, if query fails, item is ignored.

Query is evaluated using a stack of boolean values. Initially stack contains infinite amount of 'true' values. At the end of evaluation, if stack contains at least one 'false' value, query fails.

Query words operate according to the following rules:

Query

Desciption

?name

pushes 'true' if item has value of property name, 'false' if it does not.

?-name

pushes 'true' if item does not have value of property name, 'false' otherwise.

?name=x?=name=x

pushes 'true' if property name has value equal to x, 'false' otherwise.

?<name=x

pushes 'true' if property name has value less than x, 'false' otherwise.

?>name=x

pushes 'true' if property name has value greater than x, 'false' otherwise.

?#operations

applies operations to the values in the stack.

operation string is evaluated left to right.

sequence of decimal digits followed by any other character or end of word is interpreted as a stack index. top value has index 0.

index that is followed by a character pushes copy of value at that index.

index that is followed by the end of word replaces all values with the value at that index.

! character replaces top value with the opposite.

& pops two values and pushes result of logical 'and' operation.

| pops two values and pushes result of logical 'or' operation.

. after an index does nothing.

. after another character pushes copy of top value.

Warning: Regular expressions are not supported in API, so do not try to send query with ~ symbol

OID

print command can return OID values for properties that are available in SNMP. This feature appeared in 3.23 version.

In console, OID values can be seen by running 'print oid' command. In API, these properties have name that ends with ".oid", and can be retrieved by adding their name to the value of '.proplist'. An example:

/system/resource/print

=.proplist=uptime,cpu-load,uptime.oid,cpu-load.oid

!re

=uptime=01:22:53

=cpu-load=0

=uptime.oid=.1.3.6.1.2.1.1.3.0

=cpu-load.oid=.1.3.6.1.2.1.25.3.3.1.2.1

!done

!trap

When for some reason API sentence fails trap is sent in return accompanied with message attribute and on some occasions category argument.

message

When API sentence fails some generic message or message from used internal process is return to give more details about failure