# Attach a file to a record

Use this recipe to put a file, such as a drawing, a certificate or a
customer's order, on a record in Omono, where your staff see it with the
record. You can attach files to companies, quotes, sales orders, purchase
orders, credit notes and stock items.

Every request in this recipe sends your API key in an
`Authorization: Bearer <key>` header. Paths are relative to
`https://<host>/api/v1`.

> **Note:** The document write operations are in beta. Learn more about
> [beta operations](/docs/compatibility#stable-and-beta-operations).

You need the scopes `documents:read` and `documents:write`.

## Steps

1. **Check the size.** Omono takes files up to 20 MB. It refuses a larger
   one as soon as the upload passes 20 MB, and some HTTP clients report
   that as a connection error. So check the size before you send.

2. **Upload the file.** Send `multipart/form-data` with exactly one parent
   ID, then the optional `name` and `pinned`, and the `file` part last:

   ```bash
   curl -H "Authorization: Bearer $KEY" \
     -H "Idempotency-Key: order-1042-drawing" \
     -F salesOrderId=<id> \
     -F name=drawing-rev-c.pdf \
     -F file=@drawing-rev-c.pdf \
     https://<host>/api/v1/documents
   ```

   The `file` part must be the last part. Omono stores the file as it
   arrives, so it refuses any field that comes after it. The `201` response
   returns the document, with its `id` and a `contentUrl`.

3. **Find it again.** `GET /api/v1/documents?salesOrderId=<id>` lists the
   files on the order. To download one, follow its `contentUrl`, which
   redirects to a short-lived link to the file.

4. **Rename, unpin or archive it.** Send `name`, `pinned` or `archived` in
   `PATCH /api/v1/documents/{id}`.

Omono doesn't scan files for malware, so treat the files you download as
untrusted.

## Errors to handle

| Status   | Code                                                                                                           | What to do                                                                                                                                                                             |
| -------- | -------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 409      | [`document-archived`](/docs/errors/document-archived)                                                          | You tried to rename, pin, unpin or unarchive an archived document. Archiving is one-way, so upload the file again if you need it.                                                      |
| 413      | [`payload-too-large`](/docs/errors/payload-too-large)                                                          | The file is larger than 20 MB. Nothing was stored.                                                                                                                                     |
| 415      | [`unsupported-media-type`](/docs/errors/unsupported-media-type)                                                | The request isn't `multipart/form-data`, or it's compressed.                                                                                                                           |
| 422      | [`validation-failed`](/docs/errors/validation-failed)                                                          | A field comes after the `file` part, there's no `file` part, or there isn't exactly one parent ID.                                                                                     |
| 422      | [`invalid-reference`](/docs/errors/invalid-reference)                                                          | The parent ID isn't a record in your organisation.                                                                                                                                     |
| 422      | [`invalid-filename`](/docs/errors/invalid-filename)                                                            | The name isn't a valid file name. Send another `name`.                                                                                                                                 |
| 429      | [`upload-limit-reached`](/docs/errors/upload-limit-reached)                                                    | Your organisation has uploaded its daily allowance of files. Retry after the number of seconds in `Retry-After`, when the next day starts in UTC, or ask Omono for a larger allowance. |
| 429, 503 | [`rate-limited`](/docs/errors/rate-limited), [`temporarily-unavailable`](/docs/errors/temporarily-unavailable) | Retry after the number of seconds in `Retry-After`, with the same `Idempotency-Key`.                                                                                                   |
