# n8n Workflow Trigger

## Objective

Trigger self-hosted or cloud n8n workflows from product events with signed payloads.

An outbound trigger to user-supplied n8n webhook URLs, with shared-secret signing, versioned payloads, and a bounded wait for responding workflows.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Treat every destination as untrusted because self-hosted n8n means the user picks the host. Resolve the hostname before each connection, reject private, loopback, link-local, and metadata addresses, and re-check on redirect rather than only when the URL is saved.
2. Offer an optional shared secret per destination. When it is set, sign the request body with it and include a timestamp in the signed material so the workflow can verify origin and reject replays.
3. Version the payload for each event type and include the version in the body. Add fields without removing old ones, and treat any removal or rename as a new version so live workflows keep parsing what they were built against.
4. Bound the wait for workflows that respond synchronously. Apply a short connect and read timeout, treat anything past it as an unacknowledged delivery, and never hold a user-facing request or a job worker open waiting on someone else's server.
5. Reuse the shared outbound delivery pipeline from the Zapier Trigger Webhook brief for queueing, retry, logging, and auto-disable. This brief owns only the SSRF checks, the shared-secret signing option, and the synchronous-response handling.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- A self-hosted URL is the most direct route to server-side request forgery this app will ever offer. Validate the scheme and resolved address on every attempt, not once at save time, because DNS can be repointed at an internal host afterwards.
- Not every user will configure a secret, and an unsigned payload cannot be trusted by the receiver. Support an optional shared secret, make its absence visible in the settings screen as a reduced-assurance state, and allow rotating it without recreating the integration.
- A workflow written against last quarter's payload will break on an unannounced field change. Keep schemas versioned, publish the current version in the body, and keep at least one prior version working through a stated deprecation window.
- A workflow that responds synchronously can hang for as long as its slowest node. Enforce a timeout, release the worker, and record the delivery as sent-but-unacknowledged rather than blocking the app indefinitely.
- Retrying a delivery whose workflow already ran can duplicate side effects such as sending a message or charging a customer. Retry only where the delivery was not acknowledged, carry a stable delivery identifier so the workflow can deduplicate, and do not retry a workflow that returned a definitive rejection.
- A self-hosted instance behind a home connection will be intermittently unreachable. Back off with jitter, cap the retry window, and disable the destination after a sustained run of failures with a clear message to the owner.
- A user-supplied host may present an invalid or self-signed certificate. Require a valid chain by default, and if an override is offered at all, scope it to the single destination and make the reduced security explicit.
- When n8n is unavailable, the app must continue working normally. Show the integration as degraded with the last successful delivery time, and never surface the third party's failure as an error on the user's own action.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Destination hosts are resolved and validated on every attempt, and internal-network targets are refused.
- [ ] An optional shared secret signs the payload with a timestamp, and can be rotated in place.
- [ ] Payload schemas are versioned, the version travels in the body, and prior versions keep working through a deprecation window.
- [ ] Synchronous workflows are bounded by a timeout that never blocks a user request or holds a worker open.
- [ ] Only unacknowledged deliveries are retried, and each carries a stable delivery identifier.
- [ ] An unreachable n8n instance leaves the app fully usable and the integration visibly degraded.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
