---
title: Refresh an expired token
summary: Keep a browser page working past the lifetime of its delegated token with the JavaScript SDK's expired-token handler, and what the other SDKs do instead.
order: 80
languages: [javascript, java, csharp]
terms: [auth-token, versions-token, api-token]
see-also: [how-to/auth/create-a-versions-token, how-to/auth/use-an-anonymous-token, explanation/tokens-and-delegation]
notion: https://www.notion.so/bimbase/Auth-Tokens-ba73574cf219406898ee0159b58bd187
---

# Refresh an expired token

For a web page that runs on a short-lived delegated token ([[term:versions-token|versions]], nodes or recursive-node token, or an anonymous login) and may stay open longer than that token lives. The JavaScript SDK can fetch a new [[term:auth-token|auth token]] the moment it needs one, through a handler you write; the handler's job is to ask *your* backend for a fresh token.

## Before you start

- A browser client created with `createClientWithAuth` ([[page:how-to/auth/create-a-versions-token#use]]).
- An endpoint in your backend that checks the user's own session and answers with a new token ([[page:how-to/auth/create-a-versions-token]]).

## 1. Register the handler

The handler receives the expired auth object and must return a promise of a new one. The client stores the new object and uses it from then on.

```javascript {#register}
const client = await factory.createClientWithAuth(auth);

client.registerExpiredTokenHandler(async (expiredAuth) => {
	// Your backend validates the user's own session (a cookie, a header) and issues a new token
	const response = await fetch("/api/bimworks-token", {credentials: "include"});
	if (!response.ok) {
		throw new Error("Could not refresh the BIM.works token");
	}
	return await response.json();   // the auth object created by the backend
});
```

`registerExpiredTokenHandler` is a method of the client ([[page:reference/javascript]]).

## 2. Know when it runs

- Before every request the client compares the token's `expires_millis_utc` with the clock. When the token has expired it calls the handler, waits for the new token, and then sends the request with it. Requests that arrive while a refresh is running wait for the same refresh.
- The check is client-side and clock-based. A token the server rejects for another reason (deleted API token, wrong scope) does not trigger the handler; such errors reach your code as usual.
- Without a handler, a request with an expired token fails with "No expiredTokenHandler is present".
- `createClientWithAuth` itself refuses a token that has already expired ([[error:TOKEN_EXPIRED]]); the handler does not help there, so obtain a fresh token before creating the client.

```tip
Keep the token's lifetime short and the refresh cheap. A refresh is one call to your backend; a stolen token, on the other hand, works until it expires, because delegated tokens cannot be revoked.
```

## 3. In the other SDKs

Java and .NET have no handler; they are meant for backends that log in with an [[term:api-token|API token]], whose auth token lives 90 days. When such a client's token has expired (a service that runs for months), create a new client from the long-lived factory; the same applies to a Java or .NET client made from a delegated token with `createClientWithAuth`. Over HTTP, a `401` with [[error:TOKEN_EXPIRED]] means: log in again and retry.

## Check

Create a versions token with `expires_seconds` of 5, create the client, wait ten seconds, and run a query. The console logs "Token has expired" followed by "Calling expiredTokenHandler", the backend endpoint is called once, and the query answers. Drop the handler and repeat: the query fails instead.
