> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-rn-guide-message-privately.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# User Management

> Create and update CometChat users with the Android SDK, and use REST APIs when deletion is required.

<Accordion title="AI Integration Quick Reference">
  ```kotlin theme={null}
  // 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](https://api-explorer.cometchat.com).
</Accordion>

When a user registers in your app, create them in CometChat. When they log in, [log them into CometChat](/sdk/android/authentication-overview) as well.

## Creating a user

Ideally, user creation should happen on your backend using the [REST API](https://api-explorer.cometchat.com/reference/creates-user). For on-the-fly creation during development, use the `createUser()` method with a [`User`](/sdk/reference/entities#user) object and your API Key.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    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());
    }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val apiKey = "API_KEY" // Replace with your API Key.
    val user = User()
    user.uid = "user1" // Replace with your uid for the user to be created.
    user.name = "Kevin" // Replace with the name of the user

    CometChat.createUser(user, apiKey, object : CometChat.CallbackListener<User>() {
    override fun onSuccess(user: User) {
      Log.d("createUser", user.toString())
    }

    override fun onError(e: CometChatException) {
      Log.e("createUser", e.message)
    }
    })
    ```
  </Tab>
</Tabs>

<Warning>
  **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](https://api-explorer.cometchat.com) to keep your API Key secure. Never expose API Keys in production client code.
</Warning>

<Warning>
  UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.
</Warning>

## Updating a user

Use `updateUser()` with a [`User`](/sdk/reference/entities#user) object and API Key. Ideally done on your backend via the [REST API](https://api-explorer.cometchat.com/reference/update-user).

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    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());
    }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val apiKey = "API_KEY" // Replace with your API Key.
    val user = User()
    user.uid = "user1" // Replace with your uid for the user to be updated.
    user.name = "Kevin Fernandez" // Replace with the name of the user

    CometChat.updateUser(user, apiKey, object : CometChat.CallbackListener<User>() {
    override fun onSuccess(user: User) {
      Log.d("updateUser", user.toString())
    }

    override fun onError(e: CometChatException) {
      Log.e("updateUser", e.message)
    }
    })
    ```
  </Tab>
</Tabs>

Please make sure the [`User`](/sdk/reference/entities#user) object provided to the `updateUser()` method has the `UID` of the user to be updated.

## Updating logged-in user

Use `updateCurrentUserDetails()` to update the currently logged-in user's profile. No API Key required.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    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());
    }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val user = User()
    user.name = "Andrew Joseph"

    CometChat.updateCurrentUserDetails(user, object : CallbackListener<User>() {
    override fun onSuccess(user: User) {
      Log.d(TAG, user.toString())
    }

    override fun onError(e: CometChatException) {
      Log.d(TAG, e.message)
    }
    })
    ```
  </Tab>
</Tabs>

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](https://api-explorer.cometchat.com/reference/delete-user) section.

## User Class

| Field         | Editable                                            | Information                                                          |
| ------------- | --------------------------------------------------- | -------------------------------------------------------------------- |
| uid           | specified on user creation. Not editable after that | Unique identifier of the user                                        |
| name          | Yes                                                 | Display name of the user                                             |
| avatar        | Yes                                                 | URL to profile picture of the user                                   |
| link          | Yes                                                 | URL to profile page                                                  |
| role          | Yes                                                 | User role of the user for role based access control                  |
| metadata      | Yes                                                 | Additional information about the user as JSON                        |
| status        | No                                                  | Status of the user. Could be either online/offline                   |
| statusMessage | Yes                                                 | Any custom status message that needs to be set for a user            |
| lastActiveAt  | No                                                  | The unix timestamp of the time the user was last active.             |
| hasBlockedMe  | No                                                  | A boolean that determines if the user has blocked the logged in user |
| blockedByMe   | No                                                  | A boolean that determines if the logged in user has blocked the user |
| tags          | Yes                                                 | A list of tags to identify specific users                            |

## User Payload Structure

<Accordion title="User Object">
  The `User` object returned by SDK methods contains the following fields:

  | Parameter       | Type           | Description                                                   |
  | --------------- | -------------- | ------------------------------------------------------------- |
  | `uid`           | String         | Unique identifier of the user                                 |
  | `name`          | String         | Display name of the user                                      |
  | `avatar`        | String         | URL to user's profile picture                                 |
  | `link`          | String         | URL to user's profile page                                    |
  | `role`          | String         | User role for role-based access control                       |
  | `metadata`      | JSONObject     | Custom data set by developer. Can contain any key-value pairs |
  | `status`        | String         | User online status. Values: `"online"`, `"offline"`           |
  | `statusMessage` | String         | Custom status message set by user                             |
  | `lastActiveAt`  | long           | Unix timestamp of last activity (milliseconds)                |
  | `hasBlockedMe`  | boolean        | Whether this user has blocked the logged-in user              |
  | `blockedByMe`   | boolean        | Whether the logged-in user has blocked this user              |
  | `tags`          | Array\<String> | List of tags for user identification and filtering            |
  | `deactivatedAt` | long           | Unix timestamp when user was deactivated (0 if active)        |

  **Sample User Object:**

  ```json theme={null}
  {
    "uid": "user_123",
    "name": "John Doe",
    "avatar": "https://example.com/avatar.png",
    "link": "https://example.com/profile/user_123",
    "role": "default",
    "metadata": {
      "customKey": "customValue",
      "preferences": {
        "theme": "dark",
        "notifications": true
      }
    },
    "status": "online",
    "statusMessage": "Available",
    "lastActiveAt": 1699900000000,
    "hasBlockedMe": false,
    "blockedByMe": false,
    "tags": ["premium", "verified"],
    "deactivatedAt": 0
  }
  ```
</Accordion>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Authentication" icon="key" href="/sdk/android/authentication-overview">
    Log users into CometChat after creating their accounts
  </Card>

  <Card title="Retrieve Users" icon="users" href="/sdk/android/retrieve-users">
    Fetch user lists and search for specific users
  </Card>

  <Card title="User Presence" icon="circle-dot" href="/sdk/android/user-presence">
    Track user online/offline status in real-time
  </Card>

  <Card title="Block Users" icon="user-slash" href="/sdk/android/block-users">
    Implement user blocking and unblocking features
  </Card>
</CardGroup>
