# Task 019 — n8n Webhooks

## Objective

Complete outbound webhook client and inbound n8n API with authentication and documented payloads.

## Scope

- Central `src/lib/n8n.ts` with retries and logging
- All four outbound env webhooks wired to domain events
- `POST /api/webhooks/n8n` inbound handler
- `N8N_INBOUND_SECRET` env var (add to env docs)
- HMAC signature optional for outbound

**Out of scope:** Building n8n workflows themselves.

## Requirements

1. Skip webhook if URL empty.
2. Standard payload envelope per `docs/09-n8n-integration.md`.
3. Inbound rejects invalid secret.
4. Idempotent inbound handlers where applicable.

## Files to create or modify

| Path | Action |
|------|--------|
| `src/lib/n8n.ts` | Enhance |
| `src/app/api/webhooks/n8n/route.ts` | Create |
| Wire services | leads, campaigns, inbox, follow-up scheduler |
| `docs/09-n8n-integration.md` | Payload schemas |
| `docs/10-environment-variables.md` | N8N_INBOUND_SECRET |
| `docs/16-changelog.md` | Entry |

## Acceptance criteria

- [ ] Test outbound webhook received in n8n test workflow
- [ ] Inbound creates lead from sample payload
- [ ] All events documented with JSON examples

## Documentation updates required

- `docs/09-n8n-integration.md`
- `docs/10-environment-variables.md`
- `docs/03-api-specification.md`
- `docs/16-changelog.md`
