Manage chat rooms

Updated

Shows how to manage chat rooms by calling Agora Chat RESTful APIs.

This page shows how to manage chat rooms by calling Chat RESTful APIs, including adding, deleting, modifying, and retrieving chat rooms.

Before calling the following methods, ensure that you understand the frequency limit of calling Chat RESTful API calls described in Limitations.

Common parameters

The following table lists common request and response parameters of the Chat RESTful APIs:

Request parameters

ParameterTypeDescriptionRequired
hostStringThe domain name assigned by the Chat service to access RESTful APIs. For how to get the domain name, see Get the information of your project.Yes
app_idStringThe unique identifier automatically assigned to each project by AgoraYes
usernameString

The unique login account of the user. The user ID must be 64 characters or less and cannot be empty. The following character sets are supported:

  • 26 lowercase English letters (a-z)
  • 10 numbers (0-9)
  • "_", "-", "."

Do not use any of the 26 uppercase English letters (A-Z). Ensure that each username under the same App ID is unique.

Yes

Response parameters

ParameterTypeDescription
actionStringThe request method.
organizationStringThe unique identifier assigned to each company (organization) by the Chat service.
applicationStringA unique internal ID assigned to each app by the Chat service. You can safely ignore this parameter.
applicationNameStringThe unique identifier assigned to each app by the Chat service .
uriStringThe request URI.
pathStringThe request path, which is part of the request URL. You can safely ignore this parameter.
entities JSONThe response entity.
timestampNumberThe Unix timestamp (ms) of the HTTP response.
durationNumberThe duration (ms) from when the HTTP request is sent to the time the response is received.

Authorization

Chat RESTful APIs require Bearer HTTP authentication. Every time an HTTP request is sent, the following Authorization field must be filled in the request header:

Authorization: Bearer ${YourAppToken}

In order to improve the security of the project, Agora uses a token (dynamic key) to authenticate users before they log in to the chat system. Chat RESTful APIs only support authenticating users using app tokens. For details, see Authentication using App Token.

Creating a chat room

Creates a chat room.

HTTP request

POST https://{host}/app-id/{app_id}/chatrooms

Path parameter

For the parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
AcceptStringapplication/jsonYes
Content-TypeStringapplication/jsonYes
AuthorizationStringThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes

Request body

The request body is a JSON object, which contains the following fields:

FieldTypeDescriptionRequired
nameStringThe chat room name which can contain a maximum of 128 characters.Yes
descriptionStringThe chat room description which can contain a maximum of 512 characters.Yes
maxusersIntThe maximum number of members (including the chat room owner) that can join a chat room. The value range is [1,10,000], with 1000 as the default. To increase the upper limit, contact support@agora.io.No
ownerStringThe username of the chat room creator.Yes
membersJSONArrayThe array of user IDs of regular chat room members and administrators, excluding the chat room owner. If you specify this parameter, remember to pass in at least one user ID. The number of user IDs in the array cannot exceed the value of maxusers.No

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds, and the response body contains the following fields:

FieldTypeDescription
idStringThe chat room ID. This is the unique identifier assigned to each chat room by the Chat service.

For other fields and detailed descriptions, see Common parameters.

If the returned HTTP status code is not 200, the request fails. You can refer to Status codes for possible reasons.

Example

Request example

# Replace  with the app token you generated on the server
curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer ' -d '{
   "name": "testchatroom1",
   "description": "test",
   "maxusers": 300,
   "owner": "user1",
   "members": [
     "user2"
   ]
 }' 'http://XXXX/app-id/XXXX/chatrooms'

Response example

{
    "data": {
        "id": "66213271109633"
    }
}

Retrieving basic information of all chat rooms

Retrieves the basic information of all chat rooms under the app by page.

HTTP request

GET https://{host}/app-id/{app_id}/chatrooms?limit={N}&cursor={cursor}

Path parameter

For the parameters and detailed descriptions, see Common parameters .

Query parameters

