Users

All resource URLs include an {appId}, which you can find on the Settings page of your dashboard.

GETPUT/v1/{appId}/users/{userId}

{userId} is your internal unique identifier for the user.

Create or update a 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.

PUT/v1/{appId}/users/{userId}
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> | null
11}

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.

Batch update users

You can update multiple users with a single API call.

PUT/v1/{appId}/users
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> | null
12 }
13}

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:

PUT/v1/{appId}/users
1{
2 // Valid update object
3 "alice": { "name": "Alice" },
4
5 // Invalid; required `name` property is missing
6 "sebastian": {}
7}

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.

Get created users

After you successfully create or update a user, you can fetch it back with a GET REST call.

GET/v1/{appId}/users/{userId}
1type UserId = string;
2type UnixMilliseconds = number;
3
4type 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};

List conversations a user is a part of

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.

GET/v1/{appId}/users/{userId}/conversations
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};
15
16type Response = {
17 data: Array<UsersConversation>
18}

Ordering

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 timestamp
  • createdAt: sort by date of creation

The following values are accepted in the orderDirection query parameter:

  • DESC (default): descending order
  • ASC: ascending order

Pagination

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

Filters

If you want to filter a user's conversations you have the following options:

Filter by the last message's timestamp

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.

Only return unreads

You can pass unreadsOnly=true to only list conversations that contain messages the user hasn't read yet.

Filter by custom filters

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.

NodeJS example

1// fetches a conversation which has a custom field { "category": "shoes" } and the user can read and write in
2const 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();

Examples

1GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?unreadsOnly=true
2GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?lastMessageBefore=1521522849308
3GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?lastMessageAfter=1421522849732
4GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?lastMessageBefore=1521522849308&lastMessageAfter=1421522849732
5GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?unreadsOnly=true&lastMessageBefore=1521522849308&lastMessageAfter=1421522849732
6GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5
7GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&startingAfter=c_84726
8GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&startingAfter=c_84726&lastMessageBefore=1521522849308
9GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&offsetTs=1521522849308
10GET https://api.talkjs.com/v1/{appId}/users/{userId}/conversations?limit=5&sortBy=lastActivity+DESC

List all users in the application

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.

GET/v1/{appId}/users
1{
2 "data": Array<User>
3}

Limits and pagination

Similarly to other list requests, pagination and limits are applicable to listing users.

Filters

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:

GET/v1/{appId}/users?isOnline=true
GET/v1/{appId}/users?isOnline=false

List users examples

1GET https://api.talkjs.com/v1/{appId}/users
2GET https://api.talkjs.com/v1/{appId}/users?limit=50
3GET https://api.talkjs.com/v1/{appId}/users?limit=50&startingAfter=u_31
4GET https://api.talkjs.com/v1/{appId}/users?startingAfter=u_31
5GET https://api.talkjs.com/v1/{appId}/users?isOnline=true&limit=3
6GET https://api.talkjs.com/v1/{appId}/users?isOnline=false&limit=5&startingAfter=u_48

Delete users

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.