Back to work

UGC Ad Creator · Ad production automation

A product photo in, an approved ad clip out

A brand sends a photo and a one-line brief. It gets a script to approve, then a short creator-style clip.

What can’t go wrong

  • USD 0Nothing paid before approval
    How we know
    The script comes back with a cost estimate and three buttons. Until Approve is pressed, nothing is generated and nothing is charged. A cancelled request ended with no generation and no cost. Pricing and caps are managed in configuration.
  • USD 20Daily spending cap held
    How we know
    Two requests asked for a clip at the same moment with USD 19.50 already spent against a USD 20 daily cap. One went ahead and the other was stopped; the day ended at exactly USD 20.00. The cap is set in configuration.
  • 1 tryA paid step is never repeated blindly
    How we know
    When the video service does not confirm that it started a job, the request is not sent again, because that could be charged twice. The job goes to a person, and the requester is told that up to USD 0.04 may have been charged.
  • 1 runDouble tap, one generation
    How we know
    Pressing Approve a second time was refused and one generation ran. A button pressed by anyone other than the person who sent the request was refused and nothing changed.

How a request moves

  1. A photo and a brief arriveChecked before anything happens
  2. A script comes backChecked against the brand’s rules
  3. You approve, then it is madeCost shown first, caps enforced
  4. The clip is sent for a decisionAccept it or ask again

How it works, in detail

For technical reviewers: the workflows, architecture and automated checks.

TriggerStepAnswer to senderRedrawn from the build’s layout file

Verified on real n8n

29 of 29 scenarios passed on n8n 2.42.6.

  • 29/29End-to-end scenarios passed
    Details
    Run on n8n 2.42.6 on 15 Nov 2026. Each scenario posts Telegram updates, signed provider callbacks and watchdog ticks to the production webhooks and checks the answer, the calls in order, the rows, events, cost rows, replies and alerts. The scenarios took 141.6 s.
  • 68Unit checks passed
    Details
    38 workflow unit checks and 30 unit checks of the shared kit, run without n8n and without a network. The logic is plain modules inlined into the Code nodes.
  • 180Nodes in three workflows
    Details
    Take UGC ad requests 61, Generate UGC ad clips 75, Watch UGC ad jobs 44. The workflow files are generated from source by workflow/build.mjs, which checks they are fresh.
  • 0Validator errors
    Details
    n8n-mcp validate_workflow in the runtime and strict profiles on all 4 workflows, the shared error workflow included: 0 errors. The strict profile gave warnings that were reviewed and accepted. All 17 error outputs are wired.
  • 3 × 5 sRetries on every call but one
    Details
    Every HTTP call retries 3 times, 5 seconds apart, and every write carries an Idempotency-Key. The one exception is Create provider task, which has no retries because the paid call cannot be deduplicated.

Inside the automation · 180 nodes

Three workflows, one clip