parametertypedescribeIs it required?
limitNumberThe number of chat rooms expected to be fetched each time. The value range is [1,100], the default is 10, This parameter is only required when fetching pages.No
cursorStringThe starting position for data query. This parameter is required only for paginated queries. For the first query, you do not need to set cursor and the server returns chat rooms of the number specified with limit in the descending order of their creation time. You can get the cursor from the response body and pass it in the URL of the next query request. If there is no longer a cursor field in the response body, all chat rooms in the app are retrieved.No

If neither limit nor cursor is set, the server returns the first page of the chat room list with 10 chat rooms.

Request header

ParameterTypeDescriptionRequired
AcceptStringapplication/jsonYes
AuthorizationStringThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds. The response body contains the following fields:

FieldTypeDescription
idStringThe chat room ID. This is the unique identifier assigned to the chat room by the Chat.
nameStringThe chat room name.
ownerStringThe username of the chat room creator.
affiliations_countNumberThe number of members (including the chat room creator) in the chat room.

For other fields and detailed descriptions, see Common parameters.

If the returned HTTP status code is not 200, the request fails. You can refer to Status codes for possible reasons.

Example

Request example

# Replace  with the app token you generated on the server
curl --location --request GET 'http://XXXX/app-id/XXXX/chatrooms?limit=10' \
--header 'Authorization: Bearer '

Retrieving chat rooms that a user joins

Retrieves all the chat rooms that a user joins.

HTTP request

GET https://{host}/app-id/{app_id}/users/{username}/joined_chatrooms?pagenum={N}&pagesize={N}

Path parameter

For the parameters and detailed descriptions, see Common parameters .

Query parameter

ParameterTypeDescriptionRequired
pagenumNumberThe page number on which chat rooms are to be retrieved.No
pagesizeNumberThe number of chat rooms to be retrieved each time. The value range is [1,1000], with 1000 as the default.No

If neither query parameter is specified, the server returns the 500 chat rooms that the user joined most recently.

Request header

ParameterTypeDescriptionRequired
AcceptStringapplication/jsonYes
AuthorizationStringThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds. The response body contains the following fields:

FieldTypeDescriptions
idStringThe ID of the chat room that the user joins. This is the unique identifier assigned to each chat room by the Chat.
nameStringThe name of the chat room that the user joins.

For other fields and detailed descriptions, see Common parameters.

If the returned HTTP status code is not 200, the request fails. You can refer to Status codes for possible reasons.

Example

Request example

# Replace  with the app token generated in your server.
curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer ' 'http://XXXX/app-id/XXXX/users/XXXX/joined_chatrooms'

Response example

{
    "data": {
        "id": "66211860774913",
        "name": "test"
    }
}

Retrieving detailed information of the specified chat rooms

Retrieves the detailed information of one or more specified chat rooms.

HTTP request

GET https://{host}/app-id/{app_id}/chatrooms/{chatroom_id}

Path parameter

ParameterTypeDescriptionRequired
chatroom_idStringThe chat room ID. The unique identifier assigned to each chat room by the Chat service. You can get the chat room ID from the response body of Retrieve basic information of all chat rooms.When retrieving multiple chat rooms, type multiple chatroom IDs (chatroom_id) separated with the comma (,). A maximum of 100 chat rooms can be retrieved at one go.In the URL, "," needs to be escaped as "%2C".Yes

For other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
AcceptStringapplication/jsonYes
AuthorizationStringThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds. The response body contains the following fields:

FieldTypeDescription
idStringThe chat room ID.
nameStringThe chat room name.
descriptionStringThe chat room description.
membersonlyBoolWhether a user requesting to join the chat room requires approval from the chat room administrator. true: Yes. false: No
allowinvitesBoolWhether to allow a chat room member to invite others to join the chat room. true: A chat room member can invite others to join the chat room. false: Only the chat room administrator can invite others to join the chat room.
maxusersIntThe maximum number of members that can join the chat room.
ownerStringThe username of the chat room creator.
createdNumberThe Unix timestamp (ms) when the chat room is created.
customStringCustom information added during creation of the chat room.
affiliations_countNumberThe number of members (including the chat room creator) in the chat room.
affiliationsJSONArrayThe chat room member array, which contains the following fields: owner: The username of the chat room creator. member: The username of each chat room member.
publicBoolIt is a reserved parameter. You can safely ignore this parameter.

