Skip to main content
Android
iOS
Web
Windows
Unity
Flutter
React Native

Message management

This page shows how to call Chat RESTful APIs to send different types of messages, upload and download files, and retrieve historical messages.

Before calling the following methods, make sure you understand the call frequency limit of the Chat RESTful APIs as 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
org_nameStringThe unique identifier assigned to each company (organization) by the Chat service. For how to get the org name, see Get the information of the Chat project.Yes
app_nameStringThe unique identifier assigned to each app by the Chat service. For how to get the app name, see Get the information of the Chat project.Yes
usernameStringThe 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)
  • 26 uppercase English letters (A-Z)
  • 10 numbers (0-9)
  • "_", "-", "."
  • The username is case insensitive, so Aa and aa are the same username.
  • Ensure that each username under the same app is unique.
  • Do not set this parameter as a UUID, email address, phone number, or other sensitive information.
Yes

Response parameters

ParameterTypeDescription
actionStringThe request method.
organizationStringThe unique identifier assigned to each company (organization) by the Chat service. This is the same as org_name.
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 . This is the same as app_name.
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:


_1
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.

Send a message

This group of methods enable you to send and receive peer-to-peer and group messages. Message types include text, image, voice, video, command, extension, file, and custom messages.

For each App Key, the call frequency limit of this method is 100 per second.

Follow the instructions below to implement sending messages:

  • For text, command, and custom messages: Call the send-message method, and pass in the message content in the request body.
  • For image, voice, video, and file messages:
    1. Call the upload-file method to upload images, voice messages, videos, or other types of files, and get the file file_uuid from the response body.
    2. Call the send-message method, and pass the file_uuid in the request body.
  • When calling the RESTful APIs to send a message, you can use the from field to specify the message sender.
  • The maximum data length of the request body and extension fields is 5 KB, or the error 413 will be returned.

Send a one-to-one message

This method sends a message to a peer user.

HTTP request


_1
POST https://{host}/{org_name}/{app_name}/messages/users

Path parameter

For the descriptions of the path parameter, see Common Parameters.

Request header
ParameterTypeDescriptionRequired
Content-TypeStringThe content type. Set it to application/json.Yes
AcceptStringThe content type. Set it to application/json.Yes
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 parameters:

ParameterTypeDescriptionRequired
fromStringThe username of the message sender. If you do not set this field, the Chat server takes the admin as the sender. If you set it as the empty string "", this request fails.No
toArrayAn array of the usernames of the message recipients. For each request, you can send a message to a maximum of 600 users. Within one minute, you can send messages to a maximum of 6,000 users.Yes
typeStringThe message type:
  • txt: Text message
  • img: Image message
  • audio: Voice message
  • video: Video message
  • file: File message
  • loc: Location message
  • cmd: Command message
  • custom: Custom message
Yes
bodyJSONThe message content. For different message types, this parameter contains different fields. For details, see Body of different message types.Yes
sync_deviceBoolWhether to synchronize the message to the message sender.
  • true: Yes.
  • false: No.
No
routetypeStringWhether the message is delivered only when the recipient(s) is/are online:
  • If this parameter is set to ROUTE_TYPE, the message is delivered only when the recipient(s) is/are online. In this case, the message is discarded if the recipient is offline.
  • If you do not set this parameter, the message is delivered whether the recipients are online or not. If the recipient(s) is/are offline, the message will not be delivered until they get online.
No
extJSONThe extension field of the message. It cannot be null.No
ext.em_ignore_notificationBoolWhether to send a silent message:
  • true: Yes;
  • (Default)false: No.
Sending silent messages means that when the user is offline, Agora Chat will not push message notifications to the user's device through a third-party message push service. Therefore, users will not receive push notifications for messages. When the user goes online again, all messages sent from the offline period will be received. Unlike the Do Not Disturb mode which is set by the recipient to prevent notifications during a certain period, sending silent messages is set by the sender.
No

Body of different message types

  • Text message

    ParameterTypeDescriptionRequired
    msgStringThe message content.Yes
  • Image message

    ParameterTypeDescriptionRequired
    filenameStringThe name of the image file. You are advised to pass in this parameter, or there is no image name displayed on the client that receives the message.No
    secretStringThe secret for accessing the image file. You can obtain the value of secret from the share-secret parameter in the response body of the upload method. If you set restrict-access as true in the request header of upload when uploading the image file, ensure that you set this parameter.No
    sizeJSONThe size of the image (in pixels). This parameter contains two fields:
    • height: The image height.
    • width: The image width.
    No
    urlStringThe URL address of the image file, in the format of https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}, in which file_uuid can be obtained from the response body of upload after you upload the file to the server.Yes
  • Voice message

    ParameterTypeDescriptionRequired
    filenameStringThe name of the audio file. You are advised to pass in this parameter, or there is no voice file name displayed on the client that receives the message.No
    secretStringThe secret for accessing the audio file. You can obtain the value of secret from the share-secret parameter in the response body of the upload method. If you set restrict-access as true in the request header of upload when uploading the audio file, ensure that you set this parameter.No
    lengthIntThe length of the audio file (in seconds).No
    urlStringThe URL address of the audio file, in the format of https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}, in which file_uuid can be obtained from the response body of upload after you upload the file to the server.Yes
  • Video message

    ParameterTypeDescriptionRequired
    filenameStringThe name of the video file. You are advised to pass in this parameter, or there is no video file name displayed on the client that receives the message.No
    thumbStringThe URL address of the video thumbnail, in the format of https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}, in which file_uuid can be obtained from the response body of upload after you upload the file to the server.No
    lengthIntThe length of the video file (in seconds).No
    secretStringThe secret for accessing the video file. You can obtain the value of secret from the share-secret parameter in the response body of the upload method. If you set restrict-access as true in the request header of upload when uploading the video file, ensure that you set this parameter.No
    file_lengthLongThe data length of the video file (in bytes).No
    thumb_secretStringThe secret for accessing the video thumbnail. You can obtain the value of thumb_secret from the share-secret parameter in the response body of the upload method. If you set restrict-access as true in the request header of upload when uploading the thumbnail, ensure that you set this parameter.No
    urlStringThe URL address of the video file, in the format of https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}, in which file_uuid can be obtained from the response body of upload after you upload the file to the server.Yes
  • File message

    ParameterTypeDescriptionRequired
    filenameStringThe name of the file. You are advised to pass in this parameter, or there is no file name displayed on the client that receives the message.Yes
    secretStringThe secret for accessing the file. You can obtain the value of secret from the share-secret parameter in the response body of the upload method. If you set restrict-access as true in the request header of upload when uploading file, ensure that you set this parameter.No
    urlStringThe URL address of the file, in the format of https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}, in which file_uuid can be obtained from the response body of upload after you upload the file to the server.Yes
  • Location message

    ParameterTypeDescriptionRequired
    latStringThe latitude of the location (in degrees).Yes
    lngStringThe longitude of the location (in degrees).Yes
    addrStringThe address of the location.Yes
  • CMD message

    ParameterTypeDescriptionRequired
    actionStringThe content of the command.Yes
  • Custom message

    ParameterTypeDescriptionRequired
    customEventStringThe event type customized by the user. The value of this parameter should be a regular expression, for example, [a-zA-Z0-9-_/\.]{1,32}.No
    customExtsJSONThe event attribute customized by the user. The data type is Map<String,String>. You can set a maximum of 16 elements.No

