# attachInvoiceDocument mutation


`attachInvoiceDocument` 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 an invoice imported from the accounting system, as the invoice's
own PDF. Call it after you upload the accounting system's PDF with `POST /api/v1/files`.

An invoice has one PDF, keyed on the invoice and never on a number. The file becomes version 1
of a new document. When the invoice already has an active document of that kind, the file
becomes that document's next version instead.

The document's kind and keys come from the invoice:

- An `INVOICE` gives an `INVOICE` document, a `CREDIT_NOTE` a `CREDIT_NOTE` document, and an
  `ADVANCE` a `PROFORMA_INVOICE` document.
- `externalNumber` is the invoice's number, and `externalId` is its id in its accounting
  system. Every new version brings them up to date.

It returns the same result as [`attachDocument`](/chameleoon-max/mutation/attach-document).

#### Permissions

- Requires the `invoice.sync` permission, with the invoice in your API key's scope.
- The upload (`POST /api/v1/files`) requires `invoice.sync` (or `order.write`), or
  `email.send`.
- Reading the document back ([`document`](/chameleoon-max/query/document), `Invoice.documents`) requires
  `invoice.read`.

#### Repeated calls

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

- The same `requestId` with the same input (the `invoice` reference as you sent it) returns
  `applied: false` and the document as it is now.
- The same `requestId` with other input is `CONFLICT` with `extensions.reason`
  `REQUEST_ID_REUSED`.
- The same bytes as the current version return `applied: false`, and nothing changes.

#### Errors

| Code | Reason or field | When |
| --- | --- | --- |
| `CONFLICT` | `INVOICE_REMOVED` | The invoice was removed in its accounting system. A cancelled invoice is accepted. |
| `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 `invoice` | The invoice does not exist or your API key cannot see it. |
| `VALIDATION` | field `invoice` | The reference names both an `id` and a `source` key, or neither. |

> 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`: [`AttachInvoiceDocumentInput!`](/chameleoon-max/type/attach-invoice-document-input) · required

## Returns

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

## Example

**Mutation**

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

**Variables**

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

**Response**

```json
{
  "data": {
    "attachInvoiceDocument": {
      "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.
