---
title: Create a versions token
summary: Issue a short-lived token for an exact set of model versions in your backend and use it in the browser.
order: 40
languages: [javascript, java, csharp, php]
terms: [versions-token, auth-token, scope, version]
see-also: [how-to/auth/create-a-nodes-token, how-to/auth/create-a-recursive-node-token, how-to/auth/refresh-an-expired-token, explanation/tokens-and-delegation, tutorials/dotnet/quick-start]
notion: https://www.notion.so/bimbase/Auth-Tokens-ba73574cf219406898ee0159b58bd187
---

# Create a versions token

For a backend that decides which models a user may see and wants the browser to load exactly those. A [[term:versions-token|versions token]] names a set of [[term:version|versions]], possibly from different nodes, and gives access to nothing else; it is the delegated token you want most of the time.

## Before you start

- A backend logged in with an [[term:api-token|API token]] (or an account) that has read rights on the versions and the `tokens:issue` scope. Delegated and anonymous tokens cannot issue tokens ([[error:CREDENTIAL_CREATION_NOT_ALLOWED]]).
- The version UUIDs. A node's current version is its `last_version_uuid` ([[page:how-to/models/list-the-tree]]).
- The [[term:scope|scopes]] the page needs. A viewer page needs `fs:read`, `geometry:read` and `query:run`, and `gis:read` when it shows a map; `geometry:read` deliberately does not include `model:read`, so a viewer link cannot download the source file. You cannot grant a scope your own credential lacks.

## 1. Choose the lifetime

`expires_seconds` is required and there is no revocation: pick the shortest time the page needs, and refresh from the backend when it runs out ([[page:how-to/auth/refresh-an-expired-token]]). A page that stays open for a working day is better served by refreshing hourly than by an eight-hour token.

## 2. Create the token in the backend

```code-group {#create} classic
```javascript
// Node.js backend: 30 minutes, scopes as wire strings
const result = await client.tokens.createVersionsToken(
	[BIM_WORKS_FIRST_MODEL, BIM_WORKS_SECOND_MODEL], 30 * 60,
	null, false, false, ["fs:read", "geometry:read", "query:run"]);
const auth = result.auth;   // the JSON to send to the browser
```
```java
import java.time.Duration;
import java.util.EnumSet;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.databind.node.ObjectNode;
import nl.bimbase.bimworks.client.Scope;

Set<UUID> versions = Set.of(UUID.fromString(BIM_WORKS_FIRST_MODEL), UUID.fromString(BIM_WORKS_SECOND_MODEL));
ObjectNode auth = client.tokens.createVersionsToken(versions, Duration.ofMinutes(30),
		EnumSet.of(Scope.FS_READ, Scope.GEOMETRY_READ, Scope.QUERY_RUN), null, false);
String forBrowser = auth.toString();   // the JSON to send to the browser
```
```csharp
ISet<Guid> versions = Collections.CreateHashSet(Guid.Parse(BIM_WORKS_FIRST_MODEL), Guid.Parse(BIM_WORKS_SECOND_MODEL));
ISet<Scope> scopes = new HashSet<Scope> { Scope.FS_READ, Scope.GEOMETRY_READ, Scope.QUERY_RUN };
CreateVersionsTokenResponse response = client.Tokens.CreateVersionsToken(
    new CreateVersionsTokenRequest(versions, scopes, TimeSpan.FromMinutes(30)));
AuthToken auth = response.GetAuth();
string forBrowser = auth.GetJson().GetRawText();   // the JSON to send to the browser
```
```

```sdk-example
operation: CreateVersionsToken
body: |
  {
    "version_uuids": [BIM_WORKS_FIRST_MODEL, BIM_WORKS_SECOND_MODEL],
    "expires_seconds": 1800,
    "scopes": ["fs:read", "geometry:read", "query:run"]
  }
```

The methods: `createVersionsToken` on [[js:@bimworks/jssdk/Tokens|Tokens]] and [[java:nl.bimbase.bimworks.client.apis.TokensApi|TokensApi]], `CreateVersionsToken` on [[dotnet:BIM.works_SDK.Tokens|Tokens]], `AuthApi::createVersionsToken` in [[page:reference/sdk/php]]. Over HTTP the operation is [[openapi:CreateVersionsToken]]:

```try-it
operation: CreateVersionsToken
method: POST
path: /openapi/auth/CreateVersionsToken
body: |
  {
    "version_uuids": [BIM_WORKS_FIRST_MODEL, BIM_WORKS_SECOND_MODEL],
    "expires_seconds": 1800,
    "scopes": ["fs:read", "geometry:read", "query:run"]
  }
```

The fields, for all three delegated tokens:

| Field | Meaning |
|---|---|
| `expires_seconds` | Required. Lifetime from now: at most 90 days (the instance's `auth.issued_token_max_validity`), and never past the expiry of the API token that asks for it. A longer request is cut, not refused. |
| `version_uuids` | The versions (for nodes and recursive-node tokens: `node_uuids` or `node_uuid`). |
| `scopes` | The scopes granted; at most the issuer's own. Preferred. Left out, a token made with an API token gets that token's scopes, never none. |
| `tasks` | Deprecated: an allow-list of task names. It does not cover calls added later, and the reply carries a `deprecation` notice when only `tasks` is given. On a token made with an API token, a listed task runs with the token's scopes and its own, under the same entitlement checks as `scopes`. |
| `ip_addresses` | Addresses that may use the token; empty means any. |
| `lock_to_caller_ip` | Restrict the token to the address that asks for it. Over HTTP the field is called `lock_to_called_ip`; the SDKs' task calls accept both names. Ignored when `ip_addresses` is given. |
| `link_account` | Record the issuing account in the token, so activity is attributed to it. |
| `response_format` | HTTP only: `auth` (the object, default) or `base64` (the bearer string). |

## 3. Use it in the browser

Send the auth JSON to the page over your own channel and create a client from it. The client validates the token with the server; an already expired token is refused before any call is made ([[error:TOKEN_EXPIRED]]).

```code-group {#use}
```javascript
const factory = new BimWorksClientFactory(BIM_WORKS_ADDRESS);
const client = await factory.createClientWithAuth(auth);   // the object from the backend
const objects = await client.bim.query([BIM_WORKS_FIRST_MODEL], "GET qid, attribute.Name WHERE type = IFCDOOR");
```
```java
// A Java client from a delegated token, for example in a test of the token
try (BimWorksClient delegated = factory.createClientWithAuth(auth)) {
	delegated.bim.query("GET qid WHERE type = IFCDOOR", UUID.fromString(BIM_WORKS_FIRST_MODEL));
}
```
```csharp
using (BimWorksClient delegated = factory.CreateClientWithAuth(auth))
{
    delegated.Bim.Query(Guid.Parse(BIM_WORKS_FIRST_MODEL), "GET qid WHERE type = IFCDOOR");
}
```
```

`createClientWithAuth` is on the factory of each SDK ([[page:reference/javascript]], [[page:reference/java]], [[page:reference/dotnet]]); the HTTP equivalent is the `base64` string in the `Authorization: Bearer` header.

## Check

With the browser client, query one of the versions: it answers. Query a version that is not in the token: it fails with [[error:AuthorizationError.NO_READ_RIGHTS]]. Call something outside the scopes, for example a download with only `geometry:read`: it fails with [[error:NON_AUTHORIZED_SCOPE]].