HTTP response

Response body

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

ParameterTypeDescription
dataJSONThe detailed content of the response. The value of this parameter includes a key-value pair where key represents the username of the message recipient and value the message ID. For example, if the returned data is "user2":"1029457500870543736", it means that user2 has sent a message with the ID of 1029457500870543736.

For the other parameters and descriptions, see Common parameters.

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

Example

Request example
  • Send a text message to the specified user without synchronizing the message with the sender


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'http://XXXX/XXXX/XXXX/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "txt","body": {"msg": "testmessages"},"ext": {"em_ignore_notification": true}}'

  • Send a text message to the online user while synchronizing the message with the sender


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'http://XXXX/XXXX/XXXX/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "txt","body": {"msg": "testmessages"},"ext": {"em_ignore_notification": true},"routetype":"ROUTE_ONLINE", "sync_device":true}' ```

  • Send an image message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "img","body": {"filename":"testimg.jpg","secret":"VfXXXXNb_","url":"https://XXXX/XXXX/XXXX/chatfiles/55f12940-XXXX-XXXX-8a5b-ff2336f03252","size":{"width":480,"height":720}}}'

  • Send a voice message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "audio","body": {"url": "https://XXXX/XXXX/XXXX/chatfiles/1dfc7f50-XXXX-XXXX-8a07-7d75b8fb3d42","filename": "testaudio.amr","length": 10,"secret": "HfXXXXCjM"}}'

  • Send a video message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "video","body": {"filename":"testvideo.avi","thumb" : "https://XXXX/XXXX/XXXX/chatfiles/67279b20-7f69-11e4-8eee-21d3334b3a97","length" : 0,"secret":"VfXXXXNb_","file_length" : 58103,"thumb_secret" : "ZyXXXX2I","url" : "https://XXXX/XXXX/XXXX/chatfiles/671dfe30-XXXX-XXXX-ba67-8fef0d502f46"}}' ```

  • Send a file message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "file","body": {"filename":"test.txt","secret":"1-g0XXXXua","url":"https://XXXX/XXXX/XXXX/chatfiles/d7eXXXX7444"}}'

  • Send a location message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/users" -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["user2"],"type": "loc","body":{"lat": "39.966","lng":"116.322","addr":"North America"}}'

  • Send a CMD message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/users" -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization:Bearer {YourToken}" -d '{"from": "user1","to": ["user2"],"type": "cmd","body":{"action":"action1"}}'

  • Send a custom message


    _16
    # Replace {YourToken} with the app token generated on your server
    _16
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/users" \
    _16
    -H 'Content-Type: application/json' \
    _16
    -H 'Accept: application/json' \
    _16
    -H "Authorization:Bearer <YourToken>" \
    _16
    -d '{
    _16
    "from": "user1",
    _16
    "to": ["user2"],
    _16
    "type": "custom",
    _16
    "body": {
    _16
    "customEvent": "custom_event",
    _16
    "customExts":{
    _16
    "ext_key1":"ext_value1"
    _16
    }
    _16
    }
    _16
    }'

