How-to guide·Task APIJavaScript

Link data to objects from code

Link a file, a URL, a text or an identifier to objects of a version with the JavaScript SDK or the OpenAPI, list what a later version shows, download a file, replace or delete an item, and confirm, re-attach or end a link.

Linked data attaches files, URLs, texts and identifiers to objects without changing the model: no new version is made, and each link follows its object to the later versions (Linked data). The JavaScript SDK has the calls as client.linkedData; the OpenAPI has them as the operations of the LinkedData tag, from LinkData to EndLink, under /openapi/linkeddata/. Both answer the same JSON. The shell tabs below are the OpenAPI calls with curl, where $TOKEN is an auth token (Authentication and scopes).

Before you start#

  • A client whose token has the scope links:write to link, edit and delete, and links:read to list and download. The LINKED_DATA feature only shows the UI; these calls need the scopes.
  • Write rights on the model to link and delete, read rights to list and download.
  • client.linkedData.policy() (GetLinkedDataPolicy) answers what your instance accepts: the file families with their extensions, the limits, the categories and the URL schemes. Linked data file types lists the defaults.

A link call takes the version the objects are on (version_uuid, a Vid), the objects as targets and the data as items, and links every item to every target. A target is {oid} (an object id on that version), {qid} (a Qid of that version) or {global_id} (an IFC GlobalId). An item has a kind (FILE, URL, TEXT or IDENTIFIER), its value, and optionally title, description, category (one of the instance's categories) and valid_until (yyyy-mm-dd).

In JavaScript an item with a file (a File, a Blob or bytes) is a file; the SDK sends it as a stream of the task, and takes the file name from a File (give filename for a Blob or bytes). LinkData is a multipart request: the JSON part request holds version_uuid, targets and items, and each file part is a file, taken by the FILE items in order; an item without a filename gets the part's.

The answer has one entry per item, with the index of the item in the request: the item with its links, or the error and message of a refused item. One refused file does not refuse the others: FILE_TYPE_NOT_ALLOWED, FILE_CONTENT_MISMATCH, MACROS_NOT_ALLOWED and the other file errors are per item. A target that is not in the version fails the whole call with LINK_TARGET_NOT_FOUND (HTTP 404), and a GlobalId on several objects with AMBIGUOUS_GLOBAL_ID (400); its details list the object ids, so link by oid instead.

Without a title, an item is named after its file name without the extension, the URL's host, the identifier's system and value, or the text's first line. A text up to 16 KiB is kept in the item; a longer one is stored as a file. One call takes at most 20 items and 50,000 targets (TOO_MANY_FILES, TOO_MANY_TARGETS). With strip_metadata: true a JPEG, PNG or WebP photo loses its location and camera details losslessly, keeping its orientation; a HEIC or TIFF is stored as it is, and its facts.metadata_kept says why.

To link an item that exists to more objects, client.linkedData.addTargets(itemUuid, versionUuid, targets) (AddLinkTargets); a target it is on already is left as it is.

2. List what a later version shows#

After a new upload or a BUL transaction, the list of the new version answers the links as they were followed onto it (ListLinkedData). The call follows the model up to that version first, so the answer is never out of date.

const listed = await client.linkedData.list(newVersionUuid, {
	oids: [61]              // optional: only these objects; qids works too
});
for (const link of listed.links) {
	const item = listed.items.find((i) => i.uuid === link.item_uuid);
	console.log(item.title, "on object", link.oid_here, link.how, link.state);
}
curl -s -X POST "$ADDRESS/openapi/linkeddata/ListLinkedData" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"version_uuid": ""<version uuid of the first demo model>"", "oids": [61]}'

items holds each item once; links holds one entry per object, with oid_here (the object id on this version), state and how it was found there:

how Found by
LINKED made on this version
SAME_OID the same object id, after a transaction, a conversion or a branch
SAME_CONTENT the same object id, after an upload of the identical file
PAIRED a pair fixed by hand in a compare of the two versions
GLOBAL_ID its GlobalId, after an upload
MATCHED an object of the same type and size within 500 mm that is new in this version, when no object has the GlobalId any more; always CHECK, with a confidence in the trail
RESUMED its GlobalId, back after the object was gone
CONFIRMED a MATCHED or ambiguous GLOBAL_ID pairing someone confirmed
REATTACHED someone put the link on this object from this version on

state is OK, or CHECK when BIM.works is not certain (two objects with the GlobalId: candidates lists them). An orphaned link has no object on the version and is listed only with include_orphaned: true (includeOrphaned in JavaScript): it has state ORPHANED, no oid_here, and the global_id, ifc_type and object_name of the object it was last on. span_first_version says since which version the link is where it is. kinds and categories filter the list.

Two options serve a review of the whole model. with_objects: true (withObjects) adds object_here (oid, name, ifc_type, global_id) to every link, and to a CHECK link object_before: the object it was on before the pairing, with its version. include_deleted: true (includeDeleted) adds the deleted items that can still be restored (they carry deleted_on); it needs write rights on the model.

include_later: true (includeLater) adds the links made on later versions whose object is on this one, found by following them backwards (Links added in later versions). Their first_version is above the version listed and their direction is BACKWARD; backward_through_version says how far down a link was followed (0 when it stopped). The first such call for an older version does the lookups and stores them, so later calls only read.

POST/openapi/linkeddata/ListLinkedDataListLinkedData reference
Authorization
Bearer <auth token>
Content-Type
application/json
Body
1 2 3 4
Authorization: Bearer <auth token>
Content-Type: application/json
Accept: application/json
curl -X POST "$BIMWORKS_URL/openapi/linkeddata/ListLinkedData" \
  -H "Authorization: Bearer $BIMWORKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"version_uuid": BIM_WORKS_FIRST_MODEL, "with_objects": true}'
Send to see the response

To colour a viewer, client.linkedData.counts(versionUuid) (GetLinkedDataCounts) answers only which objects have linked data: objects, those to check (check), the objects per file family (by_family), one marker per object with its item count and most common family, and the totals. One item with all its links and their trail across versions is client.linkedData.get(itemUuid) (GetLinkedData).

3. Download a file#

The download answers the item's content: a file byte for byte as uploaded, with its media type and file name; a URL, an identifier or a text as plain text. In JavaScript, download answers the bytes; DownloadLinkedData answers them as the body, with the content's SHA-256 in X-BimWorks-Sha256.

const bytes = await client.linkedData.download(item.uuid);   // an ArrayBuffer
curl -s -X POST "$ADDRESS/openapi/linkeddata/DownloadLinkedData" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"item_uuid": "<item uuid>"}' -OJ

A file stored compressed goes out with Content-Encoding: zstd to a client of the task API that accepts it and decoded to one that does not, so download gives the original bytes either way; the OpenAPI always answers it decoded.

For a browser, ask for a URL instead of the bytes: client.linkedData.downloadUrl(itemUuid), or delivery: "url" in the request, answers {url, expires, disposition}. The URL works for 10 minutes without a token or session, so treat it like the file itself. It supports Range (a PDF viewer or a video player fetches only what it shows), HEAD and If-None-Match. It sends the file as a download, or with {inline: true} (disposition: "inline") shows it in the browser when the type allows it (PDF, JPEG, PNG, GIF, WebP, MP4, WebM, MP3, M4A, OGG, WAV, text); disposition in the answer says which it will be.

const {url, disposition} = await client.linkedData.downloadUrl(item.uuid, {inline: true});
window.open(url, "_blank", "noopener");
curl -s -X POST "$ADDRESS/openapi/linkeddata/DownloadLinkedData" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"item_uuid": "<item uuid>", "delivery": "url", "disposition": "inline"}'

On a server with a separate content origin (linked_data.content_origin) the URL is on that host name.

4. Replace an item from a version on#

A pump swapped in v12 gets a new datasheet: client.linkedData.replace(itemUuid, fromVersionUuid, item) (ReplaceLinkedData, multipart as LinkData, with item_uuid, from_version_uuid and item in the request part) adds the new item to the objects the old one is on in that version and ends the old one's links from there, so the older versions keep the old datasheet. The answer has the new item and its links, the old links it ended, and deleted when the old item was on that version only (it is then deleted, restorable). The new item's replaces_item_uuid names the old one, and the details of either name the other (replaces, replaced_by).

const replaced = await client.linkedData.replace(oldItem.uuid, "<version uuid of the first demo model>", {file: newFile, category: "Datasheet"});
curl -s -X POST "$ADDRESS/openapi/linkeddata/ReplaceLinkedData" -H "Authorization: Bearer $TOKEN" \
  -F 'request={"item_uuid": "<old item uuid>", "from_version_uuid": ""<version uuid of the first demo model>"",
    "item": {"kind": "FILE", "category": "Datasheet"}};type=application/json' \
  -F "file=@CR10-datasheet-2.pdf"

5. Delete an item#

Deleting removes items from every object on every version (DeleteLinkedData).

const {deleted, retention_days} = await client.linkedData.remove([item.uuid], "Uploaded to the wrong pump");
curl -s -X POST "$ADDRESS/openapi/linkeddata/DeleteLinkedData" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"item_uuids": ["<item uuid>"], "reason": "Uploaded to the wrong pump"}'

It needs write rights on the item's model; the user who added an item may also delete it within 24 hours without them. A deleted item can be restored for retention_days (30 by default) with client.linkedData.restore(itemUuids) (RestoreLinkedData, {item_uuids} → {restored}); after that the call answers RETENTION_PASSED (HTTP 410). To take an item off one object only, call client.linkedData.unlink(linkUuids) (UnlinkData) with the link's uuid from the list ({link_uuids} → {unlinked, items_deleted}): an item that would lose its last object is deleted instead, and a restore puts it back on that object.

6. Confirm, re-attach or end a link#

A link marked CHECK is settled with confirm (ConfirmLinkMatch: the object is right) or reattach (ReattachLink: another object, from that version on; the older versions keep theirs). end (EndLink) takes a link off a version and the later ones and keeps it on the older ones. Each takes the link's uuid from the list and a version of its model, and answers the link with its trail (anchors).

Confirming writes the pair as a compare pair override of the two versions, so the compare pairs them the same way. A link that is not CHECK on the version answers INVALID_ARGUMENT, as does ending a link on the version it was made on (unlink it instead).

Check#

List the version again: a confirmed link has state OK, an ended one is not listed, and a deleted item is gone from items and links, with a download of it answering LINKED_DATA_DELETED (HTTP 404).

Since 3.0