Users
All resource URLs include an {appId}
, which you can find on the Settings page of your dashboard.
{userId}
is your internal unique identifier for the user.
Before you start sending messages, you need to synchronize participants of the conversation.
Note: To update user details, you only need to send the subset of fields you want to update. In
that respect, even though you update a user with an HTTP PUT
request method, the user
update acts similar to an HTTP PATCH
request method.
1type RequestBody = {2 name: string,3 email?: Array<string> | null,4 welcomeMessage?: string | null,5 photoUrl?: string | null,6 locale?: string | null,7 role?: string | null,8 phone?: Array<string> | null,9 custom?: Map<string, string> | null,10 pushTokens?: Map<string, true | null> | null11}
1{2 "name": "Alice",4 "welcomeMessage": "Hi there, how are you? :-)",5 "photoUrl": "https://talkjs.com/images/avatar-1.jpg",6 "locale": "ar",7 "role": "buyer",8 "phone": ["+1123456789"],9 "custom": {10 "country": "ca"11 }12 "pushTokens": {13 "fcm:MY_TOKEN_ID": true14 }15}
pushTokens
is an object that has the keys in the format:
1'provider:token_id';
Where provider
is either fcm
for Android (Firebase Cloud Messaging), or apns
for iOS (Apple Push Notification Service).
the value for each key can be either true
for registering the device for push notifications, or null
for unregistering the device.
Setting pushTokens
to null
unregisters all the previously registered devices.
You can update multiple users with a single API call.
1type RequestBody = {2 [userId: string]: {3 name: string,4 email?: Array<string> | null,5 welcomeMessage?: string | null,6 photoUrl?: string | null,7 locale?: string | null,8 role?: string | null,9 phone?: Array<string> | null,10 custom?: Map<string, string> | null,11 pushTokens?: Map<string, true | null> | null12 }13}
1{2 "alice": {3 "name": "Alice",5 "phone": ["+1123456789"],6 "locale": "ar"7 },8 "sebastian": {9 "name": "Sebastian",10 "phone": ["+1123456789"],11 "custom": {12 "country": "ca"13 }14 }15}
If all of the provided update objects are valid, a HTTP 200 status code is returned and all the updates are applied. If one or more update objects are invalid, none of the updates are applied (even if some of them were valid) and a HTTP 400 status code is returned in the response, along with an object that holds error messages for the invalid updates.
Here's an example of an invalid payload and the generated response:
1{2 // Valid update object3 "alice": { "name": "Alice" },45 // Invalid; required `name` property is missing6 "sebastian": {}7}
1{2 "sebastian": {3 "#": ["Required property name was not present."]4 }5}
In this case a 400 Bad Request
is returned and neither alice
nor sebastian
are updated.
Note: Currently you can update a maximum of 100 users with a single API call. Attempting to update more will result in a 400 Bad Request
and no users will be updated.
After you successfully create or update a user, you can fetch it back with a GET REST call.
1type UserId = string;2type UnixMilliseconds = number;34type User = {5 id: UserId;6 name: string;7 welcomeMessage: string;8 photoUrl: string;9 role: string;10 email: string[] | null;11 phone: string[] | null;12 custom: { [name: string]: string };13 availabilityText: string;14 locale: string;15 createdAt: UnixMilliseconds;16 pushTokens: { [token_id: string]: true | null } | null;17};
1{2 "availabilityText": null,3 "createdAt": 1623926680918,4 "custom": {},6 "id": "123456789",7 "locale": "en-US",8 "name": "Alice",9 "phone": null,10 "photoUrl": "https://talkjs.com/images/avatar-1.jpg",11 "role": "default",12 "welcomeMessage": "Hey there! How are you? :-)"13 "pushTokens": {14 "apns:MY_TOKEN_ID": true15 }16}
This lists all conversations the user is a part of.
The response is structured similarly to the one when listing all conversations, but it contains additional, user specific fields.
1type UsersConversation = {2 id: ConversationId;3 subject: string | null;4 photoUrl: string | null;5 welcomeMessages: string[] | null;6 custom: { [name: string]: string };7 lastMessage: Message | null;8 unreadMessageCount: number;9 isUnread: boolean;10 participants: {11 [id: UserId]: { access: 'ReadWrite' | 'Read'; notify: boolean };12 };13 createdAt: UnixMilliseconds;14};1516type Response = {17 data: Array<UsersConversation>18}
1{2 "data": [3 {4 "createdAt": 1623926680929,5 "custom": {},6 "id": "87d70908d90cee5d12bd",7 "lastMessage": {8 "attachment": null,9 "conversationId": "87d70908d90cee5d12bd",10 "createdAt": 1630326385720,11 "editedAt": 1629972494633,12 "custom": {},13 "id": "msg_7HtvCJLjdZoRsYE2nCXp2n",14 "location": null,15 "origin": "web",16 "readBy": ["123456789"],17 "senderId": "432156789",18 "referencedMessageId": "msg_012ABcdefghiJKlmNOpqRS",19 "text": "it",20 "type": "UserMessage"21 },22 "participants": {"87d70908d90cee5d12bd"23 "123456789": {24 "access": "ReadWrite",25 "notify": true26 },27 "432156789": {28 "access": "ReadWrite",29 "notify": true30 }31 },32 "photoUrl": null,33 "subject": "Random conversation",34 "welcomeMessages": null,35 "unreadMessageCount": 0,36 "isUnread": false37 },38 {39 ...40 }41 ]42}
Ordering is available since API version 2021-02-09
Default order in versions before 2021-02-09
is createdAt DESC
and cannot be changed.
By default, conversations are returned in the order of the last activity in them, latest first. You can change the order by using orderBy
and orderDirection
query parameters. Conversations can either be sorted by creation date or by the last activity in the conversation. Ordering conversations by the creation date can be useful when you want stable sorting.
The following values are accepted in the orderBy
query parameter:
lastActivity
(default): sort by last activity timestampcreatedAt
: sort by date of creation
The following values are accepted in the orderDirection
query parameter:
DESC
(default): descending orderASC
: ascending order
Conversations can be paginated, as any other listing handle.
Using startingAfter
requires passing in a conversation ID, whereby results will start with the conversation right after the selected one in the current sort order. It works with all currently supported sorting options.
Another option is to use offsetTs
which accepts a timestamp and offsets the results according to the sort order. This might be more useful when used in conjunction with lastActivity
sorting since conversations can move to the front of the list as new messages come, and using one of the conversations as a pointer can yield unexpected results.
offsetTs
is available since API version 2021-02-09
If you want to filter a user's conversations you have the following options:
It is possible to filter conversations by when the last message was sent to the conversation using the lastMessageBefore
and lastMessageAfter
filters.
lastMessageBefore
and lastMessageAfter
should be Unix timestamps expressed in milliseconds.
You can pass unreadsOnly=true
to only list conversations that contain messages the user hasn't read yet.
TalkJS also supports filtering by various fields. The filter interface is precisely the same as the JavaScript SDK's Conversation Filter. In order to use the filter you need to URLencoded the JSON formatted filter.
You can use your favorite programming language to generate the filter in the required JSON structure.
1// fetches a conversation which has a custom field { "category": "shoes" } and the user can read and write in2const filter = {3 custom: { category: ['==', 'shoes'], access: ['==', 'ReadWrite'] },4};5const encodedFilter = encodeURIComponent(JSON.stringify(filter));6const res = await fetch(7 `https://api.talkjs.com/v1/${appId}/users/${userId}/conversations?filter=${encodedFilter}`8);9const conversations = await res.json();
1GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?unreadsOnly=true2GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?lastMessageBefore=15215228493083GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?lastMessageAfter=14215228497324GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?lastMessageBefore=1521522849308&lastMessageAfter=14215228497325GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?unreadsOnly=true&lastMessageBefore=1521522849308&lastMessageAfter=14215228497326GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=57GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&startingAfter=c_847268GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&startingAfter=c_84726&lastMessageBefore=15215228493089GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&offsetTs=152152284930810GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&sortBy=lastActivity+DESC
You can list all users ever created in your TalkJS application. The response has a data
field with an array of User
objects as described above.
1{2 "data": Array<User>3}
1{2 "data": [3 {4 "id": "alice",5 "createdAt": 1525249255419,6 "name": "Alice",8 "welcomeMessage": "Hi there, how are you? :-)",9 "photoUrl": "https://talkjs.com/images/avatar-1.jpg",10 "locale": "ar",11 "role": "buyer",12 "phone": ["+1123456789"],13 "custom": {14 "country": "ca"15 }16 },17 {18 ...19 }20 ]21}
Similarly to other list requests, pagination and limits are applicable to listing users.
TalkJS supports a limited set of filters for getting user information.
In order to only list users that are online or offline, you have to pass the isOnline
parameter to the query string:
1GET https://api.talkjs.com/v1/{appId}/users2GET https://api.talkjs.com/v1/{appId}/users?limit=503GET https://api.talkjs.com/v1/{appId}/users?limit=50&startingAfter=u_314GET https://api.talkjs.com/v1/{appId}/users?startingAfter=u_315GET https://api.talkjs.com/v1/{appId}/users?isOnline=true&limit=36GET https://api.talkjs.com/v1/{appId}/users?isOnline=false&limit=5&startingAfter=u_48
TalkJS currently does not have a way to delete user data. Instead, we suggest you use our edit endpoints to remove any personally identifiable information (PII) associated with the user. We have created a script that you can use to automate this process which can be found in our GitHub examples repository.