---
title: "client.Files"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://openemail.uk/docs/go/reference/files"
area: "Go"
category: "Reference"
---

# client.Files

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

## Methods

Every attachment the mailbox holds, sent and received, and the files uploaded to it, with their bytes, the download links they went out as and whether each can be deleted.

### `Files.List`

List one page of the files the mailbox holds

```go
List(ctx context.Context, opts ...openemail.RequestOption) (*openemail.Page, error)
```

Returns one page of the Files page: every attachment the mailbox holds, sent and received, and every file uploaded to it, with its name, type, size, the address it came to, the thread it belongs to and whether it can be deleted. `ListAll` collects every page and `Iterate` walks them lazily.

Only an upload that nothing depends on can be deleted, and its `deletable` is true. Every other row has a `usage` saying what keeps it: `received`, `sent`, `linked` for an upload that went out as a download link that still works, or `scheduled` for one attached to a message that has not gone out yet.

A key limited to particular addresses or domains sees only the files that arrived at them, the same boundary the thread list uses, so it does not see files uploaded for the whole workspace. A deleted file is not listed.

The index starts from the day it shipped, so an older mailbox lists what has arrived since. Older attachments are still on their messages, where `Threads.ListAttachments` reads them.

Scopes: `files:read`.

**Parameters**

- `openemail.WithQ` (`string`): Searches the file name and its type. Words match loosely, and a close spelling is tried when nothing matches exactly.
- `openemail.WithKind` (`string`): Keeps one kind: `image`, `pdf`, `audio`, `video` or `text`, the choices of the filter on the Files page.
- `openemail.WithDirection` (`string`): Keeps one direction: `inbound` for files that arrived on a message, `outbound` for files that went out on one, `uploaded` for files put on the Files page.
- `openemail.WithAddress` (`string`): Keeps the files of one address, the `deliveredTo` of the file, compared without regard to case.
- `openemail.WithSince` (`time.Time`): Keeps files added at or after this moment. A `time.Time`.
- `openemail.WithUntil` (`time.Time`): Keeps files added before this moment. A `time.Time`.
- `openemail.WithSort` (`string`): `newest` (the default), `oldest`, `largest` or `name`. A cursor carries on in the order it was handed out in.
- `openemail.WithLimit` (`int`): Page size, from 1 to 100. The server defaults to 25.
- `openemail.WithCursor` (`string`): The `NextCursor` of the previous page. Leave it out for the first page.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

A `*openemail.Page` with `Items`, `HasMore` and `NextCursor`. Each item has `id`, `filename`, `mimeType`, `sizeBytes`, `direction`, `threadId`, `messageId`, `deliveredTo`, `usage`, `deletable` and `createdAt`.

**Example**

```go
page, err := client.Files.List(ctx, openemail.WithKind("pdf"), openemail.WithSort("largest"))
if err != nil {
	return err
}

for _, file := range page.Items {
	fmt.Println(file.String("filename"), file.Int("sizeBytes"))
}

fmt.Println(page.HasMore, page.NextCursor)
```

**Notes**

- Needs `files:read`. Reading an email with `threads:read` still reads the attachments on it; `files:read` reads every file, uploads included.
- The cursor is opaque and holds where the last row sat in this order. A cursor this list did not hand out, or one handed out under another `sort`, is a 400 `invalid_cursor`.

