Classic JavaScript SDK

The classic, hand-written JavaScript SDK (the @bimworks/jssdk package) that the viewers build on — installation, the client factory and its login methods, the sub-APIs of a client, error handling, Node.js differences, and the generated TypeScript reference.

Classic SDK

This is the classic, hand-written JavaScript SDK. The 3D and 2D viewers build on it, so a page with a viewer uses it. For an integration without a viewer, the TypeScript SDK (@bimworks/sdk, for TypeScript and JavaScript) is generated from the OpenAPI document; SDKs compares the two.

@bimworks/jssdk is the classic API client for browsers and Node.js. The viewers (@bimworks/viewer3d, @bimworks/viewer2d, @bimworks/viewercommon) build on it; installing all of them is described in JavaScript and TypeScript.

Package#

Package @bimworks/jssdk on https://npm.bim.works (scope @bimworks; @sascom for the transport packages it depends on)
Version The same as your BIM.works server; the version pill at the top of this page is the server's
Module format ES modules only (no ES5 or UMD builds); single-file builds exist for pages without a build step (Downloads)
Requirements A current browser, or Node.js 22.4 or later (the Node.js entry point uses the built-in WebSocket, stable since 22.4)
Types jssdk.d.ts ships in the package

Client factory#

BimWorksClientFactory(address[, settings]) is created once per application with the instance URL; every create* method returns a promise of a logged-in BimWorksClient.

Method Logs in with
createClientWithApiToken(apiToken) An API token — the normal way for an application.
createClientWithUsernamePassword(username, password) A user's credentials; for testing.
createClientWithAuth(auth) An auth token obtained elsewhere (a versions or nodes token, a token issued by your backend).
createAnonymousClient() No credentials; a very small set of calls is allowed.
JavaScript#
import {BimWorksClientFactory} from "@bimworks/jssdk";

const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");
const results = await client.bim.query(["<version uuid of the first demo model>"], "GET qid, attribute.Name WHERE type = IFCWALL");
console.log(results);
client.close();

client.registerExpiredTokenHandler(fn) is called when the token expires, client.addConnectionRestoredListener(fn) after a reconnect, and client.close() releases the connection.

Sub-APIs#

A client is a set of API objects, each a class in the TypeScript reference:

Property Class What it does
client.auth Auth Account registration (register); the factory handles login.
client.fs Fs Nodes and versions: root node, paths, listing, creating, moving, deleting, node errors.
client.upload Upload Uploading a model from a File, a dropped item, an ArrayBuffer or (Node.js) a file name, with progress; uploadFromUrl fetches a URL in the page (same-origin, or from a server that allows the page's origin) and uploads the bytes.
client.bim Bim BQL: query, queryCount, queryQids, validateQuery, grouped queries and disciplines.
client.ifc Ifc IFC downloads (original, converted to another schema, ifcXML), IFC exports, and model compare (compareModels and the calls around it).
client.checks Checks Running model checks and IDS checks, fetching results.
client.tokens Tokens Versions, nodes and recursive-node tokens, and single-call 3D geometry download tokens. queryDelegated only posts a payload under the client's own credential (delegated query).
client.gis Gis Geo-references (reading, setting, copying from another version, and extracting them from an EPSG code, WKT, IfcSite or IfcMapConversion), CityGML 2 and 3 exports and 3D Tiles URLs.
client.geometry3d Geometry3d Geometry metadata, glTF downloads, alignments (setAlignment, calculateAlignment) and the semantics of DWG and PDF models.
client.render Render Server-side renders and snapshots.
client.info Info The server's application version (getApplicationVersion) and what the server made of the client's identification (getClientInfo).
client.ai Ai Natural-language text-to-action (textToAction).

The viewers add ModelManager, which takes the client, and ObjectStateManager (both @bimworks/viewercommon), which drive Viewer3d and Viewer2d; Qid, QidSet and QidMap are the identifier types every call returns and accepts (Qid).

Errors#

Most API calls return a promise. The calls built on the transport's execAsPromise(), such as Bim.query, Ai.textToAction and the Fs and Tokens calls, reject on failure with an object {task, error, response}: task is the request's task (task.taskName names the server task that failed), error the server's error (its code, the constant's name, the enumeration and the message — the same codes as the OpenAPI envelope) and response the raw response. Render.render, Render.renderToStillImage, the Checks downloads (downloadCheckResultsAsBcf, downloadCheckResultsAsBcfDirect, downloadCheckGltf) and executePreSignedDownload reject with the same object, also when the server answers without a download. Auth.register, the download and export calls (Ifc.exportIfc, Geometry3d.downloadGltf, …) and the Gis extract, CityGML and 3D Tiles calls reject with the server's error itself. try/catch around an await handles a rejection; codes are listed on the error codes page. Methods such as close() and Bim.setVirtualFeatures() return synchronously; Checks.runChecksAsTask() returns a transport Task, which is sent when you call its execAsPromise() (or exec() after success and error handlers).

JavaScript#
try {
  await client.bim.query(["<version uuid of the first demo model>"], "GET qid WHERE type = IFCWAL");
} catch (failure) {
  console.error(failure.task.taskName, failure.error); // Query2 …
}

Node.js#

Node.js selects the package's Node entry point automatically for import {BimWorksClientFactory} from "@bimworks/jssdk"; uploads from Node go over HTTP instead of the WebSocket transport. Logging is configured with log.setLogger and bimworks.setLogLevel.

JavaScript and TypeScript use the same package and API. TypeScript declarations include the client's auth, info, ai and reconnect callback, and Bim.queryCount; the catalogue below documents these members alongside the runtime APIs.

Generated reference#

The JavaScript API catalogue is generated from the .d.ts files of @bimworks/jssdk, @bimworks/viewercommon, @bimworks/viewer3d, @bimworks/viewer2d and the @sascom transport packages. Types and members share this site's navigation, styling and search, including signatures, overloads, parameters and remarks.

Used in#

Pages#