# Invoice Ninja webhooks in production: duplicates, ordering, retries and timeouts

Agent skill: `npx skills add hookdeck/webhook-skills --skill invoice-ninja-webhooks` ([details](https://hookdeck.com/webhooks/invoice-ninja/skill))

This guide covers the problems Invoice Ninja webhooks cause in production and how to handle each one. What Invoice Ninja sends is on the [Invoice Ninja webhooks](/webhooks/invoice-ninja) overview, every event key is on [Invoice Ninja webhook events](/webhooks/invoice-ninja/events), and the secret header check is on [Verify Invoice Ninja webhooks](/webhooks/invoice-ninja/verify).

Invoice Ninja's public API reference documents the subscription fields but not how deliveries behave. Invoice Ninja is open source, so the delivery details here come from its version 5 server code. That code can change between releases without a docs update, so check them against your own version if you self-host.

## At a glance

| Problem | What to do | With Event Gateway |
| --- | --- | --- |
| [Timeouts](#timeouts) | Return a `200` within 30 seconds; process in a background job | Accepts the delivery at once, then queues it for your service |
| [Duplicates](#duplicates) | Upsert on the record's `id` and compare `updated_at` | Can drop repeats before they reach your handler |
| [Ordering](#ordering) | Ignore bodies older than what you've stored | Logs every event, so you can see what arrived when |
| [Retries](#retries) | Never answer `404`, `405` or `410`; reconcile after incidents | Accepts deliveries while your service is down and replays them |
| [Choosing events](#choosing-events) | Give each subscription its own path or `?event=` value | Filters and routes on the path or query string |
| [Testing](#testing) | Create real records, and resend with the retry call | Forwards to localhost and replays past events |
| [Other limits](#other-limits) | Re-read records that move money; check your plan and version | Checks the secret header on every request |

## Invoice Ninja webhook timeouts and slow handlers {#timeouts}

Invoice Ninja allows 10 seconds to connect and 30 seconds for the whole request. Store the record, return a `200`, and do the rest in a background job.

**Why it happens:** A request that hits either limit counts as a connection failure, so Invoice Ninja sends it again, even if your handler went on to finish. Work that fails before you respond is worse: a `500` isn't retried, so the event is gone.

**Fix it in your handler:**

1. Check the secret header, store the body and return a `200`.
2. Process the record in a worker. See [why to process webhooks asynchronously](/webhooks/guides/why-implement-asynchronous-processing-webhooks).
3. Keep calls to other services out of the request path.

**With Event Gateway:** Hookdeck accepts each Invoice Ninja delivery as it arrives, then queues it and delivers it to your service with its own retries. Your handler's speed no longer affects Invoice Ninja's timeout.

## Duplicate Invoice Ninja webhooks {#duplicates}

The same record can reach you more than once, and there's no delivery ID to spot it with. Upsert on the record's `id` and compare `updated_at`.

**Why it happens:** A delivery that succeeds on your side but times out on Invoice Ninja's is sent again. Separately, `update_*` events fire whenever a record is saved with changes, so one action in the app can produce several deliveries, and a single action can fire more than one event, such as `update_invoice` and `sent_invoice`.

**Fix it in your handler:**

1. Key your writes on the record's `id`, and use the event name, `id` and `updated_at` together as an idempotency key for side effects. See our [guide to webhook idempotency](/webhooks/guides/implement-webhook-idempotency).
2. Treat the body as the record's current state rather than a change to apply.
3. Return a `200` for a repeat, so a duplicate doesn't look like a failure.

**With Event Gateway:** [Deduplication](/docs/deduplication) can compare the fields you choose, such as `id` and `updated_at`, and drop repeats within a time window before they reach your handler.

## Out-of-order Invoice Ninja webhooks {#ordering}

Invoice Ninja makes no promise about order, and the code gives events different delays. Compare `updated_at` with what you've stored before you apply a body.

**Why it happens:** Each event is a queued job. Payment events wait 20 to 25 seconds in the queue, client events 2 seconds and most others no time at all, so an event from just after a payment can arrive before its `create_payment`. Retries add further delay. The job also reloads the record when it runs, so the body shows the record at send time. A `create_invoice` body can already hold later edits.

**Fix it in your handler:**

1. Store `updated_at` with each record, and ignore a body with an older value. It's in whole seconds, so treat an equal value as new.
2. Handle a child record that arrives first, such as a payment for an invoice you haven't stored, by fetching the parent from the API.

**With Event Gateway:** Hookdeck logs every event and attempt, so you can see the order things arrived in when you're debugging.

## Invoice Ninja webhook retries {#retries}

Invoice Ninja retries connection errors, timeouts and `429`, up to 5 tries in all. It drops every other failure, and a `404`, `405` or `410` also archives the subscription.

**Why it happens:** Retries come about 10 to 15 seconds, 30 to 40 seconds, 1 minute and 3 minutes apart, so the whole schedule lasts about 5 minutes. Every `5xx` and every other `4xx` is logged and dropped at once. Invoice Ninja treats `404`, `405` and `410` as a dead URL and soft-deletes the subscription, the same state as archiving, so it stops sending. A route that only accepts `POST` while the subscription uses `PUT`, a deploy that briefly removes the route, or a proxy that answers `404` for an unknown path is enough.

**Fix it in your handler:**

1. Use `200` for success, `401` for bad credentials and `400` for a bad body. Use `429` only when you want a retry.
2. Accept the subscription's method on the route, and check that your framework and proxies can't answer `404`, `405` or `410` for it.
3. After deploys and incidents, check Invoice Ninja's system log and look for archived webhooks. Restore one in the app, or with `POST /api/v1/webhooks/bulk` and the `restore` action.
4. Reconcile against the Invoice Ninja API after an outage, because dropped events aren't sent again.

**With Event Gateway:** Hookdeck accepts Invoice Ninja's deliveries while your service is down and keeps them, so a `500` from your service becomes a retry on a schedule you control, and a missing route shows up as a failed delivery you can replay. If a subscription uses `PUT`, allow `PUT` on the source before you save the subscription, because the source type pre-selects `POST`.

## Choosing which Invoice Ninja events to handle {#choosing-events}

A subscription has exactly one event, and the event isn't in the request. Create a subscription for each event you need, each with its own path or `?event=` value.

**Why it happens:** The event key belongs to the subscription, and the body is just the record. Invoice Ninja adds no event header or field, so a single URL can't tell a `create_invoice` from an `update_invoice`.

**Fix it in your handler:**

1. Route on the URL, such as `https://example.com/webhooks/invoice-ninja?event=create_invoice`, and keep a table of which subscription points where.
2. Start with the events that drive your workflow, such as `sent_invoice`, `late_invoice`, `create_payment` and `approve_quote`. `late_invoice` comes from a daily check, for invoices with a balance whose due date was the day before.
3. Don't rely on `expired_quote`. It's a valid key, but the v5 server has no active code that sends it. Compare a quote's `due_date` yourself instead.

**With Event Gateway:** Hookdeck [filters](/docs/filters) can route on the path or query string, so one source can send each subscription's events to a different destination.

## Testing Invoice Ninja webhooks {#testing}

Invoice Ninja documents no test event and publishes no example deliveries, so trigger real events on a test company and keep what you receive as fixtures.

**Why you need a plan:** The body is whatever the record's transformer produces, so you can't see the shape in advance. On the hosted service, a webhook on an unpaid account never fires, which looks exactly like a broken endpoint.

**Test it safely:**

1. Use a paid hosted account or a self-hosted install. Target URLs on private addresses are rejected, so use a public URL or a tunnel.
2. Create or edit a record in the app, and log the body for each event you handle before you write a parser for it.
3. Resend one record with `POST /api/v1/webhooks/{id}/retry`, passing `entity` and `entity_id`. It's in the server's routes but not in the API reference, and it sends the record as it is now.
4. Read the outcome in Invoice Ninja's system log, which records each delivery's result.

**With Event Gateway:** The [Hookdeck CLI](/docs/cli) receives webhooks on your machine without a public server, and you can replay past events to your handler as you change it.

## Other Invoice Ninja webhook limits {#other-limits}

Webhooks aren't signed, so plan for what a leaked header value can do. Check your plan and your server version too.

**A static header, not a signature.** The custom header is the same on every request, so anyone who learns it can send convincing invoices or payments to your endpoint, and a captured request can be replayed. Use a long random value over HTTPS. For payments and anything else that moves money, re-read the record from the Invoice Ninja API by `id` before you act on it.

**Plan and permissions.** Only admins on an account with the API feature can create subscriptions, and the hosted service only sends webhooks for paid accounts.

**No source IPs.** None are published, and self-hosted installs send from their own host, so you can't allowlist by address.

**Behaviour depends on your version.** If you self-host, delivery works the way your installed version's code does, so re-check it when you upgrade.

**A separate webhook on Subscriptions.** Subscription products have their own webhook setting, which fires when a client signs up through the link. It's configured apart from the API webhooks in this guide.

**With Event Gateway:** Hookdeck's Invoice Ninja source type compares the header you name with the exact value you enter, and records a request without it as failed verification, so it never reaches your destination.