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

# Chat REST API

The Chat REST API provides HTTP access to Ably Chat functionality for server-side operations. Use this API to send messages, manage rooms, and perform administrative tasks without maintaining a persistent connection.

## Overview

The Chat REST API enables you to:

* Send messages to chat rooms
* Query message history
* Manage room metadata
* Retrieve room occupancy
* Perform moderation actions

## Base URL

All Chat REST API requests use:

<Code>
  ```text theme={null}
  https://rest.ably.io
  ```
</Code>

## Authentication

The Chat REST API uses the same authentication as the standard [REST API](/docs/api/rest-api):

### Basic Authentication

<Code>
  ```shell theme={null}
  curl https://rest.ably.io/chat/rooms/my-room/messages \
    -u "{{API_KEY}}"
  ```
</Code>

### Token Authentication

<Code>
  ```shell theme={null}
  curl https://rest.ably.io/chat/rooms/my-room/messages \
    -H "Authorization: Bearer {{TOKEN}}"
  ```
</Code>

## Rooms

Rooms are the primary organizational unit in Chat.

### Get Room

Retrieve room metadata:

<Code>
  ```shell theme={null}
  curl https://rest.ably.io/chat/rooms/my-room \
    -u "{{API_KEY}}"
  ```
</Code>

<ResponseField name="roomId" type="string">
  Unique identifier for the room.
</ResponseField>

<ResponseField name="name" type="string">
  Room display name.
</ResponseField>

<ResponseField name="metadata" type="object">
  Custom room metadata.
</ResponseField>

### List Rooms

List all active rooms:

<Code>
  ```shell theme={null}
  curl https://rest.ably.io/chat/rooms \
    -u "{{API_KEY}}"
  ```
</Code>

## Messages

Send and retrieve chat messages.

### Send Message

Send a message to a room:

<Code>
  ```shell theme={null}
  curl -X POST https://rest.ably.io/chat/rooms/my-room/messages \
    -u "{{API_KEY}}" \
    -H "Content-Type: application/json" \
    -d '{
      "text": "Hello, world!",
      "metadata": {
        "userId": "user123"
      }
    }'
  ```
</Code>

<ParamField body="text" type="string" required>
  The message text content.
</ParamField>

<ParamField body="metadata" type="object">
  Custom message metadata.
</ParamField>

<ParamField body="headers" type="object">
  Message headers for filtering and routing.
</ParamField>

### Get Message History

Retrieve message history for a room:

<Code>
  ```shell theme={null}
  curl "https://rest.ably.io/chat/rooms/my-room/messages?limit=50&direction=backwards" \
    -u "{{API_KEY}}"
  ```
</Code>

<ParamField query="start" type="integer">
  Start time in milliseconds since epoch.
</ParamField>

<ParamField query="end" type="integer">
  End time in milliseconds since epoch.
</ParamField>

<ParamField query="limit" type="integer" default="100">
  Maximum number of messages to return (max 1000).
</ParamField>

<ParamField query="direction" type="string" default="backwards">
  Query direction: `forwards` or `backwards`.
</ParamField>

## Occupancy

Query room occupancy information.

### Get Occupancy

Retrieve current room occupancy:

<Code>
  ```shell theme={null}
  curl https://rest.ably.io/chat/rooms/my-room/occupancy \
    -u "{{API_KEY}}"
  ```
</Code>

<ResponseField name="connections" type="integer">
  Number of active connections to the room.
</ResponseField>

<ResponseField name="presenceMembers" type="integer">
  Number of members present in the room.
</ResponseField>

## Typing Indicators

Manage typing indicators (typically used by server-side bots).

### Send Typing Indicator

<Code>
  ```shell theme={null}
  curl -X POST https://rest.ably.io/chat/rooms/my-room/typing \
    -u "{{API_KEY}}" \
    -H "Content-Type: application/json" \
    -d '{
      "clientId": "bot-user",
      "isTyping": true
    }'
  ```
</Code>

## Reactions

Send reactions to messages.

### Send Reaction

Add a reaction to a message:

<Code>
  ```shell theme={null}
  curl -X POST https://rest.ably.io/chat/rooms/my-room/messages/MESSAGE_ID/reactions \
    -u "{{API_KEY}}" \
    -H "Content-Type: application/json" \
    -d '{
      "type": "like",
      "metadata": {
        "userId": "user123"
      }
    }'
  ```
</Code>

### Get Reactions

Retrieve reactions for a message:

<Code>
  ```shell theme={null}
  curl https://rest.ably.io/chat/rooms/my-room/messages/MESSAGE_ID/reactions \
    -u "{{API_KEY}}"
  ```
</Code>

## Moderation

Perform moderation actions on messages.

### Delete Message

Delete a message from a room:

<Code>
  ```shell theme={null}
  curl -X DELETE https://rest.ably.io/chat/rooms/my-room/messages/MESSAGE_ID \
    -u "{{API_KEY}}"
  ```
</Code>

### Update Message

Update a message in a room:

<Code>
  ```shell theme={null}
  curl -X PATCH https://rest.ably.io/chat/rooms/my-room/messages/MESSAGE_ID \
    -u "{{API_KEY}}" \
    -H "Content-Type: application/json" \
    -d '{
      "text": "Updated message text",
      "metadata": {
        "edited": true
      }
    }'
  ```
</Code>

## Error Handling

The Chat REST API uses standard HTTP status codes:

<Code>
  ```json theme={null}
  {
    "error": {
      "code": 40160,
      "message": "Room not found",
      "statusCode": 404
    }
  }
  ```
</Code>

Common errors:

* `400` - Invalid request parameters
* `401` - Authentication failed
* `403` - Insufficient permissions
* `404` - Room or message not found
* `429` - Rate limit exceeded

## Rate Limits

The Chat REST API shares rate limits with the standard REST API:

* Message sending: Up to 2,000 messages per second
* History queries: Up to 50 requests per second

See [rate limits](/docs/platform/pricing/limits) for details.

## Examples

### Send Chat Message

<Code>
  ```javascript theme={null}
  async function sendChatMessage(roomId, text, userId) {
    const response = await fetch(
      `https://rest.ably.io/chat/rooms/${roomId}/messages`,
      {
        method: 'POST',
        headers: {
          'Authorization': `Basic ${btoa(API_KEY)}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          text: text,
          metadata: { userId: userId }
        })
      }
    );
    
    return await response.json();
  }
  ```
</Code>

### Retrieve Recent Messages

<Code>
  ```javascript theme={null}
  async function getRecentMessages(roomId, limit = 50) {
    const response = await fetch(
      `https://rest.ably.io/chat/rooms/${roomId}/messages?limit=${limit}&direction=backwards`,
      {
        headers: {
          'Authorization': `Basic ${btoa(API_KEY)}`
        }
      }
    );
    
    return await response.json();
  }
  ```
</Code>

## Related Resources

* [Chat SDK Documentation](/docs/chat)
* [REST API Reference](/docs/api/rest-api)
* [Chat Rooms Guide](/docs/chat/rooms)
* [Chat Messages Guide](/docs/chat/rooms/messages)
