---
title: Log in with an API token
summary: Create a client from an API token in JavaScript, Java, .NET, PHP or with a plain HTTP call, and know what the login gives you.
order: 20
languages: [javascript, java, csharp, php]
terms: [api-token, auth-token, node]
see-also: [how-to/auth/create-an-api-token, how-to/auth/refresh-an-expired-token, explanation/tokens-and-delegation, api, tutorials/openapi/getting-started]
notion: https://www.notion.so/bimbase/API-Tokens-3e4d7846a5fe4de5800f6fab44706c58
---

# Log in with an API token

For every application that holds an [[term:api-token|API token]]: the first lines of code in each SDK. Logging in exchanges the token for an [[term:auth-token|auth token]] that the client sends with every later call.

## Before you start

- An API token ([[page:how-to/auth/create-an-api-token]]).
- The address of your instance, `https://host[:port][/base-path]`, without a trailing slash.
- The SDK installed ([[page:how-to/install]]).

## 1. Create a factory

The factory holds the address and the connection settings and is meant to live as long as your application; one per BIM.works instance you talk to.

## 2. Log in

```code-group {#login} classic
```javascript
import {BimWorksClientFactory} from "@bimworks/jssdk";

const factory = new BimWorksClientFactory(BIM_WORKS_ADDRESS);
const client = await factory.createClientWithApiToken(BIM_WORKS_API_TOKEN);
```
```java
import nl.bimbase.bimworks.client.BimWorksClient;
import nl.bimbase.bimworks.client.BimWorksClientFactory;

try (BimWorksClientFactory factory = new BimWorksClientFactory(BIM_WORKS_ADDRESS)) {
	try (BimWorksClient client = factory.createClientWithApiToken(BIM_WORKS_API_TOKEN)) {
		// use the client
	}
}
```
```csharp
using System;
using BIM.works_SDK;

using (BimWorksClientFactory factory = new BimWorksClientFactory(new Uri(BIM_WORKS_ADDRESS)))
using (BimWorksClient client = factory.CreateClientWithApiToken(BIM_WORKS_API_TOKEN))
{
    // use the client
}
```
```

```sdk-example
operation: LoginWithApiToken
body: |
  {
    "token": BIM_WORKS_API_TOKEN
  }
```

The factory method is `createClientWithApiToken` in JavaScript ([[page:reference/javascript]]) and Java ([[page:reference/java]]) and `CreateClientWithApiToken` in .NET ([[page:reference/dotnet]]); the PHP client calls `AuthApi::loginWithApiToken` ([[page:reference/sdk/php]]). Java's and .NET's factory and client are `AutoCloseable`/`IDisposable`: close a client you no longer use, it holds a WebSocket connection.

With plain HTTP the operation is [[openapi:LoginWithApiToken]]. Ask for the `base64` response format to receive the bearer token as a string, and send it in the `Authorization` header of every later call:

```try-it
operation: LoginWithApiToken
method: POST
path: /openapi/auth/LoginWithApiToken
body: |
  {
    "token": BIM_WORKS_API_TOKEN,
    "response_format": "base64"
  }
```

Without `response_format` the reply is the full auth object (`input`, `hmac`, `hmh`, `base64`); the `base64` field is the same string.

## 3. Know what you got

- **A root node.** The client sees the [[term:node|node]] the token was created on and everything below it, nothing else; paths such as `/Project A/house.ifc` are relative to it. `fs.getRootNodeUuid()` returns it.
- **The token's scopes.** A call outside them fails with [[error:NON_AUTHORIZED_SCOPE]].
- **An auth token valid for 90 days** (the instance setting `auth.api_token_validity`), but never beyond the API token's own expiry date. A long-running Java or .NET process creates a new client when its auth token has expired; a browser can do that automatically ([[page:how-to/auth/refresh-an-expired-token]]). For a shorter one, the `LoginWithApiToken` task (not yet the HTTP operation) takes `expires_seconds`: a sign-in handed to another device, such as the phone link of [[page:demos/repair-annotations]].
- **Every request is checked against the token again**, so deleting or rotating the token in the UI cuts off running clients at once.

```note
The login fails with [[error:INVALID_API_TOKEN]] for an unknown secret, [[error:API_TOKEN_DELETED]] for a deleted one and [[error:API_TOKEN_EXPIRED]] after its `expires_on`. Repeated failures from one address are throttled and then answer `INVALID_API_TOKEN` regardless of the secret.
```

## Check

Ask for the root node; the UUID is the folder the token was created on:

```code-group {#check} classic
```javascript
console.log(await client.fs.getRootNodeUuid());
```
```java
System.out.println(client.fs.getRootNodeUuid());
```
```csharp
Console.WriteLine(client.Fs.GetRootNodeUuid());
```
```

```sdk-example
operation: GetRootNodeUuid
```

`getRootNodeUuid` is on the file system API of each SDK: [[js:@bimworks/jssdk/Fs|Fs]], [[java:nl.bimbase.bimworks.client.apis.FileSystemApi|FileSystemApi]], [[dotnet:BIM.works_SDK.Fs|Fs]], `FsApi` in [[page:reference/sdk/php]]; the HTTP operation is [[openapi:GetRootNodeUuid]].
