# Invoice operations

## Quick start

Read saved invoices with your key:

```sh
curl "$CUSTARD_API_URL/v1/invoices?limit=20" \
  --header "Authorization: Bearer $CUSTARD_API_KEY"
```

To create a private draft, replace `customerId` and `paymentMethodIds` with IDs from your customer and wallet collections. Choose a new UUID, use it in both the header and `requestId`, and keep it for retries:

```json
{
  "invoice": {
    "customerId": "91bda55d-604f-40e1-a4a6-9839e21d39b1",
    "currency": "USD",
    "issueDate": "2026-10-24",
    "dueDate": "2026-11-24",
    "items": [{ "description": "Consulting", "quantity": "1", "unitPrice": "250" }],
    "paymentMethodIds": ["738dd80d-4b78-44ce-b90e-99ad09c88fde"]
  },
  "issuance": "draft",
  "requestId": "ff0e125e-65db-49eb-b1db-b596fdf57d75"
}
```

Save that as `invoice.json` and send it:

```sh
curl "$CUSTARD_API_URL/v1/invoices" \
  --header "Authorization: Bearer $CUSTARD_API_KEY" \
  --header 'Content-Type: application/json' \
  --header 'Idempotency-Key: ff0e125e-65db-49eb-b1db-b596fdf57d75' \
  --data-binary @invoice.json
```

## Create, issue and repeat

First list or create customers and payment methods. Send `POST /invoices` with `invoice`, `issuance` and a UUID `requestId`; send the same UUID as `Idempotency-Key`.

`issuance: draft` saves a private draft. `one-time` publishes now. `recurring` creates the first private invoice and a recurring schedule; include `recurrence.frequency`, and optionally `interval`, `endDate`, `memo` and recipients. Use `schedule` to set the local start date, time, IANA timezone and delivery mode (`draft` or `email`). The response contains the first invoice and its `recurringScheduleId`.

List `/schedules`, inspect `/schedules/{id}`, PATCH its timing or template, or POST `/pause`, `/resume` or `/stop`. Pausing never builds an email backlog. Stopping changes its state to `cancelled` and leaves historical invoices on file.

## Edit and inspect

`GET /invoices` accepts `status`, `customerId` and `scheduleId`. `GET /invoices/{id}` returns the saved document, snapshots, totals, lifecycle timestamps and events. PUT replaces editable document fields; PATCH accepts the native drawer's status and receiving wallets. Paid documents are locked against content edits. Invoice numbers are retained when editing.

## Publish, email and payment status

POST `/invoices/{id}/publish` publishes a draft. `/send` publishes a draft and queues its email; `/remind` queues a reminder for an unpaid published invoice. `/deliveries/{id}/retry` handles failed deliveries, including an explicit acknowledgement for uncertain previous sends. Delivery requires configured email; acceptance does not guarantee receipt. Check `/deliveries` for queue and attempt state.

POST `/mark-paid` records a manual payment with date, method and optional memo, receiving bank/wallet and receipt. It moves no money and verifies no blockchain transaction. Stored invoice status remains `sent` while `paidAt` marks payment. POST `/void` sets status `cancelled`, displayed as Voided in the app, and cancels queued mail. Voiding one occurrence does not stop its series; use `/schedules/{id}/stop` for that. Like the native app, even a paid invoice can be voided; this does not issue a refund. `/duplicate` creates a new private draft.

## PDF and receipt files

GET `/invoices/{id}/pdf` downloads the same server-rendered, privately cached PDF used by the application. Publish the invoice first. POST `/receipts` with `name`, `contentType` and `base64` file bytes; pass its returned receipt record in a bank payment. GET `/invoices/{id}/receipt` requires access to that invoice. File downloads are streamed without a Content-Length.
