> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fynn.eu/llms.txt
> Use this file to discover all available pages before exploring further.

# Preview an invoice PDF

> Render the PDF of an invoice payload before you create or update the invoice.

Both endpoints on this page render a finished invoice PDF and store nothing. Use them to show the
document in your own create or edit form before the write happens: no invoice, no positions, no
file and no activity are created, and the invoice number is not consumed.

| Endpoint | Body | What you get back |
| - | - | - |
| `POST /api/invoices/preview` | the body of `POST /api/invoices` | the document `POST /api/invoices` would create from this payload |
| `POST /api/invoices/{id}/preview` | the body of `PUT /api/invoices/{id}` | the document the stored draft would look like after this update |

`GET /api/invoices/{id}/preview` stays as it is: it renders the draft as it is stored, with no body.

Both endpoints need the same permission as the write they preview (`invoice:write`).

## Response

`200 OK` with the PDF bytes:

```http theme={null}
Content-Type: application/pdf
Content-Disposition: inline; filename="preview.pdf"
Cache-Control: no-store
```

The bytes describe an invoice that does not exist yet, so the response must not be cached. In a
browser, read it as a blob and hand it to `URL.createObjectURL()`.

## Preview a new invoice

The body is the body of `POST /api/invoices`. `customerId` is required; everything else is
optional and falls back to the customer's or the environment's defaults, exactly as it does on
create. Positions may be empty, in which case you get the document with no line items.

```bash theme={null}
curl -X POST https://coreapi.io/api/invoices/preview \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -o preview.pdf \
  -d '{
    "customerId": "12345678-1234-1234-1234-123456789012",
    "currencyCode": "EUR",
    "positions": [
      {
        "name": "Wartungspauschale",
        "quantity": 2,
        "unitPrice": "49.00",
        "unitId": "12345678-1234-1234-1234-123456789012",
        "taxGroupId": "12345678-1234-1234-1234-123456789012"
      }
    ]
  }'
```

## Preview an update of an existing draft

The body is the body of `PUT /api/invoices/{id}`, and it is applied to the stored draft in memory
only. `{id}` must be a draft of your own environment; a finalized invoice is refused with the same
error `PUT /api/invoices/{id}` answers with.

> [!WARNING]
>
> Send the FULL payload, not a patch. The preview shows what the write would save, and the write
> overwrites what it receives:
>
> * an absent `positions` key removes every position, so the preview comes back without line items;
> * inside a position, an absent optional field such as `description`, `discountAmount` or
>   `serviceDateFrom` clears it.
>
> Send the same complete payload you would send to `PUT /api/invoices/{id}`, and the preview matches
> what you are about to save.

```bash theme={null}
curl -X POST https://coreapi.io/api/invoices/INVOICE_ID/preview \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -o preview.pdf \
  -d '{
    "customerId": "12345678-1234-1234-1234-123456789012",
    "title": "Rechnung {{ invoiceNumber }}",
    "introduction": "vielen Dank für deinen Auftrag.",
    "positions": [
      {
        "name": "Wartungspauschale",
        "description": "Inklusive Anfahrt",
        "quantity": 2,
        "unitPrice": "49.00",
        "unitId": "12345678-1234-1234-1234-123456789012",
        "taxGroupId": "12345678-1234-1234-1234-123456789012"
      }
    ]
  }'
```

For a payload that equals the stored draft, the answer is the same document
`GET /api/invoices/{id}/preview` returns.

## Errors

| Status | When |
| - | - |
| `403` | the API key has no `invoice:write` permission |
| `404` | no invoice with this ID in your environment (`POST /api/invoices/{id}/preview` only) |
| `422` | the payload is invalid |

A `422` carries the same field errors the write answers with, under `violations`:

```json theme={null}
{
  "title": "Validation Failed",
  "violations": [
    {
      "propertyPath": "positions[0].name",
      "message": "This value should not be blank.",
      "code": "c1051bb4-d103-4f74-8988-acbcafc7fdc3"
    }
  ]
}
```

Unknown reference IDs are field errors too, so `customerId`, `customerAddressId`,
`paymentMethodId`, `taxGroupId` and `currencyCode` come back in `violations` rather than as a late
`404`.

> [!TIP]
>
> The preview is the real document, with your letterhead, your document texts and the tax breakdown.
> Placeholders in `title`, `introduction` and `closing` are resolved, with one exception: an invoice
> gets its number when it is finalized, so a preview does not print a real invoice number.
