Skip to content
Text Post API

Module 01 · foundations · beginner

Solutions 01 · Text Post API

The complete result of Exercises 1–3 is workflow/learner.json.

Exercise 1

Insert an IF node (Valid Post?) between Webhook and Build Post:

  • Value: {{ $json.body.text }}
  • Operator: String → is not empty

Connect true → Build Post. The false output is handled in Exercise 2.

exists would let {"text": ""} through and create an empty post. is not empty rejects both missing and empty text.

Exercise 2

From the IF's false output:

  1. Edit Fields Build Error: error = text must be 1-280 characters (String, no quotes).
  2. Respond to Webhook Respond Error: Respond With JSON, body {{ $json }}, Options → Response Code 400.

On the true branch, rename the Respond node to Respond Created and set Options → Response Code 201. One shared Respond node can't return different codes per branch.

Exercise 3

In Valid Post?, add a second condition and keep the combinator on AND:

  • Value: {{ ($json.body.text || '').length }}
  • Operator: Number → is less than or equal to, value 280

Without || '', a request with no text makes the expression fail instead of going down the false branch.

Exercise 4

Click Publish, then:

curl -X POST http://localhost:5678/webhook/posts -H 'Content-Type: application/json' -d '{"author":"Ian","text":"Shipped my first n8n API"}'

Production runs don't appear on the canvas; open the Executions tab.

Stretch

In Build Post, add a field hashtags of type Array:

{{ ($json.body.text.match(/#(\w+)/g) || []).map(tag => tag.slice(1)) }}

match returns null when there are no hashtags, so || [] keeps the field an empty array.