Skip to content

In n8n, copy this file and paste it onto the canvas, or use Import from File.

{
  "name": "01 - Text Post API",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "posts",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "1927ec80-d771-4805-9035-4d7a0e2a10db",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -224,
        0
      ],
      "webhookId": "text-post-playground"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "c1a7e0f2-1111-4b6a-9c11-000000000001",
              "leftValue": "={{ $json.body.text }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            },
            {
              "id": "c1a7e0f2-2222-4b6a-9c11-000000000002",
              "leftValue": "={{ ($json.body.text || '').length }}",
              "rightValue": 280,
              "operator": {
                "type": "number",
                "operation": "lte"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        16,
        0
      ],
      "id": "71a4bf8e-2a10-44a2-83cd-1e0fb08e8671",
      "name": "Valid Post?",
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "p1",
              "name": "id",
              "value": "={{ $execution.id }}",
              "type": "string"
            },
            {
              "id": "p2",
              "name": "author",
              "value": "={{ $json.body.author || 'anonymous' }}",
              "type": "string"
            },
            {
              "id": "p3",
              "name": "text",
              "value": "={{ $json.body.text }}",
              "type": "string"
            },
            {
              "id": "p4",
              "name": "length",
              "value": "={{ $json.body.text.length }}",
              "type": "number"
            },
            {
              "id": "p5",
              "name": "createdAt",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "bc710d9a-ff55-4a85-9cec-7ec10903b8f9",
      "name": "Build Post",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        192,
        -80
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": 201
        }
      },
      "id": "7913e76a-c329-42f5-90e2-069d05490991",
      "name": "Respond Created",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        496,
        -80
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "60b88560-b625-464c-acae-f69ff37b1e9c",
              "name": "error",
              "value": "text must be 1-280 characters",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "2dff12e5-26a1-45ad-b695-41a1b413e25b",
      "name": "Build Error",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        192,
        64
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": 400
        }
      },
      "id": "f658a083-d02f-43ff-be76-ddc160a86e92",
      "name": "Respond Error",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        496,
        64
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Valid Post?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid Post?": {
      "main": [
        [
          {
            "node": "Build Post",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Post": {
      "main": [
        [
          {
            "node": "Respond Created",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Error": {
      "main": [
        [
          {
            "node": "Respond Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  }
}