Option 1: REST API (not available for now)
For now, there is no such REST API to add members to a team project.
But there has an user voice add rest api for adding members to a team which suggest to add this feature for REST API, you can vote and follow up.
Once the REST API is available, you can use REST API programmatically to add member to a team.
Option 2: API
You can also refer the related code under the user voice in comments part (an example for adding members to default team):
public Task<TeamProject> GetProjectAsync(string projectId)
{
var projectClient = this.Connection.GetClient<ProjectHttpClient>();
return projectClient.GetProject(projectId);
}
public async Task GrantProjectAccessAsync(string projectId, string email)
{
var project = await this.GetProjectAsync(projectId);
var client = this.Connection.GetClient<IdentityHttpClient>();
var identities = await client.ReadIdentitiesAsync(Microsoft.VisualStudio.Services.Identity.IdentitySearchFilter.MailAddress, email);
if (!identities.Any() || identities.Count > 1)
{
throw new InvalidOperationException("User not found or could not get an exact match based on email");
}
var userIdentity = identities.Single();
var groupIdentity = await client.ReadIdentityAsync(project.DefaultTeam.Id);
await client.AddMemberToGroupAsync(
groupIdentity.Descriptor,
userIdentity.Id
);
}
To add members for another team, you can use teamClient.GetTeamsAsync method to get teams for the project, and then get the team id by matching the given team name.