PTENES
MODULE 4.2

🔁 First end-to-end automation

Now you build a real workflow: from the trigger to a visible action, with AI in the middle. Trigger via webhook or schedule, collect the input, call the LLM, parse the output, execute the action, and review the execution.

6
Topics
50
Minutes
Practical
Level
Hands-on practice
Type
Trigger Collect AI / LLM Parse Action Observe

Illustrative — the six steps in this module, in the order they come together into a single workflow.

1

⚡ Trigger

O trigger determines the nature of the automation. A Webhook makes it reactive (someone sends data and it responds); a Schedule makes it proactive (runs on its own at set times).

✓ Webhook (reactive)

  • ✓Form submitted, payment approved, message received.
  • ✓Real-time response to the event.
  • ✓You receive a payload that’s ready to use.

✓ Schedule (proactive)

  • ✓Daily report, email scan every 15 min.
  • ✓Doesn’t depend on anyone triggering it.
  • ✓Scheduled by cron: 0 9 * * 1-5 (9 a.m. on business days).
# payload received by a Webhook trigger
POST /webhook/novo-lead
{ "nome": "Ana", "email": "ana@x.com", "mensagem": "Quero um orçamento" }
// disponível no fluxo como: ={{ $json.body.email }}
2

📥 Collect input data

Before calling the AI, extract and normalize the fields that matter. The quality of the model's response is proportional to the context you provide — garbage in, garbage out.

# Set node — building a clean context
{
  "nome":     "={{ $json.body.nome }}",
  "email":    "={{ $json.body.email.toLowerCase() }}",
  "mensagem": "={{ $json.body.mensagem.trim() }}"
}

📊 Why normalize at the input

  • Consistency - lowercase email prevents duplicates when saving.
  • Token savings - sending only the useful fields reduces the cost of the AI call.
  • Early validation - blocking empty input here avoids wasting a call.
$json.body

Access the payload

Set node

Build context

={{ }}

Expressions

Validation

Reject junk early

3

🤖 Call the AI

The heart of AI automation. Use the native node (OpenAI/Anthropic) when available; otherwise, the HTTP Request call the API directly. Ask for output in JSON to avoid parsing problems later.

# HTTP Request to an LLM (request body)
{
  "model": "gpt-4o-mini",
  "response_format": { "type": "json_object" },
  "messages": [
    { "role": "system", "content": "Classifique o lead. Responda JSON: {intencao, prioridade}." },
    { "role": "user", "content": "={{ $json.mensagem }}" }
  ]
}

💡 Practical tip

Add the exact JSON structure that you expect in the system prompt and connect the json_object mode. This dramatically reduces malformed outputs — and temperature: 0 makes classification more stable.

System prompt

Defines the role/format

JSON mode

Structured output

Temperature

0 = stable

Cost/token

More context = more $

4

🔍 Parse the output

The response arrives as text (even in JSON mode, it comes as a string inside a field). You need to turn it into actual fields for the next nodes to use—with a fallback in case it comes back broken.

# Code node — defensive parsing
const raw = $json.choices[0].message.content;
let parsed;
try { parsed = JSON.parse(raw); }
catch (e) { parsed = { intencao: "indefinida", prioridade: "baixa" }; }
return [{ json: parsed }];

⚠️ Attention

Models sometimes wrap the JSON in ```json ... ``` or add text before it. Always handle parse failures—never trust the output to be perfect.

5

🎯 Execute the action

A action is what the client notices and pays for: sending the email, writing the row, inserting the record. It’s the only visible step — everything before it was preparation.

# mapping fields in the output node (email)
Para:     ={{ $json.email }}
Assunto:  Recebemos seu pedido — prioridade ={{ $json.prioridade }}
Corpo:    Olá ={{ $json.nome }}, classificamos como "={{ $json.intencao }}".

🎯 Choose the action based on the recipient

  • •Email — when a person needs to be notified.
  • •Spreadsheet — when the team is following along visually.
  • •Database — when another system will consume the data.
6

▶️ Run and observe

Run the actual workflow and inspect item by item. In automation, you don't use breakpoints: you read each node's input and output in the execution panel. The bug is almost always in the data format between two nodes.

1

Test the workflow with real data

Fire once with a sample payload. Watch the flow "light up" node by node.

2

Open each node and read its I/O

Compare what went in with what came out. Where the data "disappeared" is where the error is.

3

Pin data to iterate quickly

Pin the trigger output and rerun only the subsequent nodes without making another AI call.

💡 Practical tip

Use pin data in the trigger during development. You freeze a sample input and iterate through the following nodes in seconds, without triggering real webhooks or burning tokens on every test.

📌 Module Summary

✓
Trigger - a webhook (reactive) or schedule (proactive) defines the nature of the flow.
✓
Collect - extract and normalize only the useful fields; garbage in = garbage out.
✓
Call the AI - native node or HTTP, JSON mode, and temperature 0 for stability.
✓
Parse - text → JSON with try/catch and fallback; never trust the output 100%.
✓
Action - email, spreadsheet, or database; the only step the customer notices.
✓
Observe - read I/O by node and use pin data to iterate quickly and cheaply.

Next Module:

4.3 - Integrations and data (REST APIs, authentication, database, spreadsheets, pagination)