Wialon Messages API: load_interval, Flags and Paging

Reference · last checked

How the Wialon messages API works: messages/load_interval, flags and flagsMask, paging with get_messages, message parameters, and the limits that bite.

Raw messages are what the tracker actually sent, and the reports and trips you see in Wialon are built on top of them. The Wialon messages API gives you that history directly, and it behaves differently from everything else in the Remote API. You do not get a response you can page through freely. You load a set of messages into your session, then read it back in slices. This page covers messages/load_interval and its flags, how to page through an interval too large for one response, what the p object and “Wialon parameters” actually are, and the limits that make large extractions fail.

If the request format is new to you, read the Postman setup first. Every call below is svc plus params sent to wialon/ajax.html with a session id. For the current position and last message of a unit, you do not need this page: that is flag 1024 on a unit search, covered on the units page.

Know which documentation is current

The older wiki at sdk.wialon.com still ranks for these queries, but it now states that it is no longer supported and points to help.wialon.com. The two disagree in ways that matter. The old load_interval page lists six parameters; the current one lists ten, including calcSensors and filter, and documents error 1004. The current get_messages page also states precisely what flags and flagsMask do there, which turns out to be the opposite of what they do in load_interval. When the two sources differ, trust help.wialon.com.

Understand the message loader first

The messages section describes a message loader: a set of messages held in your session on the server. messages/load_interval and messages/load_last fill it, messages/get_messages reads from it, messages/delete_message removes one message from it and from the unit, and messages/unload clears it.

The loader holds one set. Loading a second set flushes the first automatically. Two practical consequences follow. A worker that shares a session with another worker overwrites that worker’s set, so parallel extraction needs one session per worker. And get_messages answers from whatever is loaded right now: if nothing is loaded it fails with error 4 rather than returning an empty array.

Load an interval of messages

https://hst-api.wialon.com/wialon/ajax.html?sid=<sid>&svc=messages/load_interval&params={"itemId":<unitId>,"timeFrom":1764547200,"timeTo":1764633599,"flags":1,"flagsMask":65281,"loadCount":100}

That asks for the first hundred data messages with a position for one unit and one day. Times are Unix timestamps in seconds. The parameters, from the reference:

ParameterMeaning
itemIdUnit or resource id, depending on the message type; for user log records, a user id
timeFrom, timeToInterval bounds. If timeFrom is greater than timeTo, nothing is returned and count is 0
flags, flagsMaskWhich message types and bits to select; see the next section
loadCountHow many messages to return, oldest first; 0xffffffff means all found; 0 returns an empty messages array
calcSensorsAny non-zero value adds a sensors object to each message, keyed by sensor id
measureMeasurement system for sensor values: 0 metric, 1 US, 2 imperial, 3 metric with gallons; only with calcSensors
sensorIdCalculate this one sensor only; only with calcSensors
filterTrim the pos and p objects to the named fields, for example pos.x,p.pre*

The response is {"count": N, "messages": [...]}. Read count carefully: it is the number of messages found for the interval and it does not depend on loadCount. That makes a request with a small loadCount a cheap way to learn how big an interval is before deciding how to read it.

Three details are easy to miss:

  • gzip is mandatory. A request whose Accept-Encoding header lacks gzip fails with error 1003. Most HTTP libraries send it by default; curl needs --compressed.
  • It counts as a heavy request. load_interval is on the resource-intensive list, and a session may run no more than three of those at once. The fourth fails with error 1003 and the reason LIMIT api_concurrent.
  • Sensor values exist only on data messages. With calcSensors, other message types return -348201.3876 for a numeric sensor and "---" for a text one, per the message format. Filter to data messages before trusting a sensor column.

The calcSensors, measure and sensorId parameters are marked in the reference as available in Wialon Local from version 2604.

Select messages with flags and flagsMask

The two parameters work as a pair. The high byte of the mask selects the message type; the low bits select properties of the message. flags is the value those masked bits must equal. The reference gives these examples, shown here in the decimal form the JSON requires:

flagsMaskflagsSelects
65280 (0xFF00)0All data messages
65296 (0xFF10)16Data messages with the alarm bit
65281 (0xFF01)1Data messages with a position
65281 (0xFF01)1537 (0x0601)Events that are violations
65280 (0xFF00)16384 (0x4000)Messages about triggered notifications

As everywhere in this API, hexadecimal is not accepted in the request. Write 65281, not 0xFF01.

The message type values come from the message format reference:

Type flagDecimalMessage
0x00000Data message
0x0100256SMS
0x0200512Command
0x06001536Event
0x09002304SMS to and from a driver
0x10004096Log record
0x20008192Video usage
0x400016384Triggered notification
0x500020480Task