Response example
  • Send a text message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send an image message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a voice message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a video message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a file message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a location message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a CMD message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a custom message


    _13
    {
    _13
    "path": "/messages/users",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/users",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "user2": "1029457500870543736"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

Send a group message

HTTP request


_1
POST https://{host}/{org_name}/{app_name}/messages/chatgroups

Path parameter

For the descriptions of the path parameter, see Common Parameters.

Request header
ParameterTypeDescriptionRequired
Content-TypeStringThe content type. Set it to application/json.Yes
AcceptStringThe content type. Set it to application/json.Yes
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 parameters:

ParameterTypeDescriptionRequired
toArrayAn array of the group IDs that receives the message. Within one second, you can send a maximum of 20 messages to a chat group, and for each request, you can send messages to a maximum of 3 chat groups.Yes
need_group_ackBooleanWhether read receipts are required after the message is sent:
  • true: Yes.
  • false: No.
No

The other parameters and descriptions are the same with those of Sending a one-to-one message method.

HTTP response

Response body

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

ParameterTypeDescription
dataJSONThe detailed content of the response. The value of this parameter includes a key-value pair where key represents the group ID that receives the message and value the message ID. For example, if the returned data is "184524748161025": "1029544257947437432", it means that a message with the ID of 1029544257947437432 is sent in chat group 184524748161025.

For the other parameters and descriptions, see Common parameters.

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

Example

Request example
  • Send a text message to all members in a chat group whether they are online or not


    _18
    # Replace {YourAppToken} with the app token generated on your server
    _18
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatgroups'
    _18
    -H 'Content-Type: application/json'
    _18
    -H 'Accept: application/json'
    _18
    -H 'Authorization: Bearer <YourAppToken>'
    _18
    -d '{
    _18
    "from": "user1",
    _18
    "to": ["184524748161025"],
    _18
    "type": "txt",
    _18
    "need_group_ack": false,
    _18
    "body": {
    _18
    "msg": "testmessages"
    _18
    },
    _18
    "ext": {
    _18
    "em_ignore_notification": true
    _18
    },
    _18
    "routetype":"ROUTE_ONLINE"
    _18
    }'

  • Send a text message to online users in a chat group by setting routetype to ROUTE_ONLINE.


    _18
    # Replace {YourToken} with the app token generated on your server
    _18
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatgroups'
    _18
    -H 'Content-Type: application/json'
    _18
    -H 'Accept: application/json'
    _18
    -H 'Authorization: Bearer <YourAppToken>'
    _18
    -d '{
    _18
    "from": "user1",
    _18
    "to": ["184524748161025"],
    _18
    "type": "txt",
    _18
    "need_group_ack": false,
    _18
    "body": {
    _18
    "msg": "testmessages"
    _18
    },
    _18
    "ext": {
    _18
    "em_ignore_notification": true
    _18
    },
    _18
    "routetype":"ROUTE_ONLINE"
    _18
    }'

  • Send an image message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatgroups' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["184524748161025"],"type": "img","body": {"filename":"testimg.jpg","secret":"VfXXXXNb_","url":"https://XXXX/XXXX/XXXX/chatfiles/55f12940-XXXX-XXXX-8a5b-ff2336f03252","size":{"width":480,"height":720}}}'

  • Send a voice message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatgroups' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["184524748161025"],"type": "audio","body": {"url": "https://XXXX/XXXX/XXXX/chatfiles/1dfc7f50-XXXX-XXXX-8a07-7d75b8fb3d42","filename": "testaudio.amr","length": 10,"secret": "HfXXXXCjM"}}'

  • Send a video message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatgroups' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["184524748161025"],"type": "video","body": {"filename" : "testvideo.avi","thumb" : "https://XXXX/XXXX/XXXX/chatfiles/67279b20-7f69-11e4-8eee-21d3334b3a97","length" : 0,"secret":"VfXXXXNb_","file_length" : 58103,"thumb_secret" : "ZyXXXX2I","url" : "https://XXXX/XXXX/XXXX/chatfiles/671dfe30-XXXX-XXXX-ba67-8fef0d502f46"}}'

  • Send a file message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatgroups' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["184524748161025"],"type": "file","body": {"filename":"test.txt","secret":"1-g0XXXXua","url":"https://XXXX/XXXX/XXXX/chatfiles/d7eXXXX7444"}}'

  • Send a location message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/chatgroups" -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["184524748161025"],"type": "loc","body":{"lat": "39.966","lng":"116.322","addr":"North America"}}'

  • Send a CMD message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/chatgroups" -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization:Bearer {YourToken}" -d '{"from": "user1","to": ["184524748161025"],"type": "cmd","body":{"action":"action1"}}'

  • Send a Custom message


    _16
    # Replace {YourToken} with the app token generated on your server
    _16
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/chatgroups" \
    _16
    -H 'Content-Type: application/json' \
    _16
    -H 'Accept: application/json' \
    _16
    -H "Authorization:Bearer <YourToken>" \
    _16
    -d '{
    _16
    "from": "user1",
    _16
    "to": ["184524748161025"],
    _16
    "type": "custom",
    _16
    "body": {
    _16
    "customEvent": "custom_event",
    _16
    "customExts":{
    _16
    "ext_key1":"ext_value1"
    _16
    }
    _16
    }
    _16
    }'

Response example
  • Send a text message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send an image message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a voice message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a video message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a file message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a location message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a CMD message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a custom message


    _13
    {
    _13
    "path": "/messages/chatgroups",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatgroups",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "184524748161025": "1029544257947437432"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

Send a chat room message

This RESTful API allows you to send a maximum of 100 messages to 10 chat rooms in total on each call. Assuming that you send two messages to 10 chat rooms, the server counts it as 20 messages.

You can set the priority of a certain message type or chat room member to high, normal, or low. In this case, when the server is overloaded, low-priority messages can be dropped to reserve resources for the high-priority ones. This ensures that high-priority messages are sent first.

Note that this feature can increase the delivery reliability of high-priority messages, but cannot guarantee the deliveries. Even high-priorities messages can be dropped when the server load goes too high.

HTTP request


_1
POST https://{host}/{org_name}/{app_name}/messages/chatrooms

Path parameter

For the descriptions of the path parameter, see Common Parameters.

Request header
ParameterTypeDescriptionRequired
Content-TypeStringThe content type. Set it to application/json.Yes
AcceptStringThe content type. Set it to application/json.Yes
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 parameters:

ParameterTypeDescriptionRequired
toArrayAn array of the chat room IDs that receives the message. Within one second, you can send messages to a maximum of 100 chat rooms, and for each request, you can send messages to a maximum of 10 chat rooms.Yes
chatroom_msg_levelStringThe chat room message priority:
  • high
  • (Default) normal
  • low
No

The other parameters and descriptions are the same with those of Sending a one-to-one message method.

HTTP response

Response body

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

ParameterTypeDescription
dataJSONThe detailed content of the response. The value of this parameter includes a key-value pair where key represents the chat room ID that receives the message and value the message ID. For example, if the returned data is "185145305923585": "1029545553039460728", it means that a message with the ID of 1029545553039460728 is sent in chat room 185145305923585.

For the other parameters and descriptions, see Common parameters.

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

Example

Request example
  • Send a text message to members in a chat room


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'http://XXXX/XXXX/XXXX/messages/chatrooms' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["185145305923585"],"type": "txt","body": {"msg": "testmessages"}}'

  • Send an image message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatrooms' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["185145305923585"],"type": "img","body": {"filename":"testimg.jpg","secret":"VfXXXXNb_","url":"https://XXXX/XXXX/XXXX/chatfiles/55f12940-XXXX-XXXX-8a5b-ff2336f03252","size":{"width":480,"height":720}}}'

  • Send a voice message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatrooms' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["185145305923585"],"type": "audio","body": {"url": "https://XXXX/XXXX/XXXX/chatfiles/1dfc7f50-XXXX-XXXX-8a07-7d75b8fb3d42","filename": "testaudio.amr","length": 10,"secret": "HfXXXXCjM"}}'

  • Send a video message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatrooms' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["185145305923585"],"type": "video","body": {"filename":"testvideo.avi","thumb" : "https://XXXX/XXXX/XXXX/chatfiles/67279b20-7f69-11e4-8eee-21d3334b3a97","length" : 0,"secret":"VfXXXXNb_","file_length" : 58103,"thumb_secret" : "ZyXXXX2I","url" : "https://XXXX/XXXX/XXXX/chatfiles/671dfe30-XXXX-XXXX-ba67-8fef0d502f46"}}'

  • Send a file message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i 'https://XXXX/XXXX/XXXX/messages/chatrooms' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["185145305923585"],"type": "file","body": {"filename":"test.txt","secret":"1-g0XXXXua","url":"https://XXXX/XXXX/XXXX/chatfiles/d7eXXXX7444"}}'

  • Send a location message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/chatrooms" -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' -d '{"from": "user1","to": ["185145305923585"],"type": "loc","body":{"lat": "39.966","lng":"116.322","addr":"North America"}}'

  • Send a CMD message


    _2
    # Replace {YourToken} with the app token generated on your server
    _2
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/chatrooms" -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization:Bearer {YourToken}" -d '{"from": "user1","to": ["185145305923585"],"type": "cmd","body":{"action":"action1"}}'

  • Send a custom message


    _16
    # Replace {YourToken} with the app token generated on your server
    _16
    curl -X POST -i "https://XXXX/XXXX/XXXX/messages/chatrooms" \
    _16
    -H 'Content-Type: application/json' \
    _16
    -H 'Accept: application/json' \
    _16
    -H "Authorization:Bearer <YourToken>" \
    _16
    -d '{
    _16
    "from": "user1",
    _16
    "to": ["185145305923585"],
    _16
    "type": "custom",
    _16
    "body": {
    _16
    "customEvent": "custom_event",
    _16
    "customExts":{
    _16
    "ext_key1":"ext_value1"
    _16
    }
    _16
    }
    _16
    }'

