# attachDocument mutation


`attachDocument` is a GraphQL mutation of the Chameleoon MAX API. Send it as `POST https://max-api.chameleoon.sk/api/v1/graphql` with your API key, see [Chameleoon MAX](/chameleoon-max).

Attaches an uploaded file to a sales order as a document of a kind. Call it after you upload
the file with `POST /api/v1/files`, for example to attach a delivery note or an invoice PDF.

The file becomes version 1 of a new document. When an active document of the same kind on the
order already has the same `externalId` (or, without one, the same `externalNumber`), the
file becomes that document's next version instead.

An invoice imported from the accounting system keeps its own PDF: attach it with
[`attachInvoiceDocument`](/chameleoon-max/mutation/attach-invoice-document).

#### Permissions

Requires the `order.write` permission, with the order in your API key's scope.

#### Repeated calls

`requestId` makes a retry after a lost response safe.

- The same `requestId` with the same input returns `applied: false` and the document as it is
  now.
- The same `requestId` with other input is `CONFLICT` with `extensions.reason`
  `REQUEST_ID_REUSED`.

#### Errors

| Code | Reason or field | When |
| --- | --- | --- |
| `CONFLICT` | `ORDER_STATUS` | The order is not `CONFIRMED`, `COMPLETED` or `CANCELLED`. |
| `CONFLICT` | `INVOICE_REFERENCE_MISMATCH` | An `INVOICE`'s `externalId` or `externalNumber` differs from the order's invoice reference. `extensions.field` names the field. |
| `CONFLICT` | `FILE_ALREADY_ATTACHED` | A version already took the file. |
| `CONFLICT` | `REQUEST_ID_REUSED` | The `requestId` was used with other input. |
| `NOT_FOUND` | field `fileId` | The file does not exist or has expired. |
| `NOT_FOUND` | field `salesOrder` | The order does not exist or your API key cannot see it. |

#### Invoice documents

A document of kind `INVOICE` is bound to the order's invoice reference
([`setSalesOrderInvoice`](/chameleoon-max/mutation/set-sales-order-invoice)).

- An `externalId` or `externalNumber` you leave out is taken from the reference.
- One that differs from the reference is `CONFLICT` with `extensions.reason`
  `INVOICE_REFERENCE_MISMATCH`.
- When a linked invoice governs the order's reference, MAX compares only the number. A
  different `externalId` is accepted and stored as given.

> An error whose path runs below this field does not mean the call failed: the document was
> attached, so do not retry. See [`Mutation`](/chameleoon-max/mutations).

## Arguments

- `input`: [`AttachDocumentInput!`](/chameleoon-max/type/attach-document-input) · required

## Returns

[`AttachDocumentResult!`](/chameleoon-max/type/attach-document-result)

## Example

**Mutation**

```graphql
mutation attachDocument($input: AttachDocumentInput!) {
  attachDocument(input: $input) {
    applied
    document {
      id
      kind
      title
      externalId
      externalNumber
      createdAt
    }
  }
}
```

**Variables**

```json
{
  "input": {
    "requestId": "example",
    "fileId": "00000000-0000-4000-8000-000000000001",
    "salesOrder": {
      "id": "00000000-0000-4000-8000-000000000001"
    },
    "kind": "INVOICE"
  }
}
```

**Response**

```json
{
  "data": {
    "attachDocument": {
      "applied": true,
      "document": {
        "id": "00000000-0000-4000-8000-000000000001",
        "kind": "INVOICE",
        "title": "example",
        "externalId": "example",
        "externalNumber": "example",
        "createdAt": "2026-01-01T00:00:00Z"
      }
    }
  }
}
```

Values are illustrative: replace IDs and codes with your own before you send the call.
