# createRoom() function

> The createRoom() function creates a room from the client and resolves with its ID, URL and join code.

The **`createRoom()`** function creates a room from the client, for games without a backend, and resolves with the room's ID, URL and join code. Pass the URL to [`connect()`](/docs/client/connect).

## Syntax

```ts
createRoom()
createRoom(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. Guests leave it out.

  `signal`
  : An `AbortSignal` that aborts the request.

  `id`
  : A string: the room's ID. Defaults to a random 12-character ID, which is hard to guess.

  `type`
  : A string: the [room type](/docs/guides/concepts#rooms) to create. The type's own `maxPlayers` and size apply. Defaults to the type named `default`.

  `public`
  : A boolean. If `true`, the room is on quick join and the public room list while it's active. Defaults to `false`.

  `joinCode`
  : A boolean. If `true`, the room gets a 6-character join code. Defaults to `false`.

  `meta`
  : Any JSON value, at most 1 KB: the room's meta, which [`listPublic()`](/docs/client/listpublic) returns, [`quickJoin()`](/docs/client/quickjoin) matches on and room code reads as [`room.meta`](/docs/room/room/meta). It's fixed for the room's life.

  `maxPlayers`
  : A number from 1 to the room size's connection limit (64 for `standard`) that caps the room's connections. A connection past it closes with 4003. Defaults to the limit.

### Return value

A `Promise` that resolves with an object with these properties:

`id`
: The room's ID.

`url`
: The room's URL, absolute, to pass to `connect()`.

`joinCode`
: The room's join code, or `null` if it has none.

### Exceptions

`TypeError`
: Rejects with this if there's no project URL, no `project` given and no `irt-project` meta tag; if the project URL's path doesn't end in a project; or if it isn't an `http:`, `https:`, `ws:` or `wss:` URL.

[`IrtError`](/docs/client/irt-error)
: Rejects with this if the router refuses the request, with the HTTP status as its `code`: 400 for an invalid room ID, `meta` over 1 KB, a `maxPlayers` out of range or no `type` when the build has no type named `default`; 401 for an invalid token; 403, a `NotAllowedError`, when the project's `players.createRooms` is off; 409 for an `id` a room already has; 429, a `QuotaExceededError`, when the address is rate limited or the project has `limits.maxRooms` rooms; and 503 when no join code was free. If the request didn't complete, its `code` is 0 and its `name` is `TimeoutError` when `signal` came from `AbortSignal.timeout()` and aborted, `AbortError` when another `signal` aborted, with its reason as `cause`, and `NetworkError` otherwise, with the failed `fetch()`'s error as `cause`.

## Description

`createRoom()` needs the project's `players.createRooms` setting on. With it on, connecting to a room that doesn't exist also creates it, as a private room with no join code, so `createRoom()` is for a room with a join code, a public room, `meta` or `maxPlayers`.

Anyone who knows a room's ID can connect to it, so share a private room's join code instead, or a link to the game's page with it. Join codes are 6 characters of `A-H`, `J-N`, `P-Z` and `2-9`, and last as long as their room.

## Examples

### Hosting a duel with a join code

The host creates a room for two players with a join code to tell a friend, and backs off when the project is creating rooms too fast.

```ts
// host-duel.ts
import { connect, createRoom, IrtError } from "@irtio/client";

export async function hostDuel() {
  try {
    const { url, joinCode } = await createRoom({ joinCode: true, maxPlayers: 2 });
    const room = await connect(url);
    console.log(`tell your friend the code ${joinCode}`);
    return room;
  } catch (e) {
    if (e instanceof IrtError && e.code === 429) {
      console.warn("too many new rooms; try again in a minute");
      return undefined;
    }
    throw e;
  }
}
```

### Opening a public match of another type

The room is created of the `match` type and listed publicly with its map, so the client connects with the same `type`.

```ts
// open-match.ts
import { connect, createRoom } from "@irtio/client";

export async function openMatch(map: string) {
  const { url } = await createRoom({ type: "match", public: true, meta: { map } });
  return connect(url, { type: "match" });
}
```

## See also

- [`joinByCode()`](/docs/client/joinbycode)
- [`quickJoin()`](/docs/client/quickjoin)
- [`Room.setPublic()`](/docs/room/room/setpublic), for the room to take itself off the list
- [Games without a backend](/docs/guides/no-backend#creating-and-joining-rooms)
- [Errors and close codes](/docs/client/errors#room-wide-requests)
