{
  "name": "Airtable to sprkly, with your OK first",
  "nodes": [
    {
      "id": "ecef8674-2ed0-43a7-a026-0eaa314c3a65",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1640,
        -630
      ],
      "parameters": {
        "content": "## Airtable to sprkly, with your OK first\nReady records in Airtable become posts that wait for your OK. Each record gets its status and link back.\n\n### You need\n- The sprkly node: Settings, Community nodes, install `n8n-nodes-sprkly` (self-hosted n8n)\n- A sprkly API key: https://sprkly.app/docs/api?utm_source=n8n&utm_medium=workflow&utm_campaign=airtable-to-sprkly\n- An Airtable login and a table with: Caption, Publish Time, Accounts, Media, Media URL, Approval Note, Status, sprkly Post ID, Scheduled For, Link, Error\n- A Google Sheets login and a sheet, for the log of live posts\n- A Telegram bot and your chat ID, for the failure alert\n\n### Set it up\n1. Add your Airtable login to the 3 Airtable steps. Pick your base and table in each.\n2. Add your sprkly key to the sprkly steps. Put your sheet ID in **Published log settings**.\n3. Write Accounts like instagram:clienthandle. Give Publish Time a time, not only a date.\n4. Failure alert: put your chat ID in **Write the failure alert** and your bot in **Send the failure alert**. Then in Workflow settings, set Error workflow to this workflow.\n\n### Test it\n- Set one record to Ready with a time tomorrow. Run the workflow once.\n- The record gets a sprkly Post ID, and the post waits for your OK.",
        "height": 858,
        "width": 560,
        "color": 4
      }
    },
    {
      "id": "0cb91997-e664-4992-9dbf-968fe3595e02",
      "name": "Note: schedule records",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -440,
        80
      ],
      "parameters": {
        "content": "## Schedule Ready records\nOnly the first file in Media is used. A bad file link writes Error to the record and skips the post. Every other record still goes.",
        "height": 500,
        "width": 1460,
        "color": 7
      }
    },
    {
      "id": "186a0a5a-932d-4f33-bfd1-5e1f4e09ef00",
      "name": "Note: track posts",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -440,
        680
      ],
      "parameters": {
        "content": "## Track what happened\nWhen a post goes out or fails, the record is found by its id. Only posts with an External ID that starts with airtable- reach this table.",
        "height": 440,
        "width": 760,
        "color": 7
      }
    },
    {
      "id": "ac6cb45a-2cd8-42c2-81da-d293d50d24b2",
      "name": "Every 15 minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        -400,
        320
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      }
    },
    {
      "id": "5b64eed9-5707-4187-8fbe-198ab412950c",
      "name": "Get Ready records",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        -160,
        320
      ],
      "parameters": {
        "resource": "record",
        "operation": "search",
        "base": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "filterByFormula": "{Status} = 'Ready'",
        "returnAll": true,
        "options": {}
      }
    },
    {
      "id": "20967f50-11a5-42d6-a1fe-411223cc6fe7",
      "name": "Has a file?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        80,
        320
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "has-file",
              "leftValue": "={{ ($json.Media || []).length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      }
    },
    {
      "id": "f5c33512-5b10-44c1-ba2d-b88f749aeb47",
      "name": "Keep the file in sprkly",
      "type": "n8n-nodes-sprkly.sprkly",
      "typeVersion": 1.1,
      "position": [
        320,
        200
      ],
      "parameters": {
        "resource": "media",
        "operation": "addFromUrl",
        "url": "={{ $json.Media[0].url }}"
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "59e06a95-fb2e-4437-a5c3-280d06a01163",
      "name": "Schedule post for approval",
      "type": "n8n-nodes-sprkly.sprkly",
      "typeVersion": 1.1,
      "position": [
        560,
        320
      ],
      "parameters": {
        "resource": "post",
        "operation": "create",
        "accountMode": "name",
        "accountNames": "={{ [].concat($('Get Ready records').item.json.Accounts || []).join(', ') }}",
        "caption": "={{ $('Get Ready records').item.json.Caption }}",
        "scheduledTime": "={{ $('Get Ready records').item.json['Publish Time'] }}",
        "mediaIds": "={{ $json.mediaId || '' }}",
        "output": "resultRow",
        "additionalFields": {
          "externalId": "={{ 'airtable-' + $('Get Ready records').item.json.id }}",
          "mediaUrls": "={{ $('Get Ready records').item.json['Media URL'] || '' }}",
          "requireExternalId": true,
          "requiresApproval": true,
          "approvalNote": "={{ $('Get Ready records').item.json['Approval Note'] || '' }}"
        }
      }
    },
    {
      "id": "6a01fae2-f5fd-41c3-93cc-d4cffaeda55a",
      "name": "Write result to record",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        800,
        320
      ],
      "parameters": {
        "resource": "record",
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $('Get Ready records').item.json.id }}",
            "Status": "={{ $json.status || 'Error' }}",
            "sprkly Post ID": "={{ $json.postId || '' }}",
            "Scheduled For": "={{ $json.scheduledFor || '' }}",
            "Error": "={{ $json.error || '' }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "readOnly": true,
              "removed": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "sprkly Post ID",
              "displayName": "sprkly Post ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Scheduled For",
              "displayName": "Scheduled For",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Link",
              "displayName": "Link",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Error",
              "displayName": "Error",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "typecast": true
        }
      }
    },
    {
      "id": "7146fdf2-dc30-452a-8302-a7425066213c",
      "name": "When a post goes out",
      "type": "n8n-nodes-sprkly.sprklyTrigger",
      "typeVersion": 1,
      "position": [
        -400,
        800
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 5,
              "unit": "minutes"
            }
          ]
        },
        "event": "postPublished",
        "externalIdPrefix": "airtable-",
        "stripPrefix": true
      }
    },
    {
      "id": "94654bf8-1e0e-4817-bb17-a4518ea22477",
      "name": "When a post fails",
      "type": "n8n-nodes-sprkly.sprklyTrigger",
      "typeVersion": 1,
      "position": [
        -400,
        980
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 5,
              "unit": "minutes"
            }
          ]
        },
        "event": "postFailed",
        "externalIdPrefix": "airtable-",
        "stripPrefix": true
      }
    },
    {
      "id": "01dc136e-7e5d-4535-9f30-24ce08d61769",
      "name": "Write post status to record",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        -120,
        880
      ],
      "parameters": {
        "resource": "record",
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $json.sourceId }}",
            "Status": "={{ $json.statusLabel }}",
            "Link": "={{ $json.link }}",
            "Error": "={{ $json.error }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "readOnly": true,
              "removed": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "sprkly Post ID",
              "displayName": "sprkly Post ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Scheduled For",
              "displayName": "Scheduled For",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Link",
              "displayName": "Link",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Error",
              "displayName": "Error",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "typecast": true
        }
      }
    },
    {
      "name": "Note: automatic published post log",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "id": "6a4cb9f4-3d65-55cd-b5a7-ce23869683fd",
      "position": [
        -1000,
        -630
      ],
      "parameters": {
        "content": "## Log posts as they go live\n\nSet **Spreadsheet ID** in **Published log settings**. Use the same Google Sheets credential in the two log steps and your sprkly API credential on **When this workflow's post goes live**.\n\nThe workflow checks each minute for its published posts. It creates **Published Posts** and its headers as needed, then writes one row per Post ID. This branch runs after publication. It leaves the scheduling and source status branches intact.",
        "height": 215,
        "width": 520,
        "color": 5
      }
    },
    {
      "name": "When this workflow's post goes live",
      "type": "n8n-nodes-sprkly.sprklyTrigger",
      "typeVersion": 1,
      "id": "24f45cd0-b5a3-588a-96ba-3530a562395b",
      "position": [
        -1000,
        -380
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "event": "postPublished",
        "externalIdPrefix": "airtable-",
        "stripPrefix": false
      }
    },
    {
      "name": "Build published log rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "id": "f59b9662-550f-513b-bbdc-c43c2a4305b3",
      "position": [
        -760,
        -380
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const prefix = \"airtable-\";\nconst text = value => typeof value === 'string' || typeof value === 'number' ? String(value).trim() : '';\nconst values = input => Array.isArray(input) ? input.map(text).filter(Boolean) : [];\nconst rows = new Map();\n\nfor (const [index, item] of $input.all().entries()) {\n  const post = item.json;\n  const id = text(post.id);\n  const externalId = text(post.externalId);\n  if (!id || post.deleted || !['posted', 'published'].includes(post.status) ||\n      !externalId.startsWith(prefix) || externalId.length === prefix.length) continue;\n\n  const platforms = values(post.platforms);\n  const accounts = values(post.profileIds);\n  const suffix = ':' + platforms[0] + ':' + accounts[0];\n  const source = platforms.length && accounts.length && externalId.endsWith(suffix)\n    ? externalId.slice(0, -suffix.length) : externalId;\n  if (source.length <= prefix.length) continue;\n  const previous = rows.get(id)?.json;\n  rows.set(id, {\n    json: {\n      'Post ID': id,\n      Status: 'Published',\n      'Published At': text(post.publishedAt) || previous?.['Published At'] || '',\n      Platform: platforms.join(', '),\n      'Account ID': accounts.join(', '),\n      'Source ID': source,\n      'External ID': externalId,\n      'Live URL': text(post.permalink) || text(post.link) || previous?.['Live URL'] || '',\n      'Sprkly URL': 'https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=airtable-to-sprkly&post=' + encodeURIComponent(id),\n    },\n    pairedItem: index,\n  });\n}\nreturn [...rows.values()];"
      }
    },
    {
      "name": "Published log settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "id": "a21b94eb-dfd4-5634-bb3c-7a2b5926a951",
      "position": [
        -520,
        -380
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "6db00888-e4b3-536d-af95-f2e265cc7c31",
              "name": "Spreadsheet ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "1d54f5ce-f7fd-50ba-9a74-7c1caad9ffa2",
              "name": "Tab name",
              "type": "string",
              "value": "Published Posts"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "executeOnce": true
    },
    {
      "name": "Ensure published log tab",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "id": "450acf38-f52b-5665-b465-2461bd9b1bda",
      "position": [
        -280,
        -380
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "create",
        "documentId": {
          "__rl": true,
          "value": "={{ String($json[\"Spreadsheet ID\"]).trim() }}",
          "mode": "id"
        },
        "title": "={{ String($json[\"Tab name\"]).trim() }}",
        "options": {}
      },
      "alwaysOutputData": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "name": "Restore published log rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "id": "968ff8dd-cb14-5a0b-bbba-1a0a03c2d8c6",
      "position": [
        -40,
        -380
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Create Sheet has one output for the setup call. Restore every publication in the trigger batch.\nreturn $('Build published log rows').all().map(({ json }) => ({ json, pairedItem: 0 }));"
      }
    },
    {
      "name": "Upsert published post log",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "id": "b1ef2e0f-35ad-5b98-ba84-acf815c52ed2",
      "position": [
        200,
        -380
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "={{ String($(\"Published log settings\").first().json[\"Spreadsheet ID\"]).trim() }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "={{ String($(\"Published log settings\").first().json[\"Tab name\"]).trim() }}",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [
            "Post ID"
          ],
          "schema": []
        },
        "options": {
          "cellFormat": "RAW",
          "handlingExtraData": "insertInNewColumn",
          "useAppend": true
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "id": "60ccac1c-960d-4395-b767-10b440150165",
      "name": "When this workflow fails",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -1600,
        308
      ],
      "parameters": {}
    },
    {
      "id": "6e02b8d3-a407-4171-8707-5667478e791a",
      "name": "Write the failure alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -1380,
        308
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "telegramChatId",
              "type": "string",
              "value": "YOUR_TELEGRAM_CHAT_ID"
            },
            {
              "id": "f2",
              "name": "message",
              "type": "string",
              "value": "={{ 'A workflow stopped.\\n\\nWorkflow: ' + (($json.workflow || {}).name || 'unknown') + '\\nFailed step: ' + (($json.execution || {}).lastNodeExecuted || 'unknown') + '\\nWhy: ' + String((($json.execution || {}).error || {}).message || 'n8n did not give a reason.').slice(0, 300) + (($json.execution || {}).url ? '\\nOpen the run: ' + $json.execution.url : '') }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    },
    {
      "id": "4f96256f-b5d4-48f4-a6f8-94869be11f54",
      "name": "Send the failure alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -1160,
        308
      ],
      "parameters": {
        "chatId": "={{ $json.telegramChatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    }
  ],
  "connections": {
    "Every 15 minutes": {
      "main": [
        [
          {
            "node": "Get Ready records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Ready records": {
      "main": [
        [
          {
            "node": "Has a file?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has a file?": {
      "main": [
        [
          {
            "node": "Keep the file in sprkly",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Schedule post for approval",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Keep the file in sprkly": {
      "main": [
        [
          {
            "node": "Schedule post for approval",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Write result to record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule post for approval": {
      "main": [
        [
          {
            "node": "Write result to record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When a post goes out": {
      "main": [
        [
          {
            "node": "Write post status to record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When a post fails": {
      "main": [
        [
          {
            "node": "Write post status to record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When this workflow's post goes live": {
      "main": [
        [
          {
            "node": "Build published log rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build published log rows": {
      "main": [
        [
          {
            "node": "Published log settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Published log settings": {
      "main": [
        [
          {
            "node": "Ensure published log tab",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ensure published log tab": {
      "main": [
        [
          {
            "node": "Restore published log rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restore published log rows": {
      "main": [
        [
          {
            "node": "Upsert published post log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When this workflow fails": {
      "main": [
        [
          {
            "node": "Write the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the failure alert": {
      "main": [
        [
          {
            "node": "Send the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}
