# POST /v1/dm/open

URL: https://docs.folkways.io/docs/realtime-api/dm-open

Open a direct-message or group room for 2 to 50 users and invite each of them on their personal topic.

Your backend opens a room for a set of users. The Realtime API names the room, adds the members to it if it is
private, and posts an `invite` on each member's [personal topic](/docs/concepts/topics#personal-topics). JSON, not
frames; service tokens only.

## Request [#request]

```http
POST https://realtime.folkways.net/v1/dm/open
Authorization: Bearer <service token>
Content-Type: application/json

{ "members": ["user:1", "user:2"], "prefix": "dm", "data": { "startedBy": "user:1" } }
```

| Field | Default | Meaning |
| --- | --- | --- |
| `members` | required | 2 to 50 distinct identities, each one topic level |
| `prefix` | `"dm"` | Where the room goes: `{prefix}/{id}`. The room must match a topic rule |
| `data` | `null` | Any JSON, passed through in every invite |

The service token needs `publish` on the room (e.g. `dm/>`) **and** on every member's `$user/{identity}` (e.g.
`$user/>`). All rights are checked before anything is written.

## Response [#response]

```json title="200 OK"
{
  "topic": "dm/5f0c8e1d2a9b44c7e1f03a6d",
  "members": ["user:1", "user:2"],
  "invites": [
    { "identity": "user:1", "topic": "$user/user:1", "duplicate": false },
    { "identity": "user:2", "topic": "$user/user:2", "duplicate": false }
  ]
}
```

- **One room per set of members.** The ID is derived from the sorted, distinct members, so the same members always
  get the same room in this segment, and two simultaneous opens land in one room. The name isn't a secret: access
  comes from rules, tokens and member lists.
- **Invites** are `invite` stream events with `{ "type": "dm", "topic", "members", "data" }`. Opening the same room
  with the same `data` again within 10 minutes sends nobody a second invite (`duplicate: true`).
- **Members-only rooms.** If the room's rule is members-only, members not yet on its list are added with every op
  the rule allows before any invite goes out. Members already on it keep their entry, so reopening never undoes a
  block. Each member's token then only needs a ceiling such as `dm/>` with `subscribe` and `publish`. For a public
  rule, put the room in each member's token instead.
- **Partial failure.** Invites go out one member at a time. If one fails, send the same request again: invites
  already sent come back `duplicate: true` and only the rest are written.

The member's app receives the invite on `$user/{identity}` and subscribes to its `topic`.

## Errors [#errors]

| Status | `error` | When |
| --- | --- | --- |
| `401` | `AuthRequired`, `AuthInvalid`, `AuthExpired` | No token, or a bad one |
| `403` | `Forbidden` | Not a service token; no rule for the room; no `publish` on the room or a member's `$user` topic. Nobody is invited |
| `403` | `Suspended` | The segment is suspended |
| `400` | `InvalidArgument` | Fewer than 2 or more than 50 members, an identity that can't have a personal topic, unknown fields |
| `400` | `InvalidTopic` | `prefix` isn't a valid topic name |
| `429` | `QuotaExceeded` | A topic, personal-topic or monthly limit would be passed. Nobody is invited |
| `429` | `RateLimited` | The segment's backend request budget is used up. Wait `retry_after_ms`; nothing was written |
| `503` | `Unavailable`, `Draining` | Try again |

No CORS: browsers never hold service tokens.