Response example
  • Send a text message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send an image message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a voice message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a video message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a file message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a location message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a CMD message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

  • Send a custom message


    _13
    {
    _13
    "path": "/messages/chatrooms",
    _13
    "uri": "https://XXXX/XXXX/XXXX/messages/chatrooms",
    _13
    "timestamp": 1657254052191,
    _13
    "organization": "XXXX",
    _13
    "application": "e82bcc5f-XXXX-XXXX-a7c1-92de917ea2b0",
    _13
    "action": "post",
    _13
    "data": {
    _13
    "185145305923585": "1029545553039460728"
    _13
    },
    _13
    "duration": 0,
    _13
    "applicationName": "XXXX"
    _13
    }

Upload a file

This method enables you to upload images, audios, videos, or other types of files. For images and videos that have thumbnails, note the following:

  • Images: After uploading an image, the Agora server automatically generates the thumbnail of the image.
  • Videos: The Agora server does not generate thumbnails for videos automatically. After uploading a video, you must recall this method to upload the thumbnail for the video yourself.
File sizeThumbnail size
≤10 MBThe size of the thumbnail remains the same as that of the original file.
10 MBA thumbnail is generated based on the specified thumbnail-height and thumbnail-width parameters.
If you leave thumbnail-height and thumbnail-width empty, the height and width of the thumbnail is 170 × 170 pixels by default.

Take note of the following considerations before calling this method:

  • You cannot upload a file larger than 10 MB.
  • You can restrict access to the uploaded file by requiring users to provide an access key before they can download the file. The format of the key is {{url}}?share-secret={{secret}}.

For each App Key, the call frequency limit of this method is 100 per second.

HTTP request


_1
POST https://{host}/{org_name}/{app_name}/chatfiles

Path parameter

For the parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
Content-TypeStringThe content type. Pass multipart/form-dataYes
AuthorizationStringThe authentication token of the user or admin, in the format of Bearer ${YourAppToken}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.Yes
restrict-accessBoolWhether to restrict access to this file.
  • true: Restrict access to the file. The user needs to provide a file access key (share-secret) to download the file. You can obtain the access key from the response body.
  • false: The access is not restricted. Users can download the file directly.
No

Request body

The request body is in the form-data format and contains the following fields:

FieldTypeDescriptionRequired
fileStringThe local path of the file to be uploaded.Yes
thumbnail-heightNumberThe height of the image thumbnail, in pixels. This parameter is valid only if the size of the uploaded image exceeds 10 KB. If you leave this parameter empty, the height is 170 pixels by default.No
thumbnail-widthNumberThe width of the image thumbnail, in pixels. This parameter is valid only if the size of the uploaded image exceeds 10 KB. If you leave this parameter empty, the width is 170 pixels by default.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
entities.uuidStringThe file ID, a unique ID assigned to the file by the Chat service.
You need to save this uuid yourself, and provide it when calling the Send-file-messages method.
entities.typeStringFile type: chatfile.
entities.share-secretStringThe file access key.
You need to save the share-secret yourself for use when downloading the file.

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


_2
# Replace {YourAppToken} with the app token generated on your server, and the path of file with the local full path where the file to be uploaded is located
_2
curl -X POST 'https://XXXX/XXXX/XXXX/chatfiles' -H 'Authorization: Bearer <YourAppToken>' -H 'Content-Type: multipart/form-data; boundary=---WebKitFormBoundary7MA4YWxkTrZu0gW' -H 'restrict-access: true' -F 'file="@/Users/test/9.2/agora/image/IMG_2953.JPG"'

Response example


_17
{
_17
"action": "post",
_17
"application": "8be024f0-XXXX-XXXX-b697-5d598d5f8402",
_17
"path": "/chatfiles",
_17
"uri": "https://XXXX/XXXX/XXXX/chatfiles",
_17
"entities": [
_17
{
_17
"uuid": "5fd74830-XXXX-XXXX-822a-81ea50bb049d",
_17
"type": "chatfile",
_17
"share-secret": "X9dXXXX7Yc"
_17
}
_17
],
_17
"timestamp": 1554371126338,
_17
"duration": 0,
_17
"organization": "XXXX",
_17
"applicationName": "XXXX"
_17
}

Download a file

This method downloads images, audio, video, or other types of files.

For each App Key, the call frequency limit of this method is 100 per second.

HTTP request


_1
GET https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}

Path parameter

ParameterTypeRequiredDescription
file_uuidStringYesThe UUID of the file to be downloaded.

For the other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
AcceptStringThe content type. Set it toapplication/octet-stream, which means to download files in binary data stream format.Yes
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
share-secretStringThe file access key for downloading the file. After the file is uploaded successfully using the Upload the file method, you can obtain the access key from the response body of upload.This field is mandatory if you set restrict-access to true when uploading the file.

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds. You can refer to Common parameters for the parameters and detailed description.

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

