---
title: "client.Labels"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://openemail.uk/docs/csharp/reference/labels"
area: "C#"
category: "Reference"
---

# client.Labels

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

The user labels a thread can carry: list, create, rename, recolour and delete them, and read the palette of colours the app offers.

### `Labels.ListAsync`

List the workspace's labels, a page at a time

```csharp
Task<Page> ListAsync(
    int? limit = null,
    string? cursor = null,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Returns one page of the workspace's user labels, sorted by name and then by id. `Labels.ListAllAsync` collects every page and `Labels.IterateAsync` walks them lazily. Each label carries its colour, `threadCount` (how many conversations carry it now) and `createdAt` and `updatedAt`, which is everything the Labels table in the app shows.

System labels such as `INBOX`, `STARRED` and `UNREAD` are not listed. A thread carries them and `Threads.UpdateAsync` takes them, but they cannot be renamed, recoloured or deleted.

`color` is null on a label saved with no colour. Otherwise `color.backgroundColor` is a hex value or a gradient token such as `gradient:sunset`, and `color.textColor` is the ink the app draws on it, worked out on the server rather than stored.

Scopes: `labels:read`.

**Parameters**

- `limit` (`int?`): Page size, from 1 to 100. The server defaults to 25.
- `cursor` (`string?`): The `nextCursor` of the previous page, passed back as it came. Leave it out for the first page.
- `apiKey` (`string?`): Overrides the client's API key for this call only.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A `Page` of label objects, with `items`, `hasMore` and `nextCursor`. Each item has `id`, `name`, `type`, `color`, `threadCount`, `createdAt` and `updatedAt`.

**Example**

```csharp
var page = await client.Labels.ListAsync(limit: 50);

foreach (var label in page)
{
    Console.WriteLine($"{label["id"]} {label["name"]} on {label["threadCount"]} conversations, {label["color"]?["backgroundColor"]?.ToString() ?? "no colour"}");
}

if (page.HasMore)
{
    var next = await client.Labels.ListAsync(limit: 50, cursor: page.NextCursor);

    Console.WriteLine($"{next.Count} more");
}
```

**Notes**

- Labels belong to the workspace, so a narrowed key still sees every label. `threadCount` is the exception: it counts only conversations delivered to the addresses the key holds.
- An id is fixed when the label is created and survives a rename, so match on `id` rather than `name` in stored configuration.
- The sidebar order in the app is each person's own arrangement and is not exposed.
- The cursor is opaque and holds where the last row sat in this order, so a row deleted or edited between pages never breaks the walk: the next page starts at the first row that sorts after it. A cursor this list did not hand out is a 400 `invalid_cursor`.

Also available in: API [`GET /labels`](https://openemail.uk/docs/api/reference/labels#get-labels); TypeScript [`labels.list()`](https://openemail.uk/docs/sdk/reference/labels#list); Python [`labels.list()`](https://openemail.uk/docs/python/reference/labels#list); Ruby [`labels.list`](https://openemail.uk/docs/ruby/reference/labels#list); PHP [`labels->list`](https://openemail.uk/docs/php/reference/labels#list); Go [`Labels.List`](https://openemail.uk/docs/go/reference/labels#list); Java [`labels().list`](https://openemail.uk/docs/java/reference/labels#list); CLI [`openemail labels list`](https://openemail.uk/docs/cli/reference/labels#labels-list).

### `Labels.ListAllAsync`

Collect every label into one object

```csharp
Task<IReadOnlyList<JsonObject>> ListAllAsync(
    int? limit = null,
    string? cursor = null,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Walks every page of `Labels.ListAsync` and returns all labels in one object, sorted by name and then by id. One request per page.

Scopes: `labels:read`.

**Parameters**

- `limit` (`int?`): Page size for each request, from 1 to 100. The server defaults to 25.
- `cursor` (`string?`): Starts the walk after this cursor instead of the first page.
- `apiKey` (`string?`): Overrides the client's API key for every page of this walk.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A list of label objects holding every label.

**Example**

```csharp
var labels = await client.Labels.ListAllAsync(limit: 100);

Console.WriteLine(labels.Count);
```

**Notes**

- If any page fails, the exception is thrown and the labels already fetched are discarded.

Also available in: API [`GET /labels`](https://openemail.uk/docs/api/reference/labels#get-labels); TypeScript [`labels.listAll()`](https://openemail.uk/docs/sdk/reference/labels#listAll); Python [`labels.list_all()`](https://openemail.uk/docs/python/reference/labels#listAll); Ruby [`labels.list_all`](https://openemail.uk/docs/ruby/reference/labels#listAll); PHP [`labels->listAll`](https://openemail.uk/docs/php/reference/labels#listAll); Go [`Labels.ListAll`](https://openemail.uk/docs/go/reference/labels#listAll); Java [`labels().listAll`](https://openemail.uk/docs/java/reference/labels#listAll).

### `Labels.IterateAsync`

Stream the labels one at a time

```csharp
IAsyncEnumerable<JsonObject> IterateAsync(
    int? limit = null,
    string? cursor = null,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Returns an `IAsyncEnumerable<JsonObject>` that yields labels one at a time, sorted by name and then by id, and requests the next page only once the current one is used up. Nothing is fetched until the loop starts, and breaking out of the `await foreach` stops the requests.

Scopes: `labels:read`.

**Parameters**

- `limit` (`int?`): Page size for each request, from 1 to 100. The server defaults to 25.
- `cursor` (`string?`): Starts the walk after this cursor instead of the first page.
- `apiKey` (`string?`): Overrides the client's API key for every page of this walk.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

An `IAsyncEnumerable<JsonObject>` that yields one label per step.

**Example**

```csharp
await foreach (var label in client.Labels.IterateAsync())
{
    if ((int?)label["threadCount"] == 0)
    {
        Console.WriteLine($"{label["name"]} is on no conversation");
    }
}
```

**Notes**

- The generator is lazy, so an abandoned loop costs only the pages you consumed.

Also available in: API [`GET /labels`](https://openemail.uk/docs/api/reference/labels#get-labels); TypeScript [`labels.iterate()`](https://openemail.uk/docs/sdk/reference/labels#iterate); Python [`labels.iterate()`](https://openemail.uk/docs/python/reference/labels#iterate); Ruby [`labels.iterate`](https://openemail.uk/docs/ruby/reference/labels#iterate); PHP [`labels->iterate`](https://openemail.uk/docs/php/reference/labels#iterate); Go [`Labels.Iterate`](https://openemail.uk/docs/go/reference/labels#iterate); Java [`labels().iterate`](https://openemail.uk/docs/java/reference/labels#iterate).

### `Labels.ListColorsAsync`

List the colours the app offers for labels

```csharp
Task<IReadOnlyList<JsonObject>> ListColorsAsync(
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Returns the whole label palette as a plain list: the fourteen solid colours and seven gradients the app offers when you make or edit a label, in the order it shows them. It is a fixed catalogue, so there is no paging. Fetch it once and keep it.

`value` is what to send as `color.backgroundColor`: a hex such as `#3B82F6` for a solid, a token such as `gradient:sunset` for a gradient. `textColor` is the ink drawn on it. A gradient also carries `from` and `to`, drawn at 135 degrees, and `solid`, one hex for places a gradient cannot go.

A label may carry a colour outside this list. Any hex set through the API is kept as it is, and the app offers it back as its own swatch.

Scopes: `labels:read`.

**Parameters**

- `apiKey` (`string?`): Overrides the client's API key for this call only.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A list of `JsonObject` items, each with `kind`, `name`, `value`, `solid`, `from`, `to` and `textColor`.

**Example**

```csharp
var colors = await client.Labels.ListColorsAsync();

foreach (var color in colors)
{
    Console.WriteLine($"{color["kind"]} {color["name"]} {color["value"]} with {color["textColor"]} text");
}
```

**Notes**

- `kind` is `solid` or `gradient`. `from` and `to` are null on a solid.

Also available in: API [`GET /labels/colors`](https://openemail.uk/docs/api/reference/labels#get-labels-colors); TypeScript [`labels.listColors()`](https://openemail.uk/docs/sdk/reference/labels#listColors); Python [`labels.list_colors()`](https://openemail.uk/docs/python/reference/labels#listColors); Ruby [`labels.list_colors`](https://openemail.uk/docs/ruby/reference/labels#listColors); PHP [`labels->listColors`](https://openemail.uk/docs/php/reference/labels#listColors); Go [`Labels.ListColors`](https://openemail.uk/docs/go/reference/labels#listColors); Java [`labels().listColors`](https://openemail.uk/docs/java/reference/labels#listColors); CLI [`openemail labels list-colors`](https://openemail.uk/docs/cli/reference/labels#labels-list-colors).

### `Labels.GetAsync`

Read one user label by id

```csharp
Task<JsonObject> GetAsync(
    string id,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Looks up a single user label and returns it in the same shape as a row of `Labels.ListAsync`, with its colour, `threadCount`, `createdAt` and `updatedAt`.

Only user labels are served. `INBOX`, `TRASH` and the other system ids are a 404 here even though threads carry them. Ids are matched exactly, so `user_receipts` does not find `USER_RECEIPTS`.

Scopes: `labels:read`.

**Parameters**

- `id` (`string`, required): Label id such as `USER_RECEIPTS`, matched case sensitively.
- `apiKey` (`string?`): Overrides the client's API key for this call only.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A `JsonObject` with `id`, `name`, `type` set to `user`, `color`, `threadCount`, `createdAt` and `updatedAt`.

**Example**

```csharp
try
{
    var label = await client.Labels.GetAsync("USER_RECEIPTS");

    Console.WriteLine($"{label["name"]} is on {label["threadCount"]} conversations");
}
catch (OpenEmailApiException error) when (error.IsNotFound)
{
    Console.WriteLine("No such label");
}
```

**Notes**

- A missing label throws an `OpenEmailApiException` whose `IsNotFound` is true and whose `Code` is `resource_not_found`.

Also available in: API [`GET /labels/{id}`](https://openemail.uk/docs/api/reference/labels#get-labels-id); TypeScript [`labels.get()`](https://openemail.uk/docs/sdk/reference/labels#get); Python [`labels.get()`](https://openemail.uk/docs/python/reference/labels#get); Ruby [`labels.get`](https://openemail.uk/docs/ruby/reference/labels#get); PHP [`labels->get`](https://openemail.uk/docs/php/reference/labels#get); Go [`Labels.Get`](https://openemail.uk/docs/go/reference/labels#get); Java [`labels().get`](https://openemail.uk/docs/java/reference/labels#get); CLI [`openemail labels get`](https://openemail.uk/docs/cli/reference/labels#labels-get).

### `Labels.CreateAsync`

Create a user label

```csharp
Task<JsonObject> CreateAsync(
    IReadOnlyDictionary<string, object?> body,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Creates a label and returns it as it is stored. `name` is trimmed and must then be 1 to 225 characters. The id is derived from the name as `USER_` followed by the name upper cased, with each run of whitespace turned into `_`, so `Big Clients` becomes `USER_BIG_CLIENTS`, and it never changes afterwards.

A name another label already has, compared without case, is refused with 409 `label_name_taken`, and so is a name whose id another label holds because it was created under that name and renamed since. An existing label is never silently overwritten. A workspace holds at most 50 user labels, and the call past that is a 422 `label_limit_reached` on `name`.

`color` is optional. `color.backgroundColor` is a hex colour (`#RGB`, `#RGBA`, `#RRGGBB` or `#RRGGBBAA`, stored upper cased) or a gradient token such as `gradient:sunset`, and anything else is a 422 `invalid_parameter` on `color.backgroundColor`. `Labels.ListColorsAsync` returns the palette the app offers. `textColor` may be sent but is ignored: the ink is worked out from the background. Leaving `color` out stores no colour.

Scopes: `labels:write`.

**Parameters**

- `name` (`string`, required): Display name, trimmed, 1 to 225 characters. Also decides the id.
- `color` (`dictionary`): The colour, a dictionary with `backgroundColor`. Leave it out for a label with no colour.
- `color.backgroundColor` (`string`): A hex colour such as `#3B82F6` or a gradient token such as `gradient:sunset`, at most 32 characters. Required once `color` is given.
- `color.textColor` (`string`): Accepted and ignored. The ink is worked out from `backgroundColor`.
- `apiKey` (`string?`): Overrides the client's API key for this call only.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A `JsonObject` with the new `id`, the trimmed `name`, `color`, `threadCount` of 0, `createdAt` and `updatedAt`.

**Example**

```csharp
try
{
    var label = await client.Labels.CreateAsync(new Body
    {
        ["name"] = "Big Clients",
        ["color"] = new Body { ["backgroundColor"] = "#3B82F6" },
    });

    Console.WriteLine($"Created {label["id"]}");
}
catch (OpenEmailApiException error) when (error.IsConflict)
{
    Console.WriteLine($"That name is taken: {error.Code}");
}
```

**Notes**

- The SDK does not retry a create after a network failure. If you retry by hand after a lost response, a 409 `label_name_taken` means the first attempt succeeded.

Also available in: API [`POST /labels`](https://openemail.uk/docs/api/reference/labels#post-labels); TypeScript [`labels.create()`](https://openemail.uk/docs/sdk/reference/labels#create); Python [`labels.create()`](https://openemail.uk/docs/python/reference/labels#create); Ruby [`labels.create`](https://openemail.uk/docs/ruby/reference/labels#create); PHP [`labels->create`](https://openemail.uk/docs/php/reference/labels#create); Go [`Labels.Create`](https://openemail.uk/docs/go/reference/labels#create); Java [`labels().create`](https://openemail.uk/docs/java/reference/labels#create); CLI [`openemail labels create`](https://openemail.uk/docs/cli/reference/labels#labels-create).

### `Labels.UpdateAsync`

Rename or recolour a user label

```csharp
Task<JsonObject> UpdateAsync(
    string id,
    IReadOnlyDictionary<string, object?> patch,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Renames a label, recolours it, or both, and returns it as it is stored. Send `name`, `color` or both: a field left out stays as it is, and a patch with neither is a 422. `["color"] = null`, or an empty `backgroundColor`, clears the colour.

The id never changes. A label created as `Receipts` keeps `USER_RECEIPTS` after a rename to `Invoices`, and every thread keeps the label. A new name another label already has, compared without case, is refused with 409 `label_name_taken`.

Colours follow the rules on `Labels.CreateAsync`: a hex value or a gradient token, and anything else is a 422 `invalid_parameter`. Only user labels can be changed, and a system label id is a 404.

Scopes: `labels:write`.

**Parameters**

- `id` (`string`, required): Label id such as `USER_RECEIPTS`.
- `name` (`string`): New display name, trimmed, 1 to 225 characters. Left out, the name stays.
- `color` (`dictionary`): New colour, a dictionary with `backgroundColor`. Null clears it, and leaving it out keeps the stored colour.
- `color.backgroundColor` (`string`): A hex colour or a gradient token, at most 32 characters. An empty string clears the colour.
- `color.textColor` (`string`): Accepted and ignored. The ink is worked out from `backgroundColor`.
- `apiKey` (`string?`): Overrides the client's API key for this call only.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A `JsonObject` with the unchanged `id` and the new `name` and `color`.

**Example**

```csharp
var label = await client.Labels.UpdateAsync("USER_RECEIPTS", new Body
{
    ["name"] = "Invoices",
    ["color"] = new Body { ["backgroundColor"] = "gradient:sunset" },
});

Console.WriteLine($"{label["id"]} is now called {label["name"]}");

await client.Labels.UpdateAsync("USER_RECEIPTS", new Body { ["color"] = null });
```

**Notes**

- The SDK retries this call after a network failure, since the same patch sent twice leaves the same label.

Also available in: API [`PATCH /labels/{id}`](https://openemail.uk/docs/api/reference/labels#patch-labels-id); TypeScript [`labels.update()`](https://openemail.uk/docs/sdk/reference/labels#update); Python [`labels.update()`](https://openemail.uk/docs/python/reference/labels#update); Ruby [`labels.update`](https://openemail.uk/docs/ruby/reference/labels#update); PHP [`labels->update`](https://openemail.uk/docs/php/reference/labels#update); Go [`Labels.Update`](https://openemail.uk/docs/go/reference/labels#update); Java [`labels().update`](https://openemail.uk/docs/java/reference/labels#update); CLI [`openemail labels update`](https://openemail.uk/docs/cli/reference/labels#labels-update).

### `Labels.TestAsync`

Try a label on recent mail

```csharp
Task<JsonObject> TestAsync(
    string id,
    IReadOnlyDictionary<string, object?>? body = null,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Runs the AI over the newest 20 inbox threads and returns the ones the label would catch, without labelling anything. Send `aiInstructions` to try wording you have not saved yet, or leave it out to try what the label holds.

It spends one AI action of the caller, which the labelling of arriving mail never does. A message that arrived encrypted is skipped, and a key limited to particular addresses reads only the threads delivered to them.

Scopes: `labels:write`, `threads:read`.

**Parameters**

- `id` (`string`, required): Label id such as `USER_RECEIPTS`, as `ListAsync` returns it.
- `aiInstructions` (`string`): What the label is for, in plain words, 1 to 500 characters. Left out, the instructions saved on the label are tried.
- `cancellationToken` (`CancellationToken`): Cancels the request.
- `apiKey` (`string?`): Overrides the client API key for this call only.

**Returns**

A `JsonObject`, `{ object: 'label_test', labelId, examined, matched }`. `examined` is how many recent threads were read, and each match is `{ threadId, subject, sender, receivedOn }`, with `sender` the address the newest message is from and `receivedOn` when it arrived, as its Date header gives it, or null.

**Example**

```csharp
var trial = await client.Labels.TestAsync("USER_RECEIPTS", new Body { ["aiInstructions"] = "Receipts and invoices for something we paid for" });

Console.WriteLine($"{trial["matched"]?.AsArray().Count} of {trial["examined"]} recent threads would get the label");

foreach (var match in trial["matched"]?.AsArray() ?? [])
{
    Console.WriteLine($"{match?["sender"]}: {match?["subject"]}");
}
```

**Notes**

- Needs both `labels:write` and `threads:read`. An unknown label id is a 404.
- With no instructions to try, neither in the call nor on the label, it is a 422 `invalid_parameter`. A spent AI allowance is a 429 `ai_quota_exceeded`, and an install with no AI model set up a 409 `ai_not_configured`.
- Nothing is saved: the label keeps the instructions it had. Save the wording with `UpdateAsync` once it catches what you want.
- The SDK does not retry it, because each call spends an AI action.

Also available in: API [`POST /labels/{id}/test`](https://openemail.uk/docs/api/reference/labels#post-labels-id-test); TypeScript [`labels.test()`](https://openemail.uk/docs/sdk/reference/labels#test); Python [`labels.test()`](https://openemail.uk/docs/python/reference/labels#test); Ruby [`labels.test`](https://openemail.uk/docs/ruby/reference/labels#test); PHP [`labels->test`](https://openemail.uk/docs/php/reference/labels#test); Go [`Labels.Test`](https://openemail.uk/docs/go/reference/labels#test); Java [`labels().test`](https://openemail.uk/docs/java/reference/labels#test); CLI [`openemail labels test`](https://openemail.uk/docs/cli/reference/labels#labels-test).

### `Labels.DeleteAsync`

Delete a user label and remove it from every thread

```csharp
Task<JsonObject> DeleteAsync(
    string id,
    string? apiKey = null,
    CancellationToken cancellationToken = default)
```

Deletes a user label and, in the same transaction, takes it off every thread that carried it. The threads are otherwise untouched, so a thread that was only filed under this label stays in whatever folder it was in.

There is no undo. Creating a label with the same name again produces the same id, but the threads it was removed from do not get it back. Only user labels can be deleted, and a system label id is a 404.

Scopes: `labels:write`.

**Parameters**

- `id` (`string`, required): Label id such as `USER_OLD_PROJECT`.
- `apiKey` (`string?`): Overrides the client's API key for this call only.
- `cancellationToken` (`CancellationToken`): Cancels the request.

**Returns**

A `JsonObject` with `object` set to `label`, `id` and `deleted` set to true.

**Example**

```csharp
var label = await client.Labels.GetAsync("USER_OLD_PROJECT");

var deleted = await client.Labels.DeleteAsync(label["id"]!.GetValue<string>());

if ((bool?)deleted["deleted"] == true)
{
    Console.WriteLine($"Removed from {label["threadCount"]} conversations");
}
```

**Notes**

- The SDK does not retry a delete. A 404 on your own second attempt after a lost response means the first one worked.
- Call `Labels.GetAsync` first if you want to say how many conversations will lose the label: its `threadCount` is that number.

Also available in: API [`DELETE /labels/{id}`](https://openemail.uk/docs/api/reference/labels#delete-labels-id); TypeScript [`labels.delete()`](https://openemail.uk/docs/sdk/reference/labels#delete); Python [`labels.delete()`](https://openemail.uk/docs/python/reference/labels#delete); Ruby [`labels.delete`](https://openemail.uk/docs/ruby/reference/labels#delete); PHP [`labels->delete`](https://openemail.uk/docs/php/reference/labels#delete); Go [`Labels.Delete`](https://openemail.uk/docs/go/reference/labels#delete); Java [`labels().delete`](https://openemail.uk/docs/java/reference/labels#delete); CLI [`openemail labels delete`](https://openemail.uk/docs/cli/reference/labels#labels-delete).
