# How new clients are assigned

See which team and agent a client gets when you create it through the API.

[Create a client](/reference/clients-create) only needs `firstName`, `email` and `phone`. Every client must belong to a team, and it can also have an agent. If you don't choose them, Estactics does.

A team is one level of your organization's hierarchy, such as Sales or Team A. An agent is the user who owns the client.

## The flow

Start at the top and stop at the first Yes.

![Flow chart of how a new client gets a team and an agent. If you send assignedToUserId, that user is the agent and the team is the one you sent, otherwise that agent's team. If you send a campaignCode, the agent is the campaign's default agent if it has one, and the team is the one you sent, otherwise the campaign's team. If you send assignedToHierarchyId, the client gets that team and no agent. If you send none of these and the API key's user can only see clients assigned to them, the client goes to that user and their team. Otherwise it goes to that user's team with no agent. If no team can be found, the request fails with a 400 error.](/diagrams/client-assignment.svg)

## What each request gets

Each row applies only if the rows above it don't.

| You send | Agent | Team |
| --- | --- | --- |
| `assignedToUserId` | That user | The team you sent, otherwise that agent's team |
| `campaignCode` | The campaign's default agent, if it has one | The team you sent, otherwise the campaign's team |
| `assignedToHierarchyId` | None | The team you sent |
| None of these, and the API key's user can only see clients assigned to them and is on the client's brand | The API key's user | The API key's user's team |
| None of these, with any other API key | None | The API key's user's team |

If the agent you send has no team, the client goes to the API key's user's team.

> [!NOTE]
> A campaign's default agent is used even when you send a different team, so the agent can end up outside that team. If you send `assignedToUserId` with a `campaignCode`, the campaign's agent, team and brand are not used, but the client is still linked to the campaign.

## When the API key's user becomes the agent

A user without hierarchy data access can only see clients assigned to them. When such a user's API key creates a client with no agent, campaign or team, the client is assigned to them so they can see it.

This doesn't apply to the organization owner, who can see every client. It also needs the user to be on the client's brand. If they aren't, the client has no agent and they won't see it.

## Which brand the client gets

1. With a `campaignCode` and no `assignedToUserId`, the client gets the campaign's brand, even if you send `brandId`.
2. Otherwise it gets the `brandId` you sent.
3. If you sent neither, it gets your organization's main brand.

> [!WARNING]
> Other than the organization owner, users only see the clients of brands they are on, or clients assigned to them. If your API key's user isn't on the main brand, send a `brandId` they are on.

## Who can see the client

- The organization owner.
- The client's agent.
- Users with hierarchy data access who are on the client's brand, when their team, or the team set as its data scope, is at or above the client's team. When a client has an agent, the agent's team counts as the client's team.

## Existing clients

This flow only applies to new clients. A returning client keeps their team and agent, even when you set `throwErrorOnClientExists` to `false`.

The agent of a new client gets an email about it, whichever way they were chosen.

## Errors

| Status | When |
| --- | --- |
| 400 | No team can be found, because neither the agent nor the API key's user has one |
| 400 | `assignedToHierarchyId` isn't a team in your organization |
| 400 | `brandId` isn't in your organization, or the API key's user isn't on it and isn't the owner |
| 400 | The agent, including a campaign's default agent, isn't on the client's brand |
| 404 | `campaignCode` doesn't exist in your organization |
| 404 | `assignedToUserId` doesn't exist |