Scroll the steps. Tap any node for its job, retries and what it sends.

  1. Take requests1 / 6

    401 · 400

    Secret and shape first

    How it works
    A wrong or missing secret gets 401 and no call of any kind. A broken update gets 400 and is stored once as a dead letter. When the dead letter cannot be stored on 4 tries, the sender still gets 400 and the run then fails on purpose, so the alert carries the update id and the payload hash and no payload text.
  2. Take requests2 / 6

    202

    One claim per update

    How it works
    Each Telegram update id is claimed once. A second delivery of the same update got 200 duplicate after exactly one call, the ledger claim; a delivery while the first run is still going gets 409.
  3. Take requests3 / 6

    JPEG · PNG

    Photo type read from bytes

    How it works
    A photo with a caption is a request: getFile, download, the type decided from the bytes (JPEG or PNG), a hash, and a row in jobs. Text, or a photo without a caption, gets the help line and no job. The configuration allows photos up to 10 MiB and briefs up to 200 characters.
  4. Take requests4 / 6

    2

    Two tries, then a person

    How it works
    The model answers to a JSON schema and code checks the answer again: shot counts, durations, lengths, banned claims, competitor names and the model's own compliance check, which can fail a script and never pass one. A failing script is sent back once with its problems. Two failures, in the scenarios a medical claim and a competitor name, then a flagged treatment claim, give a review item and a reply without buttons.
  5. Take requests5 / 6

    64 B

    Signed buttons, requester only

    How it works
    Button data holds the action, the job, the take or clip number and an HMAC that also covers the chat, inside Telegram's 64-byte limit. Only the requester may press; a button from an earlier take or clip, a second Approve, and a Regenerate over the limit were each refused. The configuration allows 2 revisions and 2 regenerations.
  6. Take requests6 / 6

    seq + 1

    State is the newest event

    How it works
    A job's state is its newest row in an append-only job_events table. Every transition inserts seq + 1 and the unique key (job_key, seq) lets only one writer win, so a double-pressed Approve, a watchdog expiry and a callback racing a poll each resolve to one transition. A plan lists the next writes and one hub runs each: job event, Telegram call, row, generation request, done mark.
  7. Generate clips1 / 7

    7,200 s

    A claim that outlives the run

    How it works
    One claim per job and clip. The kit's claim went stale after 600 s, shorter than a generation run, so it now takes ttl_seconds; this workflow claims with 7200 and refuses a config whose worst case (6,600 s with the default limits) would be longer.
  8. Generate clips2 / 7

    3

    Image, video or stop

    How it works
    Config, job, events and the approved script are read. Clip 1 starts at the image stage, a regeneration starts at the video stage from the same key frame, and anything else stops.
  9. Generate clips3 / 7

    0

    Bot token never leaves n8n

    How it works
    A Telegram file URL contains the bot token, so it is never handed to the generator. The workflow downloads the photo and sends it to the provider's base64 upload, which returns the URL the image task uses. Rows hold file ids and hashes, never photo bytes.
  10. Generate clips4 / 7

    409

    Reserve before every paid step

    How it works
    A run reads the job's and the day's budget entries, checks both caps against exactly that state and inserts its reservation at the day's next seq. The unique key (day, seq) makes that insert a compare-and-set: the run that loses gets 409, reads again and is stopped at the cap. Against the first build, the same race check made 2 clip tasks from USD 19.50 against a USD 20 cap.
  11. Generate clips5 / 7

    0 retries

    The paid call is never retried

    How it works
    createTask costs money, takes no idempotency key and can be looked up only by the id it returns, so a retry after a lost answer would create and charge a second task. An intent row is written first. A clear refusal is handled by the workflow's own bounded loop (a 429 waits Retry-After and asks again). No answer, a 5xx or a 200 without a task id puts the job in needs_review with the reservation kept. It is the one node with no 3 retries, declared in the manifest.
  12. Generate clips6 / 7

    900 s

    Polling stops at a deadline

    How it works
    Exponential backoff to a per-stage deadline, all from config (defaults: 3 s, factor 1.6, 30 s cap, 300 s for the key frame, 900 s for the clip). Each round first checks that the job still waits for this run and whether a callback already recorded the result. In n8n 2.42.6 the Wait node sleeps in-process below 65 s, so both Wait nodes wait until a time the Code node before them computes.
  13. Generate clips7 / 7

    1

    A result used once

    How it works
    A success counts only with a result URL on an allowed https host; done without one is a failure, retried once. Content-policy refusals are not retried. Results are recorded once per provider task id, so a callback and a poll racing each other give one result and one delivery.
  14. Watch jobs1 / 2

    300 s

    Signed callbacks, recorded once

    How it works
    The provider signs over the task id and timestamp only, not the body, and a timestamp more than 300 s old is refused with 401. Is it our task, and is it recorded already: the answer is 200 recorded, 200 duplicate or 200 unknown task, and 503 when it could not be stored.
  15. Watch jobs2 / 2

    5 min

    Stuck jobs closed once

    How it works
    Every 5 minutes, or on a signed tick, jobs in generating_image or generating_video past the stage limit (480 s and 1,200 s in the configuration) are confirmed as still the newest event and expired at seq + 1 as a compare-and-set, and the requester is told. In the end-to-end runs the schedule was off and the sweep ran from the signed tick.

