# dataClient() function

> The dataClient() function returns a client for player data, collections and leaderboards that needs no room connection.

The **`dataClient()`** function returns a [`DataClient`](/docs/client/data-client): the player's data and the project's collections and leaderboards, for the player the client is, with no room connection needed.

## Syntax

```ts
dataClient()
dataClient(init)
```

### Parameters

`init` Optional
: An object with any of these properties:

  `project`
  : A string or a `URL`: the project URL, such as `"https://rooms.irt.io/my-game/"`. Defaults to the page's `irt-project` meta tag.

  `token`
  : A string: a player token from your backend. Without one, the client is a guest, the same one as its room connections.

  `guest`
  : A string: where a guest's token is kept. `"tab"` keeps it in `sessionStorage`, so each tab is its own player, and `"browser"` keeps it in `localStorage`, so every tab of the browser is one player. Unused with a `token`. Defaults to `"tab"`.

  `signal`
  : An `AbortSignal` that aborts every request of the client. Each call also takes a `signal` of its own, for that call. A request either aborts rejects with a `DataError` with code `E_UNAVAILABLE`, named `AbortError`, or `TimeoutError` for an `AbortSignal.timeout()`, with the signal's reason as its `cause`.

### Return value

A `DataClient`.

### Exceptions

`TypeError`
: Thrown if there's no project URL, no `project` given and no `irt-project` meta tag, or if the project URL's path doesn't end in a project.

## Description

The calls go over the router's `POST /v1/data/{project}`, and are the same as the Room API's [`PlayerData`](/docs/room/player-data), [`Collection`](/docs/room/collection) and [`Leaderboard`](/docs/room/leaderboard) calls, limited to what a client may do. Nothing is cached on the client: every call goes to the router. A connected [`RoomClient`](/docs/client/room-client) has the same calls, as [`room.playerData`](/docs/client/room-client/playerdata), [`room.collection()`](/docs/client/room-client/collection) and [`room.leaderboard()`](/docs/client/room-client/leaderboard), made with the connection's token or guest.

Without a `token`, the first call takes the guest token `connect()` keeps for the same `guest` setting, or gets one from `POST /v1/guest` and keeps it there, so a guest is one player in rooms and data calls. An expired guest token gets a new guest, as connecting does. With storage blocked, the token lives as long as the page.

A leaderboard's `top()`, and `around()` with a `player`, go without a token until the client has one. With the project's `players.readLeaderboardsWithoutToken` on, they need no guest; otherwise the router's 401 gets one first.

Calls reject with a [`DataError`](/docs/room/data-error): `E_UNAVAILABLE` when the router or the data service can't be reached or answers 503; `E_LIMIT` over the router's 10 calls a second per address and per player, or 30 guests a minute per address for each project; and `E_FORBIDDEN` for a rejected token or a call the project's settings don't allow a client. See [Errors and close codes](/docs/client/errors#data-calls).

## Examples

### Showing the week's best on the start screen

A start screen shows a leaderboard before the player joins any room.

```ts
// start-screen.ts
import { dataClient, DataError } from "@irtio/client";

const data = dataClient();

export async function weekBoard() {
  try {
    const { entries } = await data.leaderboard("laps").top({ period: "week", limit: 5 });
    return entries.map((e) => `${e.rank}. ${e.player} ${e.score} ms`);
  } catch (e) {
    if (e instanceof DataError && e.code === "E_UNAVAILABLE") return [];
    throw e;
  }
}
```

## See also

- [`DataClient`](/docs/client/data-client)
- [`ClientLeaderboard`](/docs/client/client-leaderboard)
- [Player data, collections and leaderboards](/docs/room/guides/player-data#from-a-client), for what a client may read and write
- [Games without a backend](/docs/guides/no-backend#guests), for how guests work