For other fields and detailed descriptions, see Common parameters.

If the returned HTTP status code is not 200, the request fails. You can refer to Status codes for possible reasons.

Example

Request example

# Replace  with the app token generated in your server.
curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer ' 'http://XXXX/app-id/XXXX/chatrooms/XXXX%2CXXXX'

Response example

{
    "action": "get",
    "application": "22bcffa0-XXXX-XXXX-9df8-516f6df68c6d",
    "applicationName": "XXXX",
    "count": 2
    "data": [
        {
            "id": "XXXX",
            "name": "testchatroom1",
            "description": "test",
            "membersonly": false,
            "allowinvites": false,
            "maxusers": 1000,
            "created": 1641365888209,
            "custom": "",
            "affiliations_count": 2,
            "affiliations": [
                {
                    "member": "user1"
                },
                {
                    "owner": "user2"
                }
            ],
            "public": true
        },
        {
            "id": "XXXX",
            "name": "testchatroom2",
            "description": "test",
            "membersonly": false,
            "allowinvites": false,
            "invite_need_confirm": true,
            "maxusers": 10000,
            "created": 1641289021898,
            "custom": "",
            "mute": false,
            "affiliations_count": 1,
            "affiliations": [
                {
                    "owner": "user3"
                }
            ],
            "public": true
        }
    ],
    "duration": 0,
    "entities": [],
    "organization": "XXXX",
    "timestamp": 1642064417048,
    "uri": "http://XXXX/app-id/XXXX/chatrooms/XXXX%2CXXXX"
}

Modifying chat room information

Modifies the information of the specified chat room. You can only modify the name, description, and maxusers of a chat room.

HTTP request

PUT https://{host}/app-id/{app_id}/chatrooms/{chatroom_id}

Path parameter

ParameterTypeDescriptionRequired
chatroom_idStringThe chat room ID. The unique identifier assigned to each chat room by the Chat service. You can get the chat room ID from the response body of Retrieve basic information of all chat rooms.Yes

For other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
Content-TypeStringapplication/jsonYes
AcceptStringapplication/jsonYes
AuthorizationStringThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes

Request body

The request body is a JSON object which only contains the following fields:

FieldTypeDescriptionRequired
nameStringThe chat room name.No
descriptionStringThe chat room description.No
maxusersNumberThe maximum number of chat room members (including the chat room creator).No

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds and the response body contains the following fields:

FieldTypeDescription
groupnameBoolWhether the chat room name is successfully changed. true: Success. false: Failure.
descriptionBoolWhether the chat room description is successfully modified. true: Success. false: Failure.
maxusersBoolWhether the maximum number of members that can join the chat room is successfully changed. true: Success. false: Failure.

If the returned HTTP status code is not 200, the request failed. You can refer to Status codes for possible reasons.

If other fields than name, description, and maxusers are passed in the request body, the request fails and the error code 400 is returned.

Example

Request example

curl -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer ' -d '{
   "name": "testchatroom",
   "description": "test",
   "maxusers": 300,
 }' 'http://XXXX/app-id/XXXX/chatrooms/XXXX'

Response example

{
    "data": {
        "description": true,
        "maxusers": true,
        "groupname": true
    }
}

Deleting the specified chat room

Deletes the specified chat room. If the specified chat room does not exist, an error returns.

HTTP request

DELETE https://{host}/app-id/{app_id}/chatrooms/{chatroom_id}

Path parameter

ParameterTypeDescriptionRequired
chatroom_idStringThe chat room ID. The unique identifier assigned to each chat room by the Chat service. You can get the chat room ID from the response body of Retrieve basic information of all chat rooms.Yes