Example

Request example


_2
# Replace {YourToken} with the app token generated on your server, and the path of file with the local full path where the file to be downloaded is located
_2
curl -X GET -H 'Accept: application/octet-stream' -H 'Authorization: Bearer {YourToken}' -H 'share-secret: f0Vr-uyyEeiHpHmsu53XXXXXXXXZYgyLkdfsZ4xo2Z0cSBnB' 'http://XXXX/XXXX/XXXX/chatfiles/7f456bf0-XXXX-XXXX-b630-777db304f26c'-o /Users/test/chat/image/image.JPG

Response example


_3
{
_3
// The content of the voice/image file
_3
}

Download a thumbnail

When uploading an image or video file, the Chat server can create a thumbnail for the file. This method has an extra thumbnail field in the request header compared with downloading a file.

For each App Key, the call frequency limit of this method is 100 per second.

HTTP request


_1
GET https://{host}/{org_name}/{app_name}/chatfiles/{file_uuid}

Path parameter

ParameterTypeRequiredDescription
file_uuidStringYesThe UUID that the server generates for the thumbnail.

For the other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeDescriptionRequired
AcceptStringapplication/octet-stream, which means to download files in binary data stream format.Yes
AuthorizationStringBearer ${YourAppToken}Yes
thumbnailBoolWhether to download the thumbnail of the image or video file.
  • true: Yes.
  • false: (Default) No.
  • Download the original file instead.
No.

HTTP response

Response body

If the returned HTTP status code is 200, the request succeeds. You can refer to Common parameters for the parameters and detailed description.

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

Example

Request example


_2
# Replace {YourToken} with the app token generated on your server
_2
curl -X GET -H 'Accept: application/octet-stream' -H 'Authorization: Bearer {YourToken}' -H 'share-secret: f0Vr-uyyEeiHpHmsu53XXXXXXXXZYgyLkdfsZ4xo2Z0cSBnB' -H 'thumbnail: true' 'http://XXXX/XXXX/XXXX/chatfiles/7f456bf0-ecb2-11e8-b630-777db304f26c'

Response example


_3
{
_3
// The content of the thumbnail
_3
}

Retrieve historical messages

This method retrieves historical messages sent and received by the user.

  • For each request, you can retrieve all the historical messages sent and received within one hour from the specified time.
  • Messages cannot be retrieved in real time. For example, at 9 a.m., you can retrieve messages that are sent or received at 8 a.m.
  • The default storage time of historical messages differs by plan version. For details, see package details.

For each App Key, the call frequency limit of this method is 10 per minute.

HTTP request


_1
GET https://{host}/{org_name}/{app_name}/chatmessages/${time}

Path parameter

ParameterTypeDescriptionRequired
timeStringThe start time of the historical messages to query. UTC time, using the ISO8601 standard, in the format yyyyMMddHH.
For example, if time is 2018112717, it means to query historical messages from 17:00 on November 27, 2018 to 18:00 on November 27, 2018.
Yes

For other parameters and detailed descriptions, see Common parameters.

Request header

ParameterDescriptionRequired
AcceptStringThe content type. Set it to application/json.
AuthorizationThe authentication token of the user or admin, in the format of Bearer ${YourAppToken}, 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:

ParameterTypeDescription
urlStringThe download address of the historical message file. This parameter is valid within a limited time duration. The Expires field indicates when the returned URL is valid. Once the URL expires, you need to call this method to get the URL again.

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


_2
# Replace {YourToken} with the app token generated on your server.
_2
curl -X GET -H 'Accept: application/json' -H 'Authorization: Bearer {YourToken}' 'http://XXXX/XXXX/XXXX/chatmessages/2018112717'

Response example


_14
{
_14
"action": "get",
_14
"application": "8be024f0-XXXX-XXXX-b697-5d598d5f8402",
_14
"uri": "'http://XXXX/XXXX/XXXX/chatmessages/2018112717",
_14
"data": [
_14
{
_14
"url": "http://XXXX?Expires=1543316122&OSSAccessKeyId=XXXX&Signature=XXXX"
_14
}
_14
],
_14
"timestamp": 1543314322601,
_14
"duration": 0,
_14
"organization": "XXXX",
_14
"applicationName": "testapp"
_14
}

Content of historical messages

After successfully querying historical messages, you can visit the URL to download the historical message file and view the specific content of the historical message.

Historical messages contain the following parameters in JSON format:

ParameterTypeDescription
msg_idStringThe message ID.
timestampLongThe UTC Unix timestamp when the message is sent, in miliseconds.
fromStringThe username that sends the message.
toStringThe message recipient.
  • For a one-to-one chat, this parameter indicates the peer user that receives the message.
  • For a group chat, this parameter indicates the chat group ID.
chat_typeStringThe chat type:
  • chat: One-to-one chat.
  • groupchat: Group chat.
  • chatroom: Chat room.
payloadJSONThe content of the message, including message extensions and customzied message attributes.

_22
{
_22
"msg_id": "5I02W-XX-8278a",
_22
"timestamp": 1403099033211,
_22
"direction":"outgoing",
_22
"to": "XXXX",
_22
"from": "XXXX",
_22
"chat_type": "chat",
_22
"payload":
_22
{
_22
"bodies": [
_22
{
_22
// For different message types, the parameters differ
_22
}
_22
],
_22
"ext":
_22
{
_22
"key1": "value1", ...
_22
},
_22
"from":"XXXX",
_22
"to":"XXXX"
_22
}
_22
}

