{
  "name": "AI-rewrite a caption and publish",
  "nodes": [
    {
      "parameters": {},
      "id": "e4f5a6b7-0001-4a5b-9c0d-111111111111",
      "name": "When clicking 'Test workflow'",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Edit the values below, then run the workflow.\n// channelId: your connected channel id (app > Settings > Channels).\n// platform: provider identifier used in settings.__type (x, instagram, linkedin, threads, ...).\nconst channelId = 'REPLACE_WITH_CHANNEL_ID';\nconst platform = 'x';\nconst tone = 'confident and friendly';\nconst draft = 'Check out our new PostSider n8n workflows. They publish for you automatically. Go to postsider.com to get them.';\n\nreturn [{ json: { channelId, platform, tone, draft } }];"
      },
      "id": "e4f5a6b7-0002-4a5b-9c0d-222222222222",
      "name": "1. Draft caption",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"gpt-4o-mini\",\n  \"temperature\": 0.7,\n  \"messages\": [\n    { \"role\": \"system\", \"content\": \"You rewrite social media captions. Keep the core message, make the copy punchier and more engaging for the stated platform. Respond with the caption text only.\" },\n    { \"role\": \"user\", \"content\": \"Platform: {{ $json.platform }}. Tone: {{ $json.tone }}. Rewrite this caption: {{ $json.draft }}\" }\n  ]\n}"
      },
      "id": "e4f5a6b7-0003-4a5b-9c0d-333333333333",
      "name": "2. Rewrite with OpenAI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "2",
          "name": "OpenAI API Key"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Take the rewritten text out of the OpenAI response and build the PostSider payload.\nconst draft = $('1. Draft caption').first().json;\nconst rewritten = $input.first().json.choices?.[0]?.message?.content?.trim() || '';\n\nreturn [{\n  json: {\n    rewritten,\n    body: {\n      type: 'now',\n      date: '',\n      shortLink: false,\n      tags: [],\n      posts: [{\n        integration: { id: draft.channelId },\n        value: [{ content: rewritten, image: [] }],\n        settings: { __type: draft.platform }\n      }]\n    }\n  }\n}];"
      },
      "id": "e4f5a6b7-0004-4a5b-9c0d-444444444444",
      "name": "3. Build publish payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.postsider.com/public/v1/posts",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.body) }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "=post-{{ $now.toISO() }}"
            }
          ]
        }
      },
      "id": "e4f5a6b7-0005-4a5b-9c0d-555555555555",
      "name": "4. Publish to PostSider",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "1",
          "name": "PostSider API Key"
        }
      }
    },
    {
      "parameters": {
        "height": 340,
        "width": 460,
        "content": "SETUP (once):\n\n1. Create an API key in PostSider: app.postsider.com > Settings > API. It starts with ps_.\n2. Create TWO Header Auth credentials in n8n:\n   - 'PostSider API Key': Name = Authorization, Value = your ps_ key.\n   - 'OpenAI API Key': Name = Authorization, Value = Bearer sk-... (your OpenAI key).\n3. Select the matching credential on each HTTP node.\n4. Edit '1. Draft caption' (channel id, platform, draft text, tone).\n\nTip: swap the OpenAI call for n8n's native OpenAI node if you prefer managing the key there. This workflow mirrors the in-app AI caption rewrite, but you control the prompt and model.\n\nSelf-hosted PostSider? Replace api.postsider.com with your domain (add /api if needed).",
        "color": 3
      },
      "id": "e4f5a6b7-0006-4a5b-9c0d-666666666666",
      "name": "Setup notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -360
      ]
    },
    {
      "parameters": {
        "content": "SETUP STEP BY STEP\nFollow in order. Do this once per workflow.\n\nSTEP 1 - Create the n8n credentials (do this once, both type Header Auth):\n  a. \"PostSider API Key\": Name = Authorization, Value = your ps_ key (app.postsider.com > Settings > API > Create key).\n  b. \"OpenAI API Key\": Name = Authorization, Value = Bearer sk-... (your OpenAI key, platform.openai.com).\n\nSTEP 2 - \"1. Draft caption\" (Set node):\n  Fill in: draft (your raw caption), channelId (a real channel id from app > Settings > Channels).\n\nSTEP 3 - Select the credentials:\n  - \"OpenAI API Key\" on \"2. Rewrite with OpenAI\"\n  - \"PostSider API Key\" on \"4. Publish to PostSider\"\n\nSTEP 4 - Run:\n  Click \"Test workflow\" once. The caption is rewritten by OpenAI and published immediately.\n\nNOTE\nSelf-hosted PostSider? Replace api.postsider.com with your domain (add /api if your install serves the API under /api).",
        "height": 620,
        "width": 640,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -700
      ],
      "id": "setup-step-ai-rewrite-publish",
      "name": "Setup step by step"
    }
  ],
  "connections": {
    "When clicking 'Test workflow'": {
      "main": [
        [
          {
            "node": "1. Draft caption",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1. Draft caption": {
      "main": [
        [
          {
            "node": "2. Rewrite with OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2. Rewrite with OpenAI": {
      "main": [
        [
          {
            "node": "3. Build publish payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "3. Build publish payload": {
      "main": [
        [
          {
            "node": "4. Publish to PostSider",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}