For other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
AcceptStringapplication/jsonYes
AuthorizationStringThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds and the response body contains the following fields:

FieldTypeDescription
successBoolWhether the chat room is successfully deleted. true: Success. false: Failure.
idStringThe ID of the chat room that is deleted.

For other fields and detailed descriptions, see Common parameters.

If the returned HTTP status code is not 200, the request fails. You can refer to Status codes for possible reasons.

Example

Request example

# Replace  with the app token generated in your server.
curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer ' 'http://XXXX/app-id/XXXX/chatrooms/XXXX'

Response example

{
    "action": "delete",
    "application": "8be024f0-XXXX-XXXX-b697-5d598d5f8402",
    "uri": "http://XXXX/app-id/XXXX/chatrooms/XXXX",
    "entities": [],
    "data": {
        "success": true,
        "id": "66211860774913"
    },
    "timestamp": 1542545100474,
    "duration": 0,
    "organization": "XXXX",
    "applicationName": "XXXX"
}

Retrieving a chat room announcement

Retrieves the announcement text for the specified chat room.

HTTP request

GET https://{host}/app-id/{app_id}/chatrooms/{chatroom_id}/announcement

Path parameter

ParameterTypeRequiredDescription
chatroom_idStringYesThe chat room ID. The unique identifier assigned to each chat room by the Chat service. You can get the chat room ID from the response body of Retrieve basic information of all chat rooms.

For other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeRequiredDescription
Content-TypeStringYesSet to application/json.
AcceptStringYesapplication/json
AuthorizationStringYesThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.

HTTP response

The response body contains the following fields:

ParameterTypeDescription
data.announcementStringThe announcement text of the specified chat room.

Example

Request example

curl -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer  ' 'http://XXXX/app-id/XXXX/chatrooms/XXXX/announcement'

Response example

{
  "action": "get",
  "application": "52XXXXf0",
  "uri": "http://XXXX/app-id/XXXX/chatrooms/12XXXX11/announcement",
  "entities": [],
  "data": {
    "announcement" : " announcement text"
  },
  "timestamp": 1542363546590,
  "duration": 0,
  "organization": "XXXX",
  "applicationName": "testapp"
}

Modifying the announcement of a chat room

Modifies the announcement text of the specified chat room. The length cannot exceed 512 characters.

HTTP request

POST https://{host}/app-id/{app_id}/chatrooms/{chatroom_id}/announcement

Path parameter

ParameterTypeDescriptionRequired
chatroom_idStringThe chat room ID. The unique identifier assigned to each chat room by the Chat service. You can get the chat room ID from the response body of Retrieve basic information of all chat rooms.Yes

For other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeRequiredDescription
Content-TypeStringYesSet to application/json.
AuthorizationStringYesThe authentication token of the user or administrator, in the format of Bearer ${token}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.

Request body

ParameterTypeRequiredDescription
announcementStringYesThe modified announcement text.

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds and the response body contains the following fields:

ParameterTypeDescription
data.idStringThe chat room ID.
data.resultBooleanWhether the chat room announcement is successfully modified: - true: Success - false: Failure.

For other fields and detailed descriptions, see Common parameters.

If the returned HTTP status code is not 200, the request fails. You can refer to Status codes for possible reasons.

Example

Request example

curl -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer  ' 'http://XXXX/app-id/XXXX/chatrooms/12XXXX11/announcement' -d '{"announcement" : "chat room announcement"}'

Response example

{
  "action": "post",
  "application": "52XXXXf0",
  "uri": "http://XXXX/app-id/XXXX/chatrooms/12XXXX11/announcement",
  "entities": [],
  "data": {
    "id": "12XXXX11",
    "result": true
  },
  "timestamp": 1594808604236,
  "duration": 0,
  "organization": "XXXX",
  "applicationName": "testapp"
}

Status codes

For details, see HTTP Status Codes.