# Contacts (/en/realtime-media/im/build/build-core-messaging/contacts/unity)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

After logging in to Chat, users can start adding contacts and chatting with each other. They can also manage these contacts, for example, by adding, retrieving and removing contacts. They can also add the specified user to the blocklist to stop receiving messages from that user.

    Agora Chat, by default, allows two users to send chat messages to each other as strangers. This means that users can chat without adding each other as a contact. If you only allow chat between contacts, you can contact [support@agora.io](mailto\:support@agora.io) to enable the friend relationship check switch. After this function is enabled, the SDK will check whether the two users trying to chat are on the contact list of each other. If no, the SDK will report error code 221.

    Agora Chat, by default, allows two users to send chat messages to each other as strangers. This means that users can chat without adding each other as a contact. If you only allow chat between contacts, you can contact [support@agora.io](mailto\:support@agora.io) to enable the friend relationship check switch. After this function is enabled, the SDK will check whether the two users trying to chat are on the contact list of each other. If no, the SDK will report error code 221.

    This page shows how to use the Chat SDK to implement contact management.

    ## Understand the tech [#understand-the-tech-5]

    The Chat SDK uses `IContactManager` to add, remove and manage contacts. The following are the core methods:

    * `AddContact`: Adds a contact.
    * `AcceptInvitation`: Accepts the contact invitation.
    * `DeclineInvitation`: Declines the contact invitation.
    * `DeleteContact`: Deletes a contact.
    * `GetAllContactsFromServer`: Retrieves a list of contacts from the server.
    * `AddUserToBlockList`: Adds the specified user to the blocklist.
    * `RemoveUserFromBlockList`: Removes the specified user from the blocklist.
    * `GetBlockListFromServer`: Retrieves a list of blocked users from the server.

    ## Prerequisites [#prerequisites-5]

    Before proceeding, ensure that you meet the following requirements:

    * You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see [Chat SDK quickstart](../../get-started-sdk).
    * You understand the API call frequency limits as described in [Limitations](../limitations).

    ## Implementation [#implementation-5]

    This section shows how to manage contacts with the methods provided by the Chat SDK.

    ### Manage the contact list [#manage-the-contact-list-3]

    Use this section to understand how to send a contact invitation, listen for contact events, and accept or decline the contact invitation.

    #### Send a contact invitation [#send-a-contact-invitation-3]

    Call `AddContact` to add the specified user as a contact:

    ```csharp
    SDKClient.Instance.ContactManager.AddContact(username, reason, callback: new CallBack(
      onSuccess: () =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    ```

    #### Listen for contact events [#listen-for-contact-events-3]

    Use `IContactManagerDelegate` to add the following delegates. When a user receives a contact invitation, you can accept or decline the invitation.

    ```csharp
    // Inherit and instantiate IContactManagerDelegate。
    public class ContactManagerDelegate : IContactManagerDelegate {
        // Occurs when a contact is added.
        public void OnContactAdded(string username)
        {
        }
        // Occurs when the contact is removed.
        public void OnContactDeleted(string username)
        {
        }
        // Occurs when a contact invitation is received.
        public void OnContactInvited(string username, string reason)
        {
        }
        // Occurs when the contact invitation is accepted.
        public void OnFriendRequestAccepted(string username)
        {
        }
        // Occurs when the contact invitation is declined.
        public void OnFriendRequestDeclined(string username)
        {
        }
    }
    // Call AddContactManagerDelegate to listen for contact events.
    ContactManagerDelegate adelegate = new ContactManagerDelegate();
    SDKClient.Instance.ContactManager.AddContactManagerDelegate(adelegate);
    // Call RemoveContactManagerDelegate to remove the delegate.
    SDKClient.Instance.ContactManager.RemoveContactManagerDelegate(adelegate);
    ```

    #### Accept or decline the contact invitation [#accept-or-decline-the-contact-invitation-3]

    After receiving `OnContactInvited`, call `AcceptInvitation` or `DeclineInvitation` to accept or decline the invitation.

    ```csharp
    // Accept the contact invitation. Once you accept the invitation, the sender receives the OnFriendRequestAccepted callback.
    SDKClient.Instance.ContactManager.AcceptInvitation(username, callback: new CallBack(
       onSuccess: () =>
       {
       },
       onError: (code, desc) =>
       {
       }
    ));
    // Decline the contact invitation. Once you decline the invitation, the sender receives the OnFriendRequestDeclined callback.
    SDKClient.Instance.ContactManager.DeclineInvitation(username, callback: new CallBack(
      onSuccess: () =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    ```

    #### Delete a contact [#delete-a-contact-5]

    Call `DeleteContact` to delete the specified contact. The deleted user receives the `OnContactDeleted` callback.

    ```csharp
    SDKClient.Instance.ContactManager.DeleteContact(username, callback: new CallBack(
      onSuccess: () =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    ```

    #### Retrieve the contact list [#retrieve-the-contact-list-4]

    To get the contact list, you can call `GetAllContactsFromServer` to retrieve contacts from the server. After that, you can also call `GetAllContactsFromDB` to retrieve contacts from the local database.

    ```csharp
    // Retrieve a list of contacts from the server.
    SDKClient.Instance.ContactManager.GetAllContactsFromServer(new ValueCallBack>(
      onSuccess: (list) =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    // After retrieving the contact list from the server, you can call `GetAllContactsFromDB` to get the list of contacts from the local database.
    Listlist = SDKClient.Instance.ContactManager.GetAllContactsFromDB();
    ```

    ### Manage the block list [#manage-the-block-list-1]

    You can add any other users to the block list, regardless of whether they are on the contact list or not. Contacts are still displayed on the contact list even if they are added to the block list. After adding users to the block list, you can still send messages to them, but will not receive messages from them as they cannot send messages or friend requests to you.

    #### Add a user to the block list [#add-a-user-to-the-block-list-1]

    Call `AddUserToBlockList` to add the specified user to the block list.

    ```csharp
    SDKClient.Instance.ContactManager.AddUserToBlockList(username, callback: new CallBack(
      onSuccess: () =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    ```

    #### Remove a user from the block list [#remove-a-user-from-the-block-list-1]

    To remove the specified user from the block list, call `RemoveUserFromBlockList`.

    ```csharp
    SDKClient.Instance.ContactManager.RemoveUserFromBlockList(username, callback: new CallBack(
      onSuccess: () =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    ```

    #### Retrieve the block list from the server [#retrieve-the-block-list-from-the-server-1]

    To get the block list, call `GetBlockListFromServer` to retrieve a list of blocked users from the server.

    ```csharp
    // Call `GetBlockListFromServer` to get the block list from the server.
    SDKClient.Instance.ContactManager.GetBlockListFromServer(new ValueCallBack>(
      onSuccess: (list) =>
      {
      },
      onError: (code, desc) =>
      {
      }
    ));
    ```

    
  
      
  
