---
title: Link data to objects from code
summary: 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.
order: 10
since: 3.0
languages: [javascript]
terms: [linked-data, version, vid, oid, qid, guid, scope, orphaned-link]
see-also: [explanation/linked-data, reference/linked-data-file-types, how-to/using-bimworks/link-data-to-objects]
---

# Link data to objects from code

[[term:linked-data|Linked data]] attaches files, URLs, texts and identifiers to objects without changing the model: no new [[term:version|version]] is made, and each link follows its object to the later versions ([[page:explanation/linked-data]]). The JavaScript SDK has the calls as `client.linkedData`; the OpenAPI has them as the operations of the LinkedData tag, from [[openapi:LinkData]] to [[openapi: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 ([[page:api/authentication]]).

## Before you start

- A client whose token has the [[term:scope|scope]] `links:write` to link, edit and delete, and `links:read` to list and download. The [[feature: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()` ([[openapi:GetLinkedDataPolicy]]) answers what your instance accepts: the file families with their extensions, the limits, the categories and the URL schemes. [[page:reference/linked-data-file-types]] lists the defaults.

## 1. Link a file, an identifier and a URL {#link}

A link call takes the version the objects are on (`version_uuid`, a [[term:vid|Vid]]), the objects as `targets` and the data as `items`, and links every item to every target. A target is `{oid}` (an [[term:oid|object id]] on that version), `{qid}` (a [[term:qid|Qid]] of that version) or `{global_id}` (an IFC [[term:guid|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). [[openapi: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.

````code-group {#link-data}
```javascript
// file: a File from an <input type="file">
const {items} = await client.linkedData.link(BIM_WORKS_FIRST_MODEL, [{global_id: "25Lw7hJ6P6vwtz3VbZJ0Q2"}], [
	{file: file, category: "Datasheet"},
	{kind: "IDENTIFIER", identifier: {system: "SAP", value: "10004711"}},
	{kind: "URL", url: "https://example.com/pumps/cr10", title: "Supplier page"}
]);
for (const entry of items) {
	if (entry.error) {
		console.warn(entry.filename, entry.error, entry.message);
	} else {
		console.log(entry.item.uuid, entry.item.title, entry.links.map((link) => link.oid_here));
	}
}
```
```shell
ADDRESS=BIM_WORKS_ADDRESS
curl -s -X POST "$ADDRESS/openapi/linkeddata/LinkData" -H "Authorization: Bearer $TOKEN" \
  -F 'request={"version_uuid": "'BIM_WORKS_FIRST_MODEL'", "targets": [{"global_id": "25Lw7hJ6P6vwtz3VbZJ0Q2"}],
    "items": [{"kind": "FILE", "category": "Datasheet"},
      {"kind": "IDENTIFIER", "identifier": {"system": "SAP", "value": "10004711"}},
      {"kind": "URL", "url": "https://example.com/pumps/cr10", "title": "Supplier page"}]};type=application/json' \
  -F "file=@CR10-datasheet.pdf"
```
````

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)` ([[openapi:AddLinkTargets]]); a target it is on already is left as it is.

## 2. List what a later version shows {#list}

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

````code-group {#list-linked-data}
```javascript
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);
}
```
```shell
curl -s -X POST "$ADDRESS/openapi/linkeddata/ListLinkedData" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"version_uuid": "'BIM_WORKS_FIRST_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 [[term:orphaned-link|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 ([[page:explanation/linked-data#links-added-later|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.

```try-it
operation: ListLinkedData
method: POST
path: /openapi/linkeddata/ListLinkedData
body: |
  {"version_uuid": BIM_WORKS_FIRST_MODEL, "with_objects": true}
```

To colour a viewer, `client.linkedData.counts(versionUuid)` ([[openapi: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)` ([[openapi:GetLinkedData]]).

## 3. Download a file {#download}

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; [[openapi:DownloadLinkedData]] answers them as the body, with the content's SHA-256 in `X-BimWorks-Sha256`.

````code-group {#download-linked-data}
```javascript
const bytes = await client.linkedData.download(item.uuid);   // an ArrayBuffer
```
```shell
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.

````code-group {#download-url}
```javascript
const {url, disposition} = await client.linkedData.downloadUrl(item.uuid, {inline: true});
window.open(url, "_blank", "noopener");
```
```shell
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 {#replace}

A pump swapped in v12 gets a new datasheet: `client.linkedData.replace(itemUuid, fromVersionUuid, item)` ([[openapi: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`).

````code-group {#replace-linked-data}
```javascript
const replaced = await client.linkedData.replace(oldItem.uuid, BIM_WORKS_FIRST_MODEL, {file: newFile, category: "Datasheet"});
```
```shell
curl -s -X POST "$ADDRESS/openapi/linkeddata/ReplaceLinkedData" -H "Authorization: Bearer $TOKEN" \
  -F 'request={"item_uuid": "<old item uuid>", "from_version_uuid": "'BIM_WORKS_FIRST_MODEL'",
    "item": {"kind": "FILE", "category": "Datasheet"}};type=application/json' \
  -F "file=@CR10-datasheet-2.pdf"
```
````

## 5. Delete an item {#delete}

Deleting removes items from every object on every version ([[openapi:DeleteLinkedData]]).

````code-group {#delete-linked-data}
```javascript
const {deleted, retention_days} = await client.linkedData.remove([item.uuid], "Uploaded to the wrong pump");
```
```shell
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)` ([[openapi: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)` ([[openapi: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 {#settle}

A link marked `CHECK` is settled with `confirm` ([[openapi:ConfirmLinkMatch]]: the object is right) or `reattach` ([[openapi:ReattachLink]]: another object, from that version on; the older versions keep theirs). `end` ([[openapi: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`).

````code-group {#settle-links}
```javascript
for (const link of listed.links.filter((l) => l.state === "CHECK")) {
	if (link.how === "MATCHED") {
		await client.linkedData.confirm(link.uuid, newVersionUuid);
	} else {
		// One of several objects with the GlobalId: put it on the right one, by oid, qid or GlobalId
		await client.linkedData.reattach(link.uuid, newVersionUuid, {oid: link.candidates[1]});
	}
}
await client.linkedData.end(listed.links[0].uuid, newVersionUuid);
```
```shell
curl -s -X POST "$ADDRESS/openapi/linkeddata/ConfirmLinkMatch" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"link_uuid": "<link uuid>", "version_uuid": "<new version uuid>"}'
curl -s -X POST "$ADDRESS/openapi/linkeddata/ReattachLink" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"link_uuid": "<link uuid>", "version_uuid": "<new version uuid>", "target": {"oid": 64}}'
curl -s -X POST "$ADDRESS/openapi/linkeddata/EndLink" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"link_uuid": "<link uuid>", "from_version_uuid": "<new version uuid>"}'
```
````

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).
