> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rxscale.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat

> Let a patient into a secure chat conversation from your own app

# Chat

Secure chat lets a doctor and a patient exchange messages. When the doctor starts a conversation, RxScale invites the patient by email. If your own app should bring the patient into the chat instead, create a single-use token for the conversation and hand it to the patient's app. The RxScale patient SDK redeems the token and opens the chat.

> [!NOTE]
>
> These endpoints are available only to organisations with the secure chat feature enabled. Without the feature, or without the required permission, the API answers `403`; the two cases have different bodies, see [Errors](#chat-errors). See [Permissions](https://docs.rxscale.com/permissions.md).

To learn about new conversations and new messages as they happen, subscribe to the [`chat_conversation_started`](https://docs.rxscale.com/webhooks/events#chat-conversation-started) and [`chat_message_created`](https://docs.rxscale.com/webhooks/events#chat-message-created) webhooks.

## Create a Patient Token

Create a single-use token that lets the patient of a conversation into the chat.

```bash theme={null}
POST /v1/management/chat/conversations/{conversation_uid}/patient-token
```

**Required permission:** `chat:patient_token`

**conversation\_uid** `string` *required*

The chat conversation UID, as sent in the `chat_conversation_started` webhook

The request has no body.

### Example Request

```bash theme={null}
curl -X POST "https://api.rxscale.com/v1/management/chat/conversations/conv-abc123/patient-token" \
  -H "X-API-Key: your-api-key-here"
```

### Response (201 Created)

```json theme={null}
{
  "invite_token": "eyJhbGciOi...",
  "expires_at": 1894060800,
  "conversation_uid": "conv-abc123"
}
```

| Field | Type | Description |
| - | - | - |
| `invite_token` | string | The token to pass to the patient's app. An opaque string: do not parse it or rely on its format. Today it is a signed token of roughly 500 characters and it may grow, so store it in a field that holds at least 2048 characters |
| `expires_at` | integer | Unix timestamp (seconds) after which the token no longer works. A token is valid for 7 days |
| `conversation_uid` | string | The conversation the token opens |

> [!WARNING]
>
> Treat the token like a password: anyone who has it can open the chat as the patient. It is single-use. Send it only to the patient's own app, over a secure connection, and never log it.

Every call creates a new token. Earlier tokens stay valid until they are used or expire, so create a token when the patient is about to open the chat rather than in advance.

### Errors

| Status | When |
| - | - |
| `403` | Your organisation does not have the secure chat feature, or the API key lacks the `chat:patient_token` permission. The body tells them apart, see below |
| `404` | The conversation does not exist, belongs to another organisation, is closed, or the patient has left it |

A `403` for a missing chat feature has the body:

```json theme={null}
{
  "error": "Feature not available in your plan",
  "feature": "chat"
}
```

A `403` for a missing permission has the body below. Branch on `code`; the `description` names the missing permission and its wording may change:

```json theme={null}
{
  "code": "permission_denied",
  "description": "API key lacks required permission: <permission>"
}
```

A `404` has the body:

```json theme={null}
{
  "error": "Conversation not found"
}
```

A conversation of another organisation is reported exactly like an unknown UID.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.