Watch a recorded run travel

Eight scenarios from the end-to-end run. Every path is built from the logged calls.

A script, a cost estimate, three buttons

What was checked
Answered 202 in 120 ms. The message ends “Estimated cost: USD 0.04 key frame + USD 0.50 clip = USD 0.54 … Nothing is generated or charged until you tap Approve.” No provider call is made before Approve.
Requestanswered202
53/53+403 ms
  1. Receive Telegram update
  2. Verify secret token
  3. Route on secret
  4. Validate update shape
  5. Route by update shape
  6. Prepare update claim
  7. Claim updateclaimed200+0 ms
  8. Read claim result
  9. Route on claim
  10. Respond 202 acceptedanswer202
  11. Fetch ad config200+34 ms
  12. Classify update
  13. Route by request kind
  14. Prepare file lookup
  15. Get Telegram file200+71 ms
  16. Read file path
  17. Download product photo200+90 ms
  18. Check product photo
  19. Route photo check
  20. Store job row201+124 ms
  21. Prepare new job plan
  22. Pick next step
  23. Route next step
  24. Prepare job event
  25. Store job event201+171 ms
  26. Read job event
  27. Pick next step
  28. Route next step
  29. Prepare script request
  30. Compose script200+215 ms
  31. Check script
  32. Store script row201+237 ms
  33. Read script row
  34. Route script verdict
  35. Pick next step
  36. Route next step
  37. Prepare job event
  38. Store job event201+280 ms
  39. Read job event
  40. Pick next step
  41. Route next step
  42. Prepare Telegram call
  43. Send Telegram call200+328 ms
  44. Read Telegram answer
  45. Pick next step
  46. Route next step
  47. Prepare job event
  48. Store job event201+365 ms
  49. Read job event
  50. Pick next step
  51. Route next step
  52. Prepare done mark
  53. Mark update done200+403 ms

Build notes

What came up during the build, and what changed because of it.

A retry that could pay twice

What happened
n8n reruns a node when its first output item has a top-level error field, so the paid createTask call was retried after a timeout like any other. The provider's docs show no client id, no idempotency key and no list or search call, so a lost answer cannot be resolved. The call now runs with no retries, an intent row is written before it, and an unclear answer puts the job in manual review with the reservation kept. Against the first build, the new check saw createTask called 3 times and 3 tasks made.

A daily cap two jobs could overspend

What happened
The check read the day's spend and the write came later, with nothing tying them together. Every paid step now reserves its amount at the day's next sequence number under a unique key, and the run that loses reads again. Against the first build, two clips ran from USD 19.50 against a USD 20 cap. The losing run's 409 was then retried 4 times by n8n until the node returned the full response, so the body sits under body.

URL is not defined in a Code node

What happened
The first end-to-end run failed the happy path: the result-host check used new URL(), and n8n's JavaScript task runner has no URL global, so the check failed closed. The unit checks had passed because Node has URL. It is now a plain parse with a unit check.

A claim that went stale mid-run

Why
The shared kit's claim went stale after 10 minutes and a generation run can take longer. The claim now takes ttl_seconds (60 to 7200); the generation workflow claims with 7200 and refuses a config whose worst case is longer.

State as inserts, not updates

Why
The kit's data service has row insert and exact-match find but no update. A job's state is the newest row of an append-only table, and the unique key (job_key, seq) turns each transition into a compare-and-set. The same idea serves the watchdog and the poll-or-callback race.

A failed dead-letter write was silent

What happened
When the dead letter could not be stored, the error output went straight to the 400. Now the sender still gets the 400, then a node fails the run on purpose so the error workflow alerts with the update id and the payload hash. Against the first build the check waited 60 s for an alert that never came.

Code, build log and full results

Next

Trench Desk

Trench Desk