Also available in: API [`GET /files`](https://openemail.uk/docs/api/reference/files#get-files); TypeScript [`files.list()`](https://openemail.uk/docs/sdk/reference/files#list); Python [`files.list()`](https://openemail.uk/docs/python/reference/files#list); Ruby [`files.list`](https://openemail.uk/docs/ruby/reference/files#list); PHP [`files->list`](https://openemail.uk/docs/php/reference/files#list); Java [`files().list`](https://openemail.uk/docs/java/reference/files#list); C# [`Files.ListAsync`](https://openemail.uk/docs/csharp/reference/files#list); CLI [`openemail files list`](https://openemail.uk/docs/cli/reference/files#files-list).

### `Files.ListAll`

Collect every file into one slice

```go
ListAll(ctx context.Context, opts ...openemail.RequestOption) ([]openemail.Object, error)
```

Walks every page of `List` and returns with every file, in the order `sort` names. One request per page, with the same filters on each.

Scopes: `files:read`.

**Parameters**

- `openemail.WithQ` (`string`): Searches the file name and its type. Words match loosely, and a close spelling is tried when nothing matches exactly.
- `openemail.WithKind` (`string`): Keeps one kind: `image`, `pdf`, `audio`, `video` or `text`, the choices of the filter on the Files page.
- `openemail.WithDirection` (`string`): Keeps one direction: `inbound` for files that arrived on a message, `outbound` for files that went out on one, `uploaded` for files put on the Files page.
- `openemail.WithAddress` (`string`): Keeps the files of one address, the `deliveredTo` of the file, compared without regard to case.
- `openemail.WithSince` (`time.Time`): Keeps files added at or after this moment. A `time.Time`.
- `openemail.WithUntil` (`time.Time`): Keeps files added before this moment. A `time.Time`.
- `openemail.WithSort` (`string`): `newest` (the default), `oldest`, `largest` or `name`. A cursor carries on in the order it was handed out in.
- `openemail.WithLimit` (`int`): Page size for each request, from 1 to 100. The server defaults to 25.
- `openemail.WithCursor` (`string`): Starts the walk after this cursor instead of the first page.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for every page of this walk.

**Returns**

A `[]openemail.Object` holding every file.

**Example**

```go
all, err := client.Files.ListAll(ctx, openemail.WithQ("invoice"))
if err != nil {
	return err
}

for _, file := range all {
	fmt.Println(file.String("id"), file.String("object"))
}
```

**Notes**

- If any page fails the call fails and the rows already fetched are discarded.

Also available in: API [`GET /files`](https://openemail.uk/docs/api/reference/files#get-files); TypeScript [`files.listAll()`](https://openemail.uk/docs/sdk/reference/files#listAll); Python [`files.list_all()`](https://openemail.uk/docs/python/reference/files#listAll); Ruby [`files.list_all`](https://openemail.uk/docs/ruby/reference/files#listAll); PHP [`files->listAll`](https://openemail.uk/docs/php/reference/files#listAll); Java [`files().listAll`](https://openemail.uk/docs/java/reference/files#listAll); C# [`Files.ListAllAsync`](https://openemail.uk/docs/csharp/reference/files#listAll).

### `Files.Iterate`

Stream the files one at a time

```go
Iterate(ctx context.Context, opts ...openemail.RequestOption) *openemail.Iterator
```

Returns an iterator that yields one file at a time, in the order `sort` names, and requests the next page only once the current one is drained. Nothing is fetched until you consume it, and breaking out of the loop stops the requests.

Scopes: `files:read`.

**Parameters**

- `openemail.WithQ` (`string`): Searches the file name and its type. Words match loosely, and a close spelling is tried when nothing matches exactly.
- `openemail.WithKind` (`string`): Keeps one kind: `image`, `pdf`, `audio`, `video` or `text`, the choices of the filter on the Files page.
- `openemail.WithDirection` (`string`): Keeps one direction: `inbound` for files that arrived on a message, `outbound` for files that went out on one, `uploaded` for files put on the Files page.
- `openemail.WithAddress` (`string`): Keeps the files of one address, the `deliveredTo` of the file, compared without regard to case.
- `openemail.WithSince` (`time.Time`): Keeps files added at or after this moment. A `time.Time`.
- `openemail.WithUntil` (`time.Time`): Keeps files added before this moment. A `time.Time`.
- `openemail.WithSort` (`string`): `newest` (the default), `oldest`, `largest` or `name`. A cursor carries on in the order it was handed out in.
- `openemail.WithLimit` (`int`): Page size for each request, from 1 to 100. The server defaults to 25.
- `openemail.WithCursor` (`string`): Starts the walk after this cursor instead of the first page.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for every page of this walk.

**Returns**

An `*openemail.Iterator` yielding one file per step.

**Example**

```go
for file, err := range client.Files.Iterate(ctx, openemail.WithSort("oldest")).All() {
	if err != nil {
		return err
	}

	fmt.Println(file.String("createdAt"), file.String("filename"))
}
```

**Notes**

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

Also available in: API [`GET /files`](https://openemail.uk/docs/api/reference/files#get-files); TypeScript [`files.iterate()`](https://openemail.uk/docs/sdk/reference/files#iterate); Python [`files.iterate()`](https://openemail.uk/docs/python/reference/files#iterate); Ruby [`files.iterate`](https://openemail.uk/docs/ruby/reference/files#iterate); PHP [`files->iterate`](https://openemail.uk/docs/php/reference/files#iterate); Java [`files().iterate`](https://openemail.uk/docs/java/reference/files#iterate); C# [`Files.IterateAsync`](https://openemail.uk/docs/csharp/reference/files#iterate).

### `Files.Get`

Read one file by id

```go
Get(ctx context.Context, id string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Returns one file: its name, type, size, whether it was sent, received or uploaded, the address it came to, the thread and message it belongs to, and whether it can be deleted. `Download` fetches its bytes.

`deletable` is true only for an upload that nothing depends on. Otherwise `usage` says what keeps it: `received` or `sent` for a file that came in or went out on a message, `linked` for an upload that went out as a download link that still works, and `scheduled` for one attached to a message that has not gone out yet.

Scopes: `files:read`.

**Parameters**

- `id` (`string`, required): The id from `List`, `file_` and 24 hex.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object`, with `usage` and `deletable`.

**Example**

```go
file, err := client.Files.Get(ctx, "file_6bb640f5b99e47deb758f1f5")
if err != nil {
	return err
}

fmt.Println(file.String("filename"), file.String("mimeType"))
```

**Notes**

- A deleted file, one outside the addresses a narrowed key holds, and an unknown id all answer 404 `resource_not_found`.

Also available in: API [`GET /files/{id}`](https://openemail.uk/docs/api/reference/files#get-files-id); TypeScript [`files.get()`](https://openemail.uk/docs/sdk/reference/files#get); Python [`files.get()`](https://openemail.uk/docs/python/reference/files#get); Ruby [`files.get`](https://openemail.uk/docs/ruby/reference/files#get); PHP [`files->get`](https://openemail.uk/docs/php/reference/files#get); Java [`files().get`](https://openemail.uk/docs/java/reference/files#get); C# [`Files.GetAsync`](https://openemail.uk/docs/csharp/reference/files#get); CLI [`openemail files get`](https://openemail.uk/docs/cli/reference/files#files-get).

### `Files.Stats`

Count the files and the space they take

```go
Stats(ctx context.Context, opts ...openemail.RequestOption) (openemail.Object, error)
```

Returns the numbers on the Analytics tab of the Files page in one request: how many files the mailbox holds and how many bytes they take, in all and split into received, sent and uploaded, the file types and the addresses taking the most space, how many files arrived on each of the last 30 days, and the download links still working with how often they were fetched.

Deleted files are not counted. A key limited to particular addresses or domains counts only the files that arrived at them, so files uploaded for the whole workspace are left out of its numbers.

Scopes: `files:read`.

**Parameters**

- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` with `totals`, `received`, `sent` and `uploaded`, each a map with `files` and `bytes`, then `types`, `byDay`, `addresses` and `links`.

**Example**

```go
stats, err := client.Files.Stats(ctx)
if err != nil {
	return err
}

fmt.Println(stats.Object("totals").Int("files"), stats.Object("totals").Int("bytes"), stats.Object("uploaded").Int("files"))
```

**Notes**

- Needs `files:read`.
- `types` holds at most the 8 types taking the most bytes and `addresses` at most the 6 addresses holding the most. A file uploaded for the whole workspace has no address, so it counts in the totals and never in `addresses`.
- `byDay` covers the last 30 days in UTC, today included, and is sparse: a day on which no file arrived has no entry, so a chart must fill the gaps. Each `day` is `YYYY-MM-DD`.
- `links` counts the download links that still work: `shares` of them, fetched `downloads` times in all, last at `lastDownloadAt`, which is null when none was ever fetched. Scanners and link previewers are left out of the count.

Also available in: API [`GET /files/stats`](https://openemail.uk/docs/api/reference/files#get-files-stats); TypeScript [`files.stats()`](https://openemail.uk/docs/sdk/reference/files#stats); Python [`files.stats()`](https://openemail.uk/docs/python/reference/files#stats); Ruby [`files.stats`](https://openemail.uk/docs/ruby/reference/files#stats); PHP [`files->stats`](https://openemail.uk/docs/php/reference/files#stats); Java [`files().stats`](https://openemail.uk/docs/java/reference/files#stats); C# [`Files.StatsAsync`](https://openemail.uk/docs/csharp/reference/files#stats); CLI [`openemail files stats`](https://openemail.uk/docs/cli/reference/files#files-stats).

### `Files.Download`

Fetch the bytes of a file

```go
Download(ctx context.Context, id string, opts ...openemail.RequestOption) ([]byte, error)
```

Returns the file exactly as it is stored, the same bytes the Download action on the Files page saves. The response carries the file's own `Content-Type` and name.

Scopes: `files:read`.

**Parameters**

- `id` (`string`, required): The id from `List`.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

A `[]byte` holding the file.

**Example**

```go
data, err := client.Files.Download(ctx, "file_6bb640f5b99e47deb758f1f5")
if err != nil {
	return err
}

fmt.Println(len(data), "bytes")
```

**Notes**

- The whole file is held in memory, so fetch very large ones one at a time.
- A file whose bytes are gone from storage answers 404 like a missing one.

Also available in: API [`GET /files/{id}/content`](https://openemail.uk/docs/api/reference/files#get-files-id-content); TypeScript [`files.download()`](https://openemail.uk/docs/sdk/reference/files#download); Python [`files.download()`](https://openemail.uk/docs/python/reference/files#download); Ruby [`files.download`](https://openemail.uk/docs/ruby/reference/files#download); PHP [`files->download`](https://openemail.uk/docs/php/reference/files#download); Java [`files().download`](https://openemail.uk/docs/java/reference/files#download); C# [`Files.DownloadAsync`](https://openemail.uk/docs/csharp/reference/files#download); CLI [`openemail files download`](https://openemail.uk/docs/cli/reference/files#files-download).

### `Files.ListLinks`

List the download links a file went out as

```go
ListLinks(ctx context.Context, id string, opts ...openemail.RequestOption) (*openemail.Page, error)
```

Returns one page of the download links made for this file, newest first. A large attachment sent from OpenEmail travels as a link rather than inside the message, and each send makes a link of its own. Every row says how often it was fetched, when last, and whether it was revoked. While one of its links still works, an upload cannot be deleted.

A file attached inside a message has no links, because nothing records when those are opened.

Scopes: `files:read`.

**Parameters**

- `id` (`string`, required): The file id from `List`.
- `openemail.WithLimit` (`int`): Page size, from 1 to 100. The server defaults to 25.
- `openemail.WithCursor` (`string`): The `NextCursor` of the previous page. Leave it out for the first page.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

A `*openemail.Page` with `Items`, `HasMore` and `NextCursor`. Each item has `id`, `url`, `host`, `downloads`, `lastDownloadAt`, `revokedAt` and `createdAt`.

**Example**

```go
page, err := client.Files.ListLinks(ctx, "file_6bb640f5b99e47deb758f1f5")
if err != nil {
	return err
}

for _, link := range page.Items {
	fmt.Println(link.String("url"), link.Int("downloads"))
}

fmt.Println(page.HasMore, page.NextCursor)
```

**Notes**

- The count leaves out scanners and link previewers, the same classifier the `email.downloaded` webhook uses.

Also available in: API [`GET /files/{id}/links`](https://openemail.uk/docs/api/reference/files#get-files-id-links); TypeScript [`files.listLinks()`](https://openemail.uk/docs/sdk/reference/files#listLinks); Python [`files.list_links()`](https://openemail.uk/docs/python/reference/files#listLinks); Ruby [`files.list_links`](https://openemail.uk/docs/ruby/reference/files#listLinks); PHP [`files->listLinks`](https://openemail.uk/docs/php/reference/files#listLinks); Java [`files().listLinks`](https://openemail.uk/docs/java/reference/files#listLinks); C# [`Files.ListLinksAsync`](https://openemail.uk/docs/csharp/reference/files#listLinks); CLI [`openemail files list-links`](https://openemail.uk/docs/cli/reference/files#files-list-links).

### `Files.ListAllLinks`

Collect every download link of a file into one slice

```go
ListAllLinks(ctx context.Context, id string, opts ...openemail.RequestOption) ([]openemail.Object, error)
```

Walks every page of `ListLinks` and returns with every download link of the file, newest first. One request per page, with the same filters on each.

Scopes: `files:read`.

**Parameters**

- `id` (`string`, required): The file id from `List`.
- `openemail.WithLimit` (`int`): Page size for each request, from 1 to 100. The server defaults to 25.
- `openemail.WithCursor` (`string`): Starts the walk after this cursor instead of the first page.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for every page of this walk.

**Returns**

A `[]openemail.Object` holding every download link of the file.

**Example**

```go
links, err := client.Files.ListAllLinks(ctx, "file_6bb640f5b99e47deb758f1f5")
if err != nil {
	return err
}

for _, link := range links {
	fmt.Println(link.Int("downloads"))
}
```

**Notes**

- If any page fails the call fails and the rows already fetched are discarded.

Also available in: API [`GET /files/{id}/links`](https://openemail.uk/docs/api/reference/files#get-files-id-links); TypeScript [`files.listAllLinks()`](https://openemail.uk/docs/sdk/reference/files#listAllLinks); Python [`files.list_all_links()`](https://openemail.uk/docs/python/reference/files#listAllLinks); Ruby [`files.list_all_links`](https://openemail.uk/docs/ruby/reference/files#listAllLinks); PHP [`files->listAllLinks`](https://openemail.uk/docs/php/reference/files#listAllLinks); Java [`files().listAllLinks`](https://openemail.uk/docs/java/reference/files#listAllLinks); C# [`Files.ListAllLinksAsync`](https://openemail.uk/docs/csharp/reference/files#listAllLinks).

### `Files.IterateLinks`

Stream the download links of a file one at a time

```go
IterateLinks(ctx context.Context, id string, opts ...openemail.RequestOption) *openemail.Iterator
```

Returns an iterator that yields one link at a time, newest first, and requests the next page only once the current one is drained. Nothing is fetched until you consume it, and breaking out of the loop stops the requests.

Scopes: `files:read`.

**Parameters**

- `id` (`string`, required): The file id from `List`.
- `openemail.WithLimit` (`int`): Page size for each request, from 1 to 100. The server defaults to 25.
- `openemail.WithCursor` (`string`): Starts the walk after this cursor instead of the first page.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for every page of this walk.

**Returns**

An `*openemail.Iterator` yielding one link per step.

**Example**

```go
for link, err := range client.Files.IterateLinks(ctx, "file_6bb640f5b99e47deb758f1f5").All() {
	if err != nil {
		return err
	}

	fmt.Println(link.String("url"), link.String("revokedAt"))
}
```

**Notes**

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

Also available in: API [`GET /files/{id}/links`](https://openemail.uk/docs/api/reference/files#get-files-id-links); TypeScript [`files.iterateLinks()`](https://openemail.uk/docs/sdk/reference/files#iterateLinks); Python [`files.iterate_links()`](https://openemail.uk/docs/python/reference/files#iterateLinks); Ruby [`files.iterate_links`](https://openemail.uk/docs/ruby/reference/files#iterateLinks); PHP [`files->iterateLinks`](https://openemail.uk/docs/php/reference/files#iterateLinks); Java [`files().iterateLinks`](https://openemail.uk/docs/java/reference/files#iterateLinks); C# [`Files.IterateLinksAsync`](https://openemail.uk/docs/csharp/reference/files#iterateLinks).

### `Files.CreateLink`

Publish a file at a public link

```go
CreateLink(ctx context.Context, id string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Makes a public download link for the file and returns with it. Anybody holding `url` can open the file without signing in, which is how an image or a document is referenced from an email or a web page. Each call makes a new link with its own download count, and a link keeps working until it is revoked with `RevokeLink` or the file is deleted.

The link lives on the files host of `openemail.WithDomain` when that domain has one set up, such as `files.acme.com`, else on the files host of the address the file belongs to, else on the OpenEmail API address. A file that came in or went out on a message is copied to public storage first. Programs and scripts are refused with 422 `file_unshareable`.

Scopes: `files:write`.

**Parameters**

- `id` (`string`, required): The file id from `List`.
- `openemail.WithDomain` (`string`): The domain whose files host serves the link, such as `acme.com`.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` with `id`, `url`, `host`, `downloads`, `lastDownloadAt`, `revokedAt` and `createdAt`.

**Example**

```go
link, err := client.Files.CreateLink(ctx, "file_6bb640f5b99e47deb758f1f5", openemail.WithDomain("acme.com"))
if err != nil {
	return err
}

fmt.Println(link.String("url"))
```

**Notes**

- Needs `files:write`.
- Not retried after a lost response, because a second call makes a second link. Look for it with `ListLinks` first.

Also available in: API [`POST /files/{id}/links`](https://openemail.uk/docs/api/reference/files#post-files-id-links); TypeScript [`files.createLink()`](https://openemail.uk/docs/sdk/reference/files#createLink); Python [`files.create_link()`](https://openemail.uk/docs/python/reference/files#createLink); Ruby [`files.create_link`](https://openemail.uk/docs/ruby/reference/files#createLink); PHP [`files->createLink`](https://openemail.uk/docs/php/reference/files#createLink); Java [`files().createLink`](https://openemail.uk/docs/java/reference/files#createLink); C# [`Files.CreateLinkAsync`](https://openemail.uk/docs/csharp/reference/files#createLink); CLI [`openemail files create-link`](https://openemail.uk/docs/cli/reference/files#files-create-link).

### `Files.RevokeLink`

Revoke a public link

```go
RevokeLink(ctx context.Context, id string, linkID string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Stops a public link from working, for good, including in mail that already went out with it, and returns with the link, `revokedAt` set. Revoking a link that is already revoked returns it unchanged, so the call is safe to repeat. An unknown link is a 404.

Scopes: `files:write`.

**Parameters**

- `id` (`string`, required): The file id from `List`.
- `linkID` (`string`, required): The link id from `ListLinks` or `CreateLink`.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` with `revokedAt` set.

**Example**

```go
link, err := client.Files.RevokeLink(ctx, "file_6bb640f5b99e47deb758f1f5", "shr_2f9c4a1e7b3d")
if err != nil {
	return err
}

fmt.Println(link.String("id"), link.String("object"))
```

**Notes**

- Needs `files:write`.

Also available in: API [`DELETE /files/{id}/links/{linkId}`](https://openemail.uk/docs/api/reference/files#delete-files-id-links-linkid); TypeScript [`files.revokeLink()`](https://openemail.uk/docs/sdk/reference/files#revokeLink); Python [`files.revoke_link()`](https://openemail.uk/docs/python/reference/files#revokeLink); Ruby [`files.revoke_link`](https://openemail.uk/docs/ruby/reference/files#revokeLink); PHP [`files->revokeLink`](https://openemail.uk/docs/php/reference/files#revokeLink); Java [`files().revokeLink`](https://openemail.uk/docs/java/reference/files#revokeLink); C# [`Files.RevokeLinkAsync`](https://openemail.uk/docs/csharp/reference/files#revokeLink); CLI [`openemail files revoke-link`](https://openemail.uk/docs/cli/reference/files#files-revoke-link).

### `Files.RevokeAllLinks`

Revoke every link to a file

```go
RevokeAllLinks(ctx context.Context, id string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Makes the file private, as Make private on the Files page does: every public download link to it stops working at once, including the ones in mail that already went out. Returns `revoked`, how many links were still working. The file itself stays, and `CreateLink` can publish it again at a new link.

A key limited to particular addresses reaches only the files that arrived at them, and any other file is a 404.

Scopes: `files:write`.

**Parameters**

- `id` (`string`, required): The file id from `List`.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` with `fileId` and `revoked`, the number of links that stopped working.

**Example**

```go
revocation, err := client.Files.RevokeAllLinks(ctx, "file_6bb640f5b99e47deb758f1f5")
if err != nil {
	return err
}

fmt.Println(revocation.Int("revoked"))
```

**Notes**

- A file with no working links returns `revoked: 0`, so the call is safe to repeat.
- Needs `files:write`.

Also available in: API [`DELETE /files/{id}/links`](https://openemail.uk/docs/api/reference/files#delete-files-id-links); TypeScript [`files.revokeAllLinks()`](https://openemail.uk/docs/sdk/reference/files#revokeAllLinks); Python [`files.revoke_all_links()`](https://openemail.uk/docs/python/reference/files#revokeAllLinks); Ruby [`files.revoke_all_links`](https://openemail.uk/docs/ruby/reference/files#revokeAllLinks); PHP [`files->revokeAllLinks`](https://openemail.uk/docs/php/reference/files#revokeAllLinks); Java [`files().revokeAllLinks`](https://openemail.uk/docs/java/reference/files#revokeAllLinks); C# [`Files.RevokeAllLinksAsync`](https://openemail.uk/docs/csharp/reference/files#revokeAllLinks); CLI [`openemail files revoke-all-links`](https://openemail.uk/docs/cli/reference/files#files-revoke-all-links).

### `Files.Upload`

Upload a file to the Files page

```go
Upload(ctx context.Context, data io.Reader, filename string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Stores the bytes as a new file, the way the Upload button on the Files page does, and returns with it: `direction` is `uploaded`, `usage` is null and `deletable` is true. Attach it to a send as a map with `fileId` in `Emails.Send`, which is how a file larger than the inline cap goes out.

`data` is an `io.Reader`, sent as the request body. The name travels as the `filename` query parameter, and it is cleaned rather than refused: characters a file name cannot hold become `_`, and a name longer than 255 characters is cut, keeping its extension. The type is `openemail.WithContentType`, and `application/octet-stream` without it. A value that is not shaped like a MIME type is stored as `application/octet-stream` too.

A key limited to particular addresses or domains uploads to the first address it holds, which becomes `deliveredTo`. An unlimited key uploads for the whole workspace and `deliveredTo` is null, so a narrowed key never reaches that file afterwards.

Scopes: `files:write`.

**Parameters**

- `data` (`io.Reader`, required): The file: an `io.Reader`, not empty and at most 100 MB.
- `filename` (`string`, required): The name the file is stored and downloaded under, such as `report.pdf`. A missing or blank name is refused before anything is sent.
- `openemail.WithContentType` (`string`): The MIME type, such as `application/pdf`. Left out, it is `application/octet-stream`.
- `openemail.WithTimeout` (`time.Duration`): How long the upload may take. Left out, it is 10 minutes, or the client timeout when that is longer. `0` waits as long as it takes.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` for the new file, with `direction: 'uploaded'`, `usage: null` and `deletable: true`.

**Example**

```go
source, err := os.Open("report.pdf")
if err != nil {
	return err
}

defer source.Close()

file, err := client.Files.Upload(ctx, source, "report.pdf", openemail.WithContentType("application/pdf"))
if err != nil {
	return err
}

fmt.Println(file.String("id"))
```

**Notes**

- Needs `files:write`, the same scope that deletes files.
- The SDK does not retry an upload, because a second attempt after a lost response could store the file twice. Look for it with `List` before trying again.
- An empty file is a 400 `upload_empty`, a program or script, judged by its name, is a 400 `upload_dangerous`, and a file over 100 MB is a 413 `upload_too_large`. A key limited to addresses that holds none is a 403 `upload_no_address`.
- A workspace keeps up to 10 GB of uploads, and past that a 507 `upload_storage_full` holds until some are deleted. It takes 500 uploads an hour, deleted ones included, and more is a 429 `upload_rate_limited`. A 502 `upload_failed` means storage failed and nothing was kept, so it is safe to try again.

Also available in: API [`POST /files`](https://openemail.uk/docs/api/reference/files#post-files); TypeScript [`files.upload()`](https://openemail.uk/docs/sdk/reference/files#upload); Python [`files.upload()`](https://openemail.uk/docs/python/reference/files#upload); Ruby [`files.upload`](https://openemail.uk/docs/ruby/reference/files#upload); PHP [`files->upload`](https://openemail.uk/docs/php/reference/files#upload); Java [`files().upload`](https://openemail.uk/docs/java/reference/files#upload); C# [`Files.UploadAsync`](https://openemail.uk/docs/csharp/reference/files#upload); CLI [`openemail files upload`](https://openemail.uk/docs/cli/reference/files#files-upload).

### `Files.Delete`

Delete an uploaded file for good

```go
Delete(ctx context.Context, id string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Removes an uploaded file: its bytes are deleted from storage and it leaves the Files page. The row stays, marked deleted, so the index knows the file existed. There is no undo.

Only an upload that nothing depends on can be deleted, the files whose `deletable` is true. A file that was received or sent stays with its message, and so does an upload that went out as a download link that still works or is attached to a message that has not gone out yet. Those are refused with 409 `file_in_use`, and the message names the file and says why. An upload that was sent inside a message can still be deleted, because the message keeps its own copy, which is listed as a separate `sent` file.

`DeleteMany` deletes up to 100 in one call and reports the ones it kept instead of failing.

Scopes: `files:write`.

**Parameters**

- `id` (`string`, required): The id from `List`.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` with `object` set to `file`, `id` and `deleted` set to `true`.

**Example**

```go
removed, err := client.Files.Delete(ctx, "file_6bb640f5b99e47deb758f1f5")
if err != nil {
	return err
}

fmt.Println(removed.Bool("deleted"))
```

**Notes**

- Needs `files:write`, the same scope that uploads. A key limited to some addresses may delete only files that arrived at them, so an upload made for the whole workspace answers it 404.
- A file that is unknown, already deleted or out of reach answers 404 `resource_not_found`. A file in use answers 409 `file_in_use` with `param: 'id'`, and its `usage` on `Get` says which rule keeps it.
- The SDK does not retry a delete. A 404 on your own second attempt after a lost response means the first one worked.

Also available in: API [`DELETE /files/{id}`](https://openemail.uk/docs/api/reference/files#delete-files-id); TypeScript [`files.delete()`](https://openemail.uk/docs/sdk/reference/files#delete); Python [`files.delete()`](https://openemail.uk/docs/python/reference/files#delete); Ruby [`files.delete`](https://openemail.uk/docs/ruby/reference/files#delete); PHP [`files->delete`](https://openemail.uk/docs/php/reference/files#delete); Java [`files().delete`](https://openemail.uk/docs/java/reference/files#delete); C# [`Files.DeleteAsync`](https://openemail.uk/docs/csharp/reference/files#delete); CLI [`openemail files delete`](https://openemail.uk/docs/cli/reference/files#files-delete).

### `Files.DeleteMany`

Delete up to 100 files in one call

```go
DeleteMany(ctx context.Context, ids []string, opts ...openemail.RequestOption) (openemail.Object, error)
```

Deletes every file in `ids` that can be deleted, the way `Delete` deletes one, and reports the rest instead of failing. It is what selecting several files on the Files page and pressing Delete does.

Only an uploaded file that nothing depends on can be deleted. A file that was received or sent stays with its message, and so does an upload that went out as a download link that still works or is attached to a message that has not gone out yet. Each of those comes back in `kept` with its `usage` and a `reason` in plain words. An id that is unknown, already deleted or outside the addresses a narrowed key holds comes back in `missing`. The rule is checked again at the moment of deleting, so a file that became used in between is kept.

There is no undo.

Scopes: `files:write`.

**Parameters**

- `ids` (`[]string`, required): 1 to 100 file ids from `List`. More than 100, or none, is a 422 on `ids`. A repeated id counts once.
- `openemail.WithAPIKey` (`string`): Overrides the client API key for this call only.

**Returns**

An `openemail.Object` with `object` set to `file_batch_delete`, `deleted`, `kept` and `missing`. `deleted` and `missing` hold ids, and each entry of `kept` is a map with `id`, `filename`, `usage` and `reason`.

**Example**

```go
result, err := client.Files.DeleteMany(ctx, []string{"file_6bb640f5b99e47deb758f1f5", "file_0c3e2a91d4b7f6058e1a2b3c"})
if err != nil {
	return err
}

fmt.Println(result.String("object"))
```

**Notes**

- Needs `files:write`, the same scope that uploads.
- A partial result is returned rather than an error, so read `kept` and `missing` to see what stayed.
- The SDK does not retry it. Calling it again after a lost response reports the files the first call deleted in `missing`.
- To clear out every upload that can go, keep the rows of `ListAll` whose `deletable` is true and pass their ids 100 at a time.

Also available in: API [`POST /files/batch-delete`](https://openemail.uk/docs/api/reference/files#post-files-batch-delete); TypeScript [`files.deleteMany()`](https://openemail.uk/docs/sdk/reference/files#deleteMany); Python [`files.delete_many()`](https://openemail.uk/docs/python/reference/files#deleteMany); Ruby [`files.delete_many`](https://openemail.uk/docs/ruby/reference/files#deleteMany); PHP [`files->deleteMany`](https://openemail.uk/docs/php/reference/files#deleteMany); Java [`files().deleteMany`](https://openemail.uk/docs/java/reference/files#deleteMany); C# [`Files.DeleteManyAsync`](https://openemail.uk/docs/csharp/reference/files#deleteMany); CLI [`openemail files delete-many`](https://openemail.uk/docs/cli/reference/files#files-delete-many).
