# DataClient

> The DataClient interface of the Client API is player data, collections and leaderboards for the player the client is, with no room connection.

The **`DataClient`** interface of the [Client API](/docs/client) is the player's data and the project's collections and leaderboards, for the player the client is, over the router's HTTP API with no room connection. [`dataClient()`](/docs/client/dataclient) returns one.

A connected [`RoomClient`](/docs/client/room-client) has the same calls, made with the connection's token or guest.

## Instance properties

[`DataClient.playerData`](/docs/client/data-client/playerdata)
: The player's data. Read only.

## Instance methods

[`DataClient.collection()`](/docs/client/data-client/collection)
: Returns one of the project's collections.

[`DataClient.leaderboard()`](/docs/client/data-client/leaderboard)
: Returns one of the project's leaderboards.

[`DataClient.player()`](/docs/client/data-client/player)
: Resolves with the player ID the calls are made as.

## Description

A client may do less than room code:

- It reads its own player data, and writes only the keys in the project's `data.player.clientWrite`.
- It gets and lists a collection's entries and reads a board with the collection's `clientRead: true`, or only the entries it owns with `clientRead: "own"`.
- With `clientWrite: "own"`, it creates entries, changes and deletes its own, and submits its own score. `owner` is always the client's player, and a create past the collection's `maxOwn` rejects with `E_QUOTA`.
- Its writes can't use `once`, which rejects with `E_INVALID`.
- It makes at most 10 calls a second per address and per player, and an address gets at most 30 guests a minute for each project; past that, calls reject with `E_LIMIT`.

Nothing is cached on the client: every call goes to the router, and `refresh()` has nothing to drop. Calls reject with a [`DataError`](/docs/room/data-error), as [`dataClient()`](/docs/client/dataclient#description) describes.

## Examples

### Saving settings before joining a room

The settings screen saves the player's volume as player data, and reports when the router can't be reached.

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

const data = dataClient();

export async function saveVolume(volume: number) {
  try {
    await data.playerData.set("settings", { volume });
    return true;
  } catch (e) {
    if (e instanceof DataError && e.code === "E_UNAVAILABLE") return false;
    throw e;
  }
}
```

`set()` needs `settings` in the project's `data.player.clientWrite`; any other key rejects with `E_FORBIDDEN`.

## See also

- [`dataClient()`](/docs/client/dataclient)
- [`ClientLeaderboard`](/docs/client/client-leaderboard)
- [Player data, collections and leaderboards](/docs/room/guides/player-data#from-a-client)