The full list also has user log, billing and a few more. One type is a trap: online notifications (0x0300) are never returned by load_interval or load_last, because they are not stored on the server. The stored record of a triggered notification is the 0x4000 type.

get_messages reuses the names flags and flagsMask with the opposite meaning. There they exclude: a message is dropped when flags is non-zero and the bitwise AND of the message flags and flags equals flagsMask. With flags 65280 and flagsMask 0, data messages are removed from the response. The values are not interchangeable either: the load_interval pair for data messages (flags 0, flagsMask 65280) copied into get_messages excludes nothing, because flags 0 means no exclusion, and the call raises no error.

Page through a large interval with get_messages

A month of messages for one unit can run to hundreds of thousands of records. Returning them in one response is slow, and a single oversized response is the common reason an extraction dies halfway. The loader exists so you do not have to: load once, then read slices by index.

https://hst-api.wialon.com/wialon/ajax.html?sid=<sid>&svc=messages/get_messages&params={"indexFrom":0,"indexTo":5000}

indexFrom is the first index and indexTo is exclusive: the message with that index is not returned. The reference example, "indexFrom":8,"indexTo":9, returns exactly one message, the one at index 8. If indexTo runs past the end you get messages up to the last one; if indexFrom is at or beyond the end you get an empty array. The response is a bare array of messages, with no count wrapper.

get_messages also accepts time-based selection, and the reference says the request must contain at least one complete pair: timeFrom with timeTo, timeFrom with loadCount, timeTo with loadCount, or the index pair. The documentation does not say whether timeFrom is inclusive, so a time cursor can repeat or skip messages that share a second. Index paging has no such ambiguity.

The official request examples show the pattern: load with loadCount 3 while count reports 9172, then fetch index 8. The loader therefore holds the whole found set, not only the messages returned by the first call. A paging loop in TypeScript:

const HOST = "https://hst-api.wialon.com/wialon/ajax.html";
const PAGE = 5000;

async function call<T>(sid: string, svc: string, params: object): Promise<T> {
  const res = await fetch(HOST, {
    method: "POST",
    body: new URLSearchParams({ svc, params: JSON.stringify(params), sid }),
  });
  const body = await res.json();
  if (body && !Array.isArray(body) && "error" in body) {
    throw new Error(`${svc} failed with error ${body.error}`);
  }
  return body as T;
}

async function* readMessages(sid: string, unitId: number, timeFrom: number, timeTo: number) {
  try {
    const first = await call<{ count: number; messages: object[] }>(sid, "messages/load_interval", {
      itemId: unitId, timeFrom, timeTo, flags: 1, flagsMask: 65281, loadCount: PAGE,
    });
    yield first.messages;
    for (let from = PAGE; from < first.count; from += PAGE) {
      yield await call<object[]>(sid, "messages/get_messages", { indexFrom: from, indexTo: from + PAGE });
    }
  } finally {
    await call(sid, "messages/unload", {});
  }
}

Run it one interval at a time per session, and size the intervals by days rather than months. Message loading is metered per user, not per session: 15 million messages loaded within two minutes, 200 million within an hour, and 15 million held across all of a user’s sessions, all from the limitations page. Hitting one returns error 1004 with the reason LIMIT msgs_count. The error codes page lays out the full budget, and the rate-limit design guide covers retry and backoff, so this page does not repeat them. The unload in the finally block matters because the set lives in the session until you replace or clear it. messages/unload always returns an empty object, even when nothing is loaded, so calling it defensively costs nothing.

Load the latest messages with load_last

When you want the most recent messages rather than an interval, use messages/load_last:

https://hst-api.wialon.com/wialon/ajax.html?sid=<sid>&svc=messages/load_last&params={"itemId":<unitId>,"lastTime":1764633599,"lastCount":50,"flags":0,"flagsMask":65280,"loadCount":50}

lastTime is the point in time the messages are requested for, and lastCount is how many messages to load. Two limits are documented: lastCount must be between 1 and 10000 or the call fails with error 4, and loading unit log messages (0x1000) requires the Manage object log access right. Without that right the log messages are silently left out and no error is returned, so an empty result for a log query is ambiguous until you check the unit’s access. The response has the same shape as load_interval and fills the same loader, so everything above about get_messages and unload applies.

Read a data message and its parameters

A data message, the 0x0000 type, is what a tracker sends. From the format reference:

{
  "t": 1764547261,
  "f": 3,
  "tp": "ud",
  "pos": { "y": 53.8396544, "x": 27.5608672, "z": 213, "s": 0, "c": 51, "sc": 9 },
  "i": 0,
  "p": { "adc1": 12.606, "pwr_ext": 12.598, "param22": 3 },
  "rt": 1764547262
}

t is the message time in UTC and rt is when the server registered it. tp is the type, ud for data. In pos, y is latitude, x longitude, z altitude, s speed, c course and sc the satellite count. lc is the LBS checksum, and each message also has tns, the time in nanoseconds (Wialon Local from 2604).

The f field is a bit field, and reading it decides whether the other fields exist:

BitDecimalMeaning
0x011Location is available
0x022Input data (i) is present
0x044Output data (o) is present
0x1016Alarm bit
0x2032Driver code, in dr and the avl_driver parameter
0x20000131072Corrected by LBS
0x40000262144Registered during a private trip, location unavailable

pos is null when the message has no location, which is routine: a message about an input change or a sensor reading carries parameters and no fix. Check bit 1 or pos before reading coordinates. This is the same split between position and message as on the units page, at the level of a single record. The dr field, and the matching avl_driver parameter, is where the driver codes from the drivers page show up on the raw feed.

What “Wialon parameters” means

The p object is the parameters of a message: a map from parameter name to value, where the format reference types the values as numbers. The example responses in the documentation use names such as adc1, pwr_ext and param22. This object is where everything a device reports beyond position lives, and the set of names depends on the device and how its messages are parsed, so build against the names you actually see in load_interval output for your own units, not against a list.

There are three ways to keep it manageable:

  • Trim at the source. The filter parameter on load_interval and get_messages keeps only the matching parts of pos and p. Masks are comma separated, * matches zero or more characters and ? exactly one. The documentation’s example pos.x,p.pre*,p.param? keeps pos.x, every parameter starting with pre, and param5 but not param. The filter never touches t, f, tp, i or o, and if no pos field matches, pos comes back as an empty object. On a unit with dozens of parameters this is the difference between a response you can parse and one you wait for.
  • Read the accumulated values instead of the history. The unit data flag 1048576 (0x00100000) adds a prms block to the unit: for each parameter name, its latest value v, the time of the last change ct, and the time of the last message that carried it at. For the question “what is this parameter now”, that is one unit request instead of a message load. The unit format reference describes it.
  • Cap the list on busy units. For units with a very large number of parameters, such as ones that relay Bluetooth tags, core/set_prms_limit limits how many of the most recent parameters the server returns per unit for the whole session, and the unit then reports the total in prms_total. It is not available in Wialon Local.

Deal with the other message types

The data message is the common case, but the loader returns anything you select with flags and flagsMask. Events (tp of evt) carry the event text in et, coordinates in x and y, and details such as evt_name in p; their f bits mark violations, maintenance, fuel fillings and route progress. SMS messages (us) have st for the text and mp for the modem phone number. Commands (ucr) have the command name in ca and the execution time in et. All of these have the same envelope of t, f, tp, rt and p, and the format reference has the field tables.

Deleting is the one write operation in this section, and it is narrower than it looks. messages/delete_message takes a msgIndex and works only on a message of a unit that you loaded into the current session. The access right depends on the type: Delete messages for data messages, SMS and commands, Manage events for events. You cannot delete the last message or the last position of a unit; that returns error 4. Treat it as a destructive operation on the unit’s history, not a cleanup of your session.

The errors you will meet

  • Error 4 on load_interval means a missing or mistyped parameter, for example a negative timeFrom. On get_messages it means no pair of parameters was given, or nothing is loaded. On load_last it also covers a lastCount outside 1 to 10000.
  • Error 7 means the item does not exist or does not store messages, or the user lacks the Request reports and messages right. The token page covers access flags.
  • Error 1003 has two causes here: a missing gzip in Accept-Encoding, or the heavy-request ceiling with reason LIMIT api_concurrent. Read the reason field to tell them apart.
  • Error 1004 with LIMIT msgs_count is the message-loading budget. Wait for the window to clear and cut the interval size.

Wialon returns these with HTTP 200, so check the response body rather than the status. Everything else is on the error codes page.

Where next

Messages are the raw material; the report execution calls answer most questions about them without moving them over the wire, and the notification engine produces the 0x4000 records you can read back here. The open-source TypeScript SDK wraps load_interval, load_last and unload with the session handling above. If you need months of raw history queryable in SQL rather than re-extracted per request, that is what FleetSQL stores.

Also in this reference

Let's connect

  • “Our client needed a data pipeline. It came back working, plus a few Wialon fixes we had not asked for. That client trusts us more now.”
    Faiz K. Customer Manager · Trakpro Limited