The fields of bodies for different message types vary:

  • Text messages

    FieldTypeDescription
    msgStringThe message content.
    typeStringThe message type. For text messages, set it as txt.

    Example:


    _8
    {
    _8
    "bodies": [
    _8
    {
    _8
    "msg": "welcome to Agora!",
    _8
    "type": "txt"
    _8
    }
    _8
    ]
    _8
    }

  • Image messages

    FieldTypeDescription
    file_lengthNumberThe size of the image attachment, in bytes.
    file_nameStringThe name of the image file.
    secretStringThe image file access key.
    This field exists if you set the access restriction when calling the upload-file method.
    sizeNumberThe size of the image, in pixels.
  • height: The image height
  • width: The image width
  • typeStringThe message type. For image messages, set it as img.
    urlStringThe URL address of the image.

    Example:


    _15
    {
    _15
    "bodies": [
    _15
    {
    _15
    "file_length": 128827,
    _15
    "filename": "test1.jpg",
    _15
    "secret": "DRGM8OZrEeO1vaXXXXXXXXHBeKlIhDp0GCnFu54xOF3M6KLr",
    _15
    "size": {
    _15
    "height": 1325,
    _15
    "width": 746
    _15
    },
    _15
    "type": "img",
    _15
    "url": "https://a1.agora.com/agora-demo/chatdemoui/chatfiles/65e54a4a-XXXX-XXXX-b821-ebde7b50cc4b"
    _15
    }
    _15
    ]
    _15
    }

  • Location messages

    FieldTypeDescription
    addrStringThe descriptions of the location.
    latNumberThe latitude of the location.
    lngNumberThe longitude of the location.
    typeStringThe message type. For location messages, set it as loc.

    Example:


    _10
    {
    _10
    "bodies": [
    _10
    {
    _10
    "addr": "test",
    _10
    "lat": 39.9053,
    _10
    "lng": 116.36302,
    _10
    "type": "loc"
    _10
    }
    _10
    ]
    _10
    }

  • Voice messages

    FieldTypeDescription
    file_lengthNumberThe size of the audio file, in bytes.
    filenameStringThe audio file name, including a suffix that indicates the the audio file format.
    secretStringThe audio file access key.
    This field exists if you set the access restriction when calling the upload-file method.
    lengthNumberThe duration of the audio file, in seconds.
    typeStringThe message type. For voice messages, set it as audio.
    urlStringThe URL address of the audio file.

    Example:


    _12
    {
    _12
    "bodies": [
    _12
    {
    _12
    "file_length": 6630,
    _12
    "filename": "test1.amr",
    _12
    "length": 10,
    _12
    "secret": "DRGM8OZrEeO1vafuJSo2IjHBeKlIhDp0GCnFu54xOF3M6KLr",
    _12
    "type": "audio",
    _12
    "url": "https://a1.agora.com/agora-demo/chatdemoui/chatfiles/0637e55a-XXXX-XXXX-ba23-51f25fd1215b"
    _12
    }
    _12
    ]
    _12
    }

  • Video messages

    FieldTypeDescription
    file_lengthNumberThe size of the video file, in bytes.
    filenameStringThe video file name, including a suffix that indicates the video file format.
    secretStringThe video file access key.
    This field exists if you set the access restriction when calling the upload-file method.
    lengthNumberThe video duration, in seconds.
    sizeNumberThe video thumbnail size, in pixels.
  • width: The width of the video thumbnail
  • height: The height of the video thumbnail
  • thumbStringThe URL address of the video thumbnail.
    thumb_secretStringThe thumbnail file access key.
    This field exists if you set the access restriction when calling the upload-file method.
    typeStringThe message type. For video messages, set it as video.
    urlStringThe URL address of the video file. You can visit this URL to download video files.

    Example:


    _18
    {
    _18
    "bodies": [
    _18
    {
    _18
    "file_length": 58103,
    _18
    "filename": "1418105136313.mp4",
    _18
    "length": 10,
    _18
    "secret": "VfEpSmSvEeS7yU8dwa9rAQc-DIL2HhmpujTNfSTsrDt6eNb_",
    _18
    "size": {
    _18
    "height": 480,
    _18
    "width": 360
    _18
    },
    _18
    "thumb": "https://a1.agora.com/agora-demo/chatdemoui/chatfiles/67279b20-XXXX-XXXX-8eee-21d3334b3a97",
    _18
    "thumb_secret": "ZyebKn9pEeSSfY03ROk7ND24zUf74s7HpPN1oMV-1JxN2O2I",
    _18
    "type": "video",
    _18
    "url": "https://a1.agora.com/agora-demo/chatdemoui/chatfiles/671dfe30-XXXX-XXXX-ba67-8fef0d502f46"
    _18
    }
    _18
    ]
    _18
    }

  • File messages

    FieldTypeDescriptions
    file_lengthNumberThe file size, in bytes.
    filenameStringThe file name, including a suffix that indicates the file format.
    secretStringThe file access key.
    This field exists if you set the access restriction when calling the upload-file method.
    typeStringThe message type. For file messages, set it as file.
    urlStringThe URL address of the file. You can visit this URL to download video files.

    Example:


    _11
    {
    _11
    "bodies": [
    _11
    {
    _11
    "file_length": 3279,
    _11
    "filename": "record.md",
    _11
    "secret": "2RNXCgeeEeeXXXX-XXXXbtZXJH4cgr2admVXn560He2PD3RX",
    _11
    "type": "file",
    _11
    "url": "https://XXXX/XXXX/XXXX/chatfiles/d9135700-XXXX-XXXX-b000-a7039876610f"
    _11
    }
    _11
    ]
    _11
    }

  • CMD messages

    FieldTypeDescription
    actionStringThe request method.
    typeStringThe message type. For command messages, set it as cmd.

    Example:


    _8
    {
    _8
    "bodies": [
    _8
    {
    _8
    "action": "run",
    _8
    "type": "cmd"
    _8
    }
    _8
    ]
    _8
    }

  • Custom messages

    FieldTypeDescription
    customExtsJSONThe custom extension properties. You can set the fields in the extension properties yourself.
    customEventStringThe custom event type.
    typeStringThe message type. For custom messages, set it as custom.

    Example:


    _13
    {
    _13
    "bodies": [
    _13
    {
    _13
    "customExts": {
    _13
    "name": "flower",
    _13
    "size": "16",
    _13
    "price": "100"
    _13
    },
    _13
    "customEvent": "gift_1",
    _13
    "type": "custom"
    _13
    }
    _13
    ]
    _13
    }

Recall a message

Once a message is sent, you can call this API to recall it. This API recalls a message that is saved both locally and on the server, whether it is a historical message, offline message or a roaming message on the server, or a message in the memory or local database of the message sender or recipient.

The default time limit for recalling a message is two minutes. You can extend this time frame to up to 7 days in Agora Console. To do so, select a project that enables Agora Chat, then click Configure > Features > Message recall.

message-recall

For each App Key, the call frequency limit of this method is 100 per second.


_1
POST https://{host}/{org_name}/{app_name}/messages/msg_recall

Path parameter

For the parameters and detailed descriptions, see Common parameters.

Request header

HTTP request

ParameterTypeRequiredDescription
AcceptStringThe content type. Set it to application/json.Yes
AuthorizationStringYesThe authentication token of the user or admin, in the format of Bearer ${YourAppToken}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.

Request body

ParameterTypeRequiredDescription
msg_idStringYesThe ID of the message to recall. As only one message can be recalled each time, you can pass in only one message ID.
toStringYesThe user, chat group, or chat room that receives the message to recall. You can specify a user ID, a chat group ID, or a chat room ID. Note: If the message to recall no longer exists on the server, only the message on the recipient client is recalled.
chat_typeStringYesThe type of the chat where the message to recall is sent.
  • chat: An one-on-one chat.
  • groupchat: A chat group.
  • chatroom: A chat room.
fromStringNoThe user who recalls the message. By default, the recaller is the app admin. You can also specify another user as the recaller.
forceboolNoWhether to allow to recall messages beyond the storage time on the server. For details on the message storage duration on the server, see Message storage duration.
  • true: Yes. In this case, you can recall messages within the recall period or those beyond the storage time on the server. For the latter, this API recalls the messages locally saved by the recipient, but the local message on the sender side still exists. If the message sending time is between your recall duration and the storage duration on the server, the recall fails. For example, if the recall duration is 2 minutes and the storage time on the server is 7 days, you can recall a message sent within 2 minutes or one that was sent more than 7 days ago; if the message is sent 3 minutes ago, the recall will fail.
  • false: No. You cannot recall messages beyond the storage time on the server. If you use the default recall time of 2 minutes or a custom duration, the server can only recall the messages sent within the specified time, and those beyond this time cannot be recalled. For example, if you set the recall time to 3 minutes and the message is sent 4 minutes ago, the recall will fail.

HTTP response

Response body

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

ParameterTypeDescription
msg_idStringThe ID of the recalled message.
recalledStringReturns yes if the request is successful.
fromStringThe user who recalls the message. By default, the recaller is the app admin.
toStringThe user, chat group, or chat room that receives the recalled message.
chattypeStringThe type of the chat where the recalled message is located.
  • chat: An one-on-one chat.
  • group_chat: A chat group.
  • chatroom: A chat room.

For other fields and detailed descriptions, see Common parameters.

If the request fails, refer to Status codes for possible reasons.

Example

Request example


_10
# Replace {YourToken} with the app token generated on your server
_10
curl -i -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer {YourToken}"
_10
"http://XXXX/XXXX/XXXX/messages/msg_recall"
_10
-d '{
_10
"msg_id": "1028442084794698104",
_10
"to": "user2",
_10
"from": "user1",
_10
"chat_type": "chat",
_10
"force": true
_10
}'

Response example

  • If the message is recalled:


    _17
    {
    _17
    "path": "/messages/msg_recall",
    _17
    "uri": "https://XXXX/XXXX/XXXX/messages/msg_recall",
    _17
    "timestamp": 1657529588473,
    _17
    "organization": "XXXX",
    _17
    "application": "09ebbf8b-XXXX-XXXX-XXXX-d47c3b38e434",
    _17
    "action": "post",
    _17
    "data": {
    _17
    "recalled": "yes",
    _17
    "chattype": "chat",
    _17
    "from": "XXXX",
    _17
    "to": "XXXX",
    _17
    "msg_id": "1028442084794698104"
    _17
    },
    _17
    "duration": 8,
    _17
    "applicationName": "XXXX"
    _17
    }

  • If the message fails to be recalled:


    _8
    {
    _8
    "msgs":
    _8
    [
    _8
    { "msg_id":"673296835082717140",
    _8
    "recalled":"not_found msg"
    _8
    }
    _8
    ]
    _8
    }

    Possible causes for failing to recall the message include the following:

    • "can't find message to": The recipient of the message to recall is not found.
    • "exceed call time limit": The time limit for recalling a message is exceeded.
    • "not_found msg": The message is already recalled or no longer exists because its storage period expires.
    • "internal error": An internal error occurs with the back-end service.

Delete conversations one way from the server

This method enables the chat user to delete conversations one way from the server. Once the conversation is deleted, this chat user can no longer retrieve the conversation from the server. Other chat users can still get the conversation from the server.

For each App Key, the call frequency limit of this method is 100 per second.

HTTP request


_1
DELETE https://{host}/{orgName}/{appName}/users/{userName}/user_channel

Path parameter

ParameterTypeDescriptionRequired
userNameStringThe username whose conversation is to be deleted.Yes

For the other parameters and detailed descriptions, see Common parameters.

Request header

ParameterTypeRequiredDescription
AuthorizationStringYesThe authentication token of the user or admin, in the format of Bearer ${YourAppToken}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.

Request body

ParameterTypeRequiredDescription
channelStringYesThe ID of the conversation that you want to delete.
typeStringYesThe type of the chat.
  • chat: A one-on-one chat.
  • groupchat: A group chat.
delete_roamBoolYesWhether to delete the chat from the server:
  • true: Yes.
  • false: No.

HTTP response

Response body

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

ParameterDescription
resultReturns ok if the request is successful.

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

Example

Request example


_7
curl -L -X DELETE 'https://XXXX/XXXX/XXXX/users/u1/user_channel'
_7
-H 'Authorization: Bearer <YourAppToken>' \
_7
-H 'Content-Type: application/json' \
_7
-H 'Accept: application/json' \
_7
-d '
_7
{ "channel": "u2", "type": "chat", "delete_roam": true }
_7
'

Response example


_14
{
_14
"path": "/users/user_channel",
_14
"uri": "https://a1.agora.com/agora-demo/test-app/users/u1/user_channel",
_14
"timestamp": 1638440544078,
_14
"organization": "agora-demo",
_14
"application": "c3624975-3d51-4b0a-9da2-ee91ed4c5a76",
_14
"entities": [],
_14
"action": "delete",
_14
"data": {
_14
"result": "ok"
_14
},
_14
"duration": 3,
_14
"applicationName": "test-app"
_14
}

Import a one-to-one chat message

This method imports a one-to-one message. You can import only one message each time.

HTTP request


_1
POST https://{host}/{orgName}/{appName}/messages/users/import

Request header

ParameterTypeRequiredDescription
AuthorizationStringYesThe authentication token of the user or admin, in the format of Bearer ${YourAppToken}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.

Request body

ParameterTypeDescriptionRequired
fromStringThe username of the message sender.Yes
targetStringThe username of the message recipient.Yes
typeStringThe message type:
  • txt: Text message
  • img: Image message
  • audio: Voice message
  • video: Video message
  • file: File message
  • loc: Location message
  • cmd: Command message
  • custom: Custom message
Yes
bodyJSONThe message content. For different message types, this parameter contains different fields. For details, see Body of different message types.Yes
is_ack_readBoolWhether to set the message as read.
  • true: Yes.
  • false: No.
No
msg_timestampLongThe timestamp for importing the messages, in milliseconds. If you leave this parameter empty, the server automatically sets it as the current time.No
need_downloadBoolWhether to download the attachment and upload it to the server:
  • true: Yes. In this case, you need to make sure that the attachment URL is publicly accessible.
  • false: (Default) No.
No

HTTP Response

Response body

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

ParameterDescription
msg_idThe ID of the imported messages.

For other fields and detailed descriptions, see Common parameters.

If the request fails, refer to Status codes for possible reasons.

Example

Request example

  • Import a text message


    _11
    # Replace {YourToken} with the app token generated on your server
    _11
    curl -X POST -H "Authorization: Bearer {YourToken}" "https://XXXX/XXXX/XXXX/messages/users/import" -d '{
    _11
    "target": "username2",
    _11
    "type": "txt",
    _11
    "body": {
    _11
    "msg": "import message."
    _11
    },
    _11
    "from": "username1",
    _11
    "is_ack_read": true,
    _11
    "msg_timestamp": 1656906628428
    _11
    }'

  • Import an image message


    _17
    # Replace {YourToken} with token generated on your server
    _17
    curl -X POST -H "Authorization: Bearer {YourToken}" "https://XXXX/XXXX/XXXX/messages/users/import" -d '{
    _17
    "target": "username2",
    _17
    "type": "img",
    _17
    "body": {
    _17
    "url": "<YourImageUrl>",
    _17
    "filename": "<ImageFileName>",
    _17
    "size": {
    _17
    "width": 1080,
    _17
    "height": 1920
    _17
    }
    _17
    },
    _17
    "from": "username1",
    _17
    "is_ack_read": true,
    _17
    "msg_timestamp": 1656906628428,
    _17
    "need_download": true
    _17
    }'

Response example


_14
{
_14
"path": "/messages/users/import",
_14
"uri": "https://XXXX/XXXX/XXXX/messages/users/import",
_14
"timestamp": 1638440544078,
_14
"organization": "XXXX",
_14
"application": "c3624975-XXXX-XXXX-9da2-ee91ed4c5a76",
_14
"entities": [],
_14
"action": "post",
_14
"data": {
_14
"msg_id": "10212123848595"
_14
},
_14
"duration": 3,
_14
"applicationName": "XXXX"
_14
}

Import a chat group message

This method imports a chat group message. You can import only one message each time.

HTTP request


_1
POST https://{host}/{orgName}/{appName}/messages/chatgroups/import

Request header

ParameterTypeRequiredDescription
AuthorizationStringYesThe authentication token of the user or admin, in the format of Bearer ${YourAppToken}, where Bearer is a fixed character, followed by an English space, and then the obtained token value.

Request body

ParameterTypeDescriptionRequired
fromStringThe username of the message sender.Yes
targetStringThe chat group ID that receives the message.Yes
typeStringThe message type:
  • txt: Text message
  • img: Image message
  • audio: Voice message
  • video: Video message
  • file: File message
  • loc: Location message
  • cmd: Command message
  • custom: Custom message
Yes
bodyJSONThe message content. For different message types, this parameter contains different fields. For details, see Body of different message types.Yes
is_ack_readBoolWhether to set the message as read.
  • true: Yes.
  • false: No.
No
msg_timestampLongThe timestamp for importing the messages, in milliseconds. If you leave this parameter empty, the server automatically sets it as the current time.No
need_downloadBoolWhether to download the attachment and upload it to the server:
  • true: Yes. In this case, you need to make sure that the attachment URL is publicly accessible.
  • false: (Default) No.
No

HTTP Response

Response body

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

ParameterDescription
msg_idThe ID of the imported messages.

For other fields and detailed descriptions, see Common parameters.

If the request fails, refer to Status codes for possible reasons.

Example

Request example

  • Import a text message


    _11
    # Replace {YourAppToken} with the app token generated on your server
    _11
    curl -X POST -H "Authorization: Bearer <YourAppToken> " "https://XXXX/XXXX/XXXX/messages/chatgroups/import" -d '{
    _11
    "target": "1123376564212",
    _11
    "type": "txt",
    _11
    "body": {
    _11
    "msg": "import message."
    _11
    },
    _11
    "from": "username1",
    _11
    "is_ack_read": true,
    _11
    "msg_timestamp": 1656906628428
    _11
    }'

  • Import an image message


    _17
    # Replace {YourAppToken} with the app token generated on your server
    _17
    curl -X POST -H "Authorization: Bearer {YourToken}" "https://XXXX/XXXX/XXXX/messages/chatgroups/import" -d '{
    _17
    "target": "1123376564212",
    _17
    "type": "img",
    _17
    "body": {
    _17
    "url": "<YourImageUrl>",
    _17
    "filename": "<ImageFileName>",
    _17
    "size": {
    _17
    "width": 1080,
    _17
    "height": 1920
    _17
    }
    _17
    },
    _17
    "from": "username1",
    _17
    "is_ack_read": true,
    _17
    "msg_timestamp": 1656906628428,
    _17
    "need_download": true
    _17
    }'

Response example


_14
{
_14
"path": "/messages/users/import",
_14
"uri": "https://XXXX/XXXX/XXXX/messages/chatgroups/import",
_14
"timestamp": 1638440544078,
_14
"organization": "XXXX",
_14
"application": "c3624975-XXXX-XXXX-9da2-ee91ed4c5a76",
_14
"entities": [],
_14
"action": "post",
_14
"data": {
_14
"msg_id": "10212123848595"
_14
},
_14
"duration": 3,
_14
"applicationName": "XXXX"
_14
}

Status codes

For details, see HTTP Status Codes.

vundefined