Wialon TypeScript SDK: Typed Remote API Client

Reference · last checked

Open-source TypeScript client for the Wialon Remote API: token login, units, trips, drivers, reports and typed errors, for Node and the browser.

Every Wialon integration begins by writing the same layer: a function that form-encodes svc and params, posts them to ajax.html, attaches the session, and turns {"error":7} into something a log line can explain. Written against the documentation alone, that layer takes days to get right, because the rules it has to respect are spread across two documentation sites that no longer agree. wialon-sdk is that layer extracted from production code and published under the MIT licence, so an integration can start at the data rather than at the transport.

Source on GitHub · Package on npm

Install it and make the first call

The package has no runtime dependencies and needs only a global fetch, so it runs in Node 18 and later, Bun, Deno, Cloudflare Workers and the browser. It ships both ESM and CommonJS builds with type declarations.

npm install wialon-sdk
import { WialonSdk } from "wialon-sdk";

const sdk = new WialonSdk({ host: "https://hst-api.wialon.com" });

const session = await sdk.auth.loginWithToken({ token: process.env.WIALON_TOKEN! });
const units = await sdk.units.list({ flags: 0x401 });

const trips = await sdk.trips.load({
  unitId: units.items[0].id,
  timeFrom: 1711929600,
  timeTo: 1712016000,
});

await sdk.auth.logout();

The token in that snippet is the 72-character credential described on the token and login page, and the flag it was created with decides what the session can read. A token minted with the authorization form’s default access_type of 0x100 logs in successfully and then fails every data read with error 7, which is the most common reason a first run of any client, this one included, returns nothing useful. Ask for 768 when creating a read-only token.

Let the SDK handle the wire format

The Remote API accepts only POST requests with a form-encoded body, and its JSON parser does not accept hexadecimal values. The SDK builds the body itself and passes flags as numbers, so 0x401 written in TypeScript reaches the server as 1025 and the hex-versus-decimal mistake cannot happen. The Postman page shows the same request format by hand, which is still the quickest way to check a credential before writing code.

Sessions are tracked on the client instance. A successful login stores the session id, the separate GIS session id that reverse geocoding requires, and the user id; every later call sends them without the caller passing anything. Three ways in are supported: loginWithToken for token/login, loginWithAuthHash for core/use_auth_hash when a parent application hands over an auth hash, and useSession for adopting a session id obtained elsewhere. loginWithCredentials remains for legacy and self-hosted installations, since core/login stopped being the supported scheme in October 2015.

Know which services are covered

ServiceCoversWialon commands
authSessionstoken/login, core/use_auth_hash, core/login, core/logout
coreRaw calls and batchingcore/search_items, core/search_item, core/batch
unitsUnit search and lookupcore/search_items, core/search_item
tripsTrips and trip updatesevents/load, unit/update_event_data
eventsDetector eventsevents/load
driversDrivers and bindingsresource/get_unit_drivers, resource/get_driver_bindings, resource/bind_unit_driver
geofencesResources, zones, geocodingcore/search_items, gis_geocode
propertiesCustom item propertiesitem/update_custom_property
maintenanceService intervalsunit/update_service_interval, unit/registry_maintenance_event
filesFile storage, tachograph filesfile/list, file/mkdir, file/rm
reportsReport execution and pagingreport/exec_report, report/get_result_rows, report/cleanup_result
messagesRaw message historymessages/load_interval, messages/load_last, messages/unload

Anything without a typed wrapper goes through sdk.core, which sends an arbitrary svc with the current session attached. The units page explains the data flags passed to units.list, and the reports page explains why report/cleanup_result has to run before each execution — a rule the reports service follows for you.

Keep concurrency inside the session’s ceiling

A session allows ten simultaneous requests, of which three may be heavy, and message loading and report execution both count as heavy. A dashboard that renders several widgets at once tends to request the same unit search more than once, and each duplicate uses up part of that allowance. The SDK de-duplicates identical reads that are already in flight, so concurrent callers share one response; writes are never merged, because a second identical mutation is a second mutation. Each request can opt in or out with a dedupe option.

De-duplication does not raise the ceiling, and nothing on the client can. Workers that share one session share its three heavy slots, so bulk extraction should give each worker its own SDK instance and its own session, as the errors page argues in more detail.

Classify errors before retrying them

Every failure is thrown as a WialonSdkError carrying the Wialon code, the svc that produced it and the HTTP status, which is enough to dispatch on:

import { WialonSdkError } from "wialon-sdk";

try {
  await sdk.units.list({ flags: 0x1 });
} catch (error) {
  if (error instanceof WialonSdkError && error.code === 1) {
    await sdk.auth.loginWithToken({ token }); // invalid session: one re-login, then replay
  }
  throw error;
}

The SDK deliberately does not retry on its own, because the right response depends on the code. Error 1 needs exactly one re-login; error 7 and error 8 are terminal and retrying them walks into the login ceiling that blocks the whole IP address; error 10 needs less concurrency, not more attempts. The errors page has the full code table and a retry classification that can be mapped directly onto error.code.

Route calls through your own backend

A browser application should not hold a Wialon token. Setting apiUrl sends every authenticated call to your own endpoint instead of the Wialon host, where a server can keep the token, add logging and enforce its own limits before forwarding to ajax.html. The gisHost and geocodeUrl options do the same for reverse geocoding, and a custom fetch can add tracing or retries without touching the SDK.

const sdk = new WialonSdk({
  apiUrl: "https://your-backend.example.com/api/wialon",
  timeoutMs: 30_000,
});

For Wialon Local or an account on a regional data centre, set host to that installation. The session returned by login carries the account’s own API host from base_url, which is worth storing alongside the token.

The SDK is open source under the MIT licence and is not affiliated with Gurtam. Issues, pull requests and requests for commands that still lack a typed wrapper go to the GitHub issue tracker.

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