Send or accept a friend request

Use the AddFriendAsync method to create a request targeting another user. It returns an awaitable task or an error.

ParameterDescription
memberIdThe unique identifier of the target user.

The following code snippet shows the AddFriendAsync definition.

/// <summary>
/// Creates a friend request or creates a friendship if the user had already an incoming friend request from the
/// targeted user.
/// </summary>
/// <param name="memberId">The Id of the target user.</param>
/// <returns>The relationship created</returns>
/// <exception cref="System.ArgumentException">Represents an error that occur when an argument is incorrectly setup.</exception>
/// <exception cref="Unity.Services.Friends.Exceptions.RelationshipsServiceException">An exception containing the HttpClientResponse with headers, response code, and string of error.</exception>
Task<Relationship> AddFriendAsync(string memberId);

The following code snippet shows how to send a friend request from the current user to a target user.

// Initialize the Friends service
await FriendsService.Instance.InitializeAsync();
await FriendsService.Instance.AddFriendAsync(memberId);

Use the AddFriendByNameAsync method to create a request targeting another user. It returns an awaitable task or an error.

ParameterDescription
nameThe name of the target user, provided using the player names service.

The following code snippet shows the AddFriendByNameAsync definition.

/// <summary>
/// Creates a friend request, or automatically creates a friendship if the user already has an incoming friend request from the
/// targeted user by their user name.
/// </summary>
/// <param name="name">The name of the target user</param>
/// <returns>The friendship created</returns>
/// <exception cref="System.ArgumentException">Represents an error that occurs when an argument is incorrectly setup.</exception>
/// <exception cref="Unity.Services.Friends.Exceptions.FriendsServiceException">An exception containing the HttpClientResponse with headers, response code, and string of error.</exception>
/// <exception cref="System.InvalidOperationException">Represents an error that occurs when the service has not been initialized.</exception>
Task<Relationship> AddFriendByNameAsync(string name);

The following code snippet shows how to send a friend request from the current user to a target user.

// Initialize the Friends service
await FriendsService.Instance.InitializeAsync();
// playerOneName being the user's profile name that exists within the player names service.
await FriendsService.Instance.AddFriendByNameAsync(playerOneName);