Skip to main content
Quick Reference for AI Agents & Developers
// Create user (use API Key - dev/testing only)
val apiKey = "API_KEY"
val user = User()
user.uid = "user1"
user.name = "Kevin"

CometChat.createUser(user, apiKey, object : CometChat.CallbackListener<User>() {
    override fun onSuccess(user: User) { }
    override fun onError(e: CometChatException) { }
})

// Update logged-in user (no API Key needed)
val updatedUser = User()
updatedUser.name = "Andrew Joseph"

CometChat.updateCurrentUserDetails(updatedUser, object : CallbackListener<User>() {
    override fun onSuccess(user: User) { }
    override fun onError(e: CometChatException) { }
})
Note: User creation/updates should ideally happen on your backend using REST API.
Available via: SDK | REST API | UI Kits
When a user logs into your app, you need to programmatically log the user into CometChat. But before you log in the user to CometChat, you need to create the user. In summary: When a user registers in your app
  1. You add the user details in your database
  2. You create a user in CometChat
When a user logs into your app
  1. You log in the user to your app
  2. You log in the user to CometChat (programmatically)

Creating a user

Ideally, user creation should take place at your backend. Refer to our REST API to learn more about creating a user and use the appropriate code sample based on your backend language. However, if you wish to create users on the fly, you can use the createUser() method. This method takes a User object and the API Key as input parameters and returns the created User object if the request is successful. For more details on the fields present in the User class, see the User Class section.
String apiKey = "AUTH_KEY"; // Replace with your API Key.
User user = new User();
user.setUid("user1"); // Replace with your uid for the user to be created.
user.setName("Kevin"); // Replace with the name of the user

CometChat.createUser(user, apiKey, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d("createUser", user.toString());
}

@Override
public void onError(CometChatException e) {
  Log.e("createUser", e.getMessage());
}
});
API Key Security: The API Key should only be used for development and testing. In production, create and update users on your backend server using the REST API to keep your API Key secure. Never expose API Keys in production client code.
UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.

Updating a user

Updating a user, similar to creating a user, should ideally be achieved at your backend using the RESTful APIs. For more information, see the update a user section. However, this can also be achieved on the fly using the updateUser() method. This method takes a User object and the API Key as inputs and returns the updated User object on successful execution of the request.
String apiKey = "AUTH_KEY"; // Replace with your API Key.
User user = new User();
user.setUid("user1"); // Replace with your uid for the user to be updated.
user.setName("Kevin Fernandez"); // Replace with the name of the user

CometChat.updateUser(user, apiKey, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d("updateUser", user.toString());
}

@Override
public void onError(CometChatException e) {
  Log.e("updateUser", e.getMessage());
}
});
Please make sure the User object provided to the updateUser() method has the UID of the user to be updated.

Updating logged-in user

Updating a logged-in user is similar to updating a user. The only difference is that this method does not require an Auth Key. This method takes a User object as input and returns the updated User object on successful execution of the request.
User user = new User();
user.setName("Andrew Joseph");

CometChat.updateCurrentUserDetails(user, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d(TAG, user.toString());
}

@Override
public void onError(CometChatException e) {
  Log.d(TAG, e.getMessage());
}
});
By using the updateCurrentUserDetails() method, you can only update the logged-in user regardless of the UID passed. Also, it is not possible to update the role of a logged-in user.

Deleting a user

Deleting a user can only be achieved via the RESTful APIs. For more information, see the delete a user section.

User Class

FieldEditableInformation
uidspecified on user creation. Not editable after thatUnique identifier of the user
nameYesDisplay name of the user
avatarYesURL to profile picture of the user
linkYesURL to profile page
roleYesUser role of the user for role based access control
metadataYesAdditional information about the user as JSON
statusNoStatus of the user. Could be either online/offline
statusMessageYesAny custom status message that needs to be set for a user
lastActiveAtNoThe unix timestamp of the time the user was last active.
hasBlockedMeNoA boolean that determines if the user has blocked the logged in user
blockedByMeNoA boolean that determines if the logged in user has blocked the user
tagsYesA list of tags to identify specific users

Best Practices

Always create and update users on your backend server using the REST API. This keeps your API Key secure and allows you to validate user data before creating accounts in CometChat.
When updating the currently logged-in user’s profile, use updateCurrentUserDetails() instead of updateUser(). This method doesn’t require an API Key and is more secure for client-side updates.
Ensure UIDs are alphanumeric with only underscores and hyphens. Validate UID format before calling createUser() to avoid API errors.
Keep user data synchronized between your app’s database and CometChat. When a user updates their profile in your app, update it in CometChat as well to maintain consistency.
Store additional user information (preferences, settings, custom attributes) in the metadata field as JSON. This allows you to extend user profiles without modifying the core schema.

Troubleshooting

Symptom: createUser() fails with “User with this UID already exists” error.Cause: Attempting to create a user with a UID that’s already registered in CometChat.Solution: Check if the user exists before creating by calling CometChat.getUser() first. If the user exists, use updateUser() instead of createUser().
Symptom: createUser() or updateUser() fails with “Invalid API Key” error.Cause: Incorrect API Key or using Auth Key instead of API Key.Solution: Verify your API Key in the CometChat Dashboard under API & Auth Keys. Ensure you’re using the API Key (for user management), not the Auth Key (for authentication).
Symptom: updateCurrentUserDetails() doesn’t update the user’s role.Cause: The updateCurrentUserDetails() method cannot modify user roles for security reasons.Solution: Update user roles on your backend using the REST API. Role changes should be controlled server-side.

Next Steps