{"name":"Google Sheets to Instagram with PubNio (approved rows only)","nodes":[{"parameters":{"rule":{"interval":[{"field":"minutes","minutesInterval":15}]}},"id":"00acada6-7f7d-42de-a5a6-fea09e793ef7","name":"Every 15 minutes","type":"n8n-nodes-base.scheduleTrigger","typeVersion":1.2,"position":[0,300]},{"parameters":{"assignments":{"assignments":[{"id":"250b9636-44b4-4a0c-a917-8a8a76c993c2","name":"baseUrl","value":"https://pubnio.com","type":"string"},{"id":"f0c521b1-a065-424e-a414-65f22693dded","name":"workspaceId","value":"","type":"string"}]},"options":{}},"id":"ea2178f6-4c0c-4862-afc3-3cd2f64533cb","name":"PubNio settings","type":"n8n-nodes-base.set","typeVersion":3.4,"position":[220,300]},{"parameters":{"documentId":{"__rl":true,"value":"","mode":"list"},"sheetName":{"__rl":true,"value":"","mode":"list"},"options":{}},"id":"a0eada56-35b2-42f3-a4a2-9b3db0d199ce","name":"Read rows","type":"n8n-nodes-base.googleSheets","typeVersion":4.7,"position":[440,300]},{"parameters":{"mode":"runOnceForEachItem","jsCode":"// One sheet row becomes one Instagram feed post. publish_at must be ISO 8601 with an offset.\nconst s = $('PubNio settings').first().json;\nconst text = (value) => String(value ?? '').trim();\nconst row = {\n  rowId: text($json.id),\n  key: text($json.pubnio_key),\n  postId: text($json.pubnio_post_id),\n  status: text($json.pubnio_status),\n  sentAt: text($json.pubnio_sent_at),\n  outcome: text($json.pubnio_outcome),\n  approved: $json.approved === true || text($json.approved).toUpperCase() === 'TRUE',\n};\nconst body = { text: text($json.caption), platforms: ['instagram'] };\nif (text($json.image_url)) body.media = [{ url: text($json.image_url) }];\nif (text($json.publish_at)) body.scheduledAt = text($json.publish_at);\nreturn { json: { ...row, body } };"},"id":"3d1ffbf4-41bf-4b20-ac60-ee9ce69dcfad","name":"Normalize row","type":"n8n-nodes-base.code","typeVersion":2,"position":[660,300]},{"parameters":{"conditions":{"options":{"caseSensitive":true,"leftValue":"","typeValidation":"loose","version":2},"conditions":[{"id":"958ca162-7370-4788-a530-3bd24c6435e2","leftValue":"={{ $json.approved }}","rightValue":"","operator":{"type":"boolean","operation":"true","singleValue":true}},{"id":"425c15a3-da79-46d6-ac34-55936aeecb07","leftValue":"={{ $json.rowId }}","rightValue":"","operator":{"type":"string","operation":"notEmpty","singleValue":true}}],"combinator":"and"},"looseTypeValidation":true,"options":{}},"id":"b7d596a3-ecd2-4cbe-ae91-6b2c57bcd2e5","name":"Approved?","type":"n8n-nodes-base.if","typeVersion":2.2,"position":[880,300]},{"parameters":{},"id":"413fdb9e-5cbb-4fe0-a833-9fe8c4af6221","name":"Not approved: skip","type":"n8n-nodes-base.noOp","typeVersion":1,"position":[1100,600]},{"parameters":{"mode":"runOnceForEachItem","jsCode":"// Decide what one approved row needs. The idempotency key is a hash of the\n// row ID, saved on the row before the first request, so a rerun after a\n// timeout sends the same key and PubNio replays its first answer.\nconst hash = (value) => {\n  let h1 = 1779033703, h2 = 3144134277, h3 = 1013904242, h4 = 2773480762;\n  for (let i = 0; i < value.length; i++) {\n    const k = value.charCodeAt(i);\n    h1 = h2 ^ Math.imul(h1 ^ k, 597399067);\n    h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);\n    h3 = h4 ^ Math.imul(h3 ^ k, 951274213);\n    h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);\n  }\n  h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);\n  h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);\n  h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);\n  h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);\n  h1 ^= h2 ^ h3 ^ h4; h2 ^= h1; h3 ^= h1; h4 ^= h1;\n  return [h1, h2, h3, h4].map((h) => (h >>> 0).toString(16).padStart(8, '0')).join('');\n};\nconst row = $json;\nconst replayWindowMs = 23 * 60 * 60 * 1000; // PubNio replays a key for 24 hours.\nconst finished = ['published', 'provider_draft', 'failed', 'cancelled', 'rejected', 'needs_review'];\nconst fresh = !row.key;\nconst key = row.key || 'n8n-' + $workflow.id + '-' + hash(String(row.rowId));\nconst sentAt = fresh ? new Date().toISOString() : row.sentAt;\n// One media import per row and URL; a resend after a rejection gets a new sentAt, so a new import.\nconst mediaKey = row.mediaUrl ? key + '-m' + hash(row.mediaUrl + '|' + sentAt).slice(0, 16) : '';\nlet action = 'submit';\nif (row.postId) action = finished.includes(row.status) ? 'done' : 'sync';\nelse if (finished.includes(row.status)) action = 'done';\n// A saved key without a readable send time fails closed: it may be older than the replay window.\nelse if (!fresh && !(Date.now() - Date.parse(row.sentAt) <= replayWindowMs)) action = 'stale';\nelse if (fresh && row.problem) action = 'invalid';\nreturn { json: { ...row, key, sentAt, mediaKey, fresh, action } };"},"id":"c0064e1d-a7f6-4202-a87d-ccd201d7b330","name":"Plan action","type":"n8n-nodes-base.code","typeVersion":2,"position":[1100,300]},{"parameters":{"rules":{"values":[{"conditions":{"options":{"caseSensitive":true,"leftValue":"","typeValidation":"strict","version":2},"conditions":[{"id":"7e7e5dd2-e6b9-4aad-abd1-069e6a1ae9ec","leftValue":"={{ $json.action }}","rightValue":"submit","operator":{"type":"string","operation":"equals"}}],"combinator":"and"},"renameOutput":true,"outputKey":"submit"},{"conditions":{"options":{"caseSensitive":true,"leftValue":"","typeValidation":"strict","version":2},"conditions":[{"id":"275ad12b-94b8-478c-a149-d6661a3f50aa","leftValue":"={{ $json.action }}","rightValue":"sync","operator":{"type":"string","operation":"equals"}}],"combinator":"and"},"renameOutput":true,"outputKey":"sync"},{"conditions":{"options":{"caseSensitive":true,"leftValue":"","typeValidation":"strict","version":2},"conditions":[{"id":"c55db738-4475-4966-aaeb-8e35350dac88","leftValue":"={{ $json.action }}","rightValue":"stale","operator":{"type":"string","operation":"equals"}}],"combinator":"and"},"renameOutput":true,"outputKey":"stale"},{"conditions":{"options":{"caseSensitive":true,"leftValue":"","typeValidation":"strict","version":2},"conditions":[{"id":"86c362e4-ab9e-4d9a-a87e-67d897f1f360","leftValue":"={{ $json.action }}","rightValue":"invalid","operator":{"type":"string","operation":"equals"}}],"combinator":"and"},"renameOutput":true,"outputKey":"invalid"},{"conditions":{"options":{"caseSensitive":true,"leftValue":"","typeValidation":"strict","version":2},"conditions":[{"id":"d044d8ba-727f-4569-a79a-a8516ff49325","leftValue":"={{ $json.action }}","rightValue":"done","operator":{"type":"string","operation":"equals"}}],"combinator":"and"},"renameOutput":true,"outputKey":"done"}]},"options":{}},"id":"7167340a-de73-4478-abd3-73f20bf85567","name":"Route by action","type":"n8n-nodes-base.switch","typeVersion":3.2,"position":[1320,300]},{"parameters":{"mode":"runOnceForEachItem","jsCode":"// Map PubNio's answer onto the row. A 202 only means PubNio accepted the\n// work; the row says published only when a delivery reports it.\nconst plan = $('Plan action').item.json;\nconst res = $input.item.json;\nconst body = res.body && typeof res.body === 'object' ? res.body : {};\nconst code = Number(res.statusCode);\nconst reject = (out) => Object.assign(out, { status: 'rejected', key: '', sentAt: '' });\nconst active = ['submitting', 'confirming', 'queued', 'scheduled'];\nconst settled = ['unresolved', 'failed', 'published', 'provider_draft', 'cancelled'];\nconst pick = (states) => active.find((s) => states.includes(s)) || settled.find((s) => states.includes(s)) || 'queued';\nconst line = (t) => [\n  t.provider + ' ' + t.placement + ': ' + t.status,\n  t.status === 'scheduled' && t.scheduledAt ? 'at ' + t.scheduledAt : '',\n  t.localDate ? '(queue slot ' + t.localDate + ' ' + t.localTime + ' ' + t.timezone + ')' : '',\n  t.url || '',\n  t.issue ? '[' + t.issue + ']' : '',\n  t.nextAction ? 'Next: ' + t.nextAction : '',\n].filter(Boolean).join(' ');\nconst out = {\n  rowId: plan.rowId,\n  key: plan.key,\n  sentAt: plan.sentAt,\n  postId: plan.postId || '',\n  status: plan.status || '',\n  outcome: plan.outcome || '',\n  error: '',\n  revision: plan.draftRevision || '',\n  media: plan.sentMedia || '',\n  retryFailed: plan.retryFailed,\n};\nif (code >= 200 && code < 300) {\n  const targets = body.deliveries || body.targets || [];\n  out.postId = body.postId || out.postId;\n  out.status = body.status || pick(targets.map((t) => t.status));\n  out.outcome = targets.map(line).join('\\n');\n  if (body.revision) out.revision = body.revision;\n  if (plan.mediaUrl !== undefined) out.media = plan.mediaUrl;\n  return { json: out };\n}\nconst problem = body.code || String(body.type || '').split('/').filter(Boolean).pop() || 'http_' + code;\nout.error = [\n  code + ' ' + problem + (body.title ? ': ' + body.title : ''),\n  ...(body.issues || body.errors || []).map((i) => (i.code || 'invalid') + ' at ' + (i.path || []).join('.') + ': ' + i.message),\n  body.currentRevision ? 'The draft is now revision ' + body.currentRevision + '.' : '',\n  body.nextAction ? 'Next: ' + body.nextAction : '',\n].filter(Boolean).join('\\n');\n// A read or a draft step failed: keep the post and its last known state.\nif (plan.postId) out.status = code === 404 ? 'needs_review' : plan.status;\n// The key was used for another body, or the same text and media already went to\n// this account in the last 24 hours: resending cannot help, so a person decides.\nelse if (problem === 'idempotency_conflict' || problem === 'duplicate_content') out.status = 'needs_review';\n// In progress, rate limited, or a server error: the outcome is unknown, so the\n// next run resends the same key and PubNio replays the first answer.\nelse if (code === 409 || code === 429 || code >= 500) out.status = 'sending';\n// PubNio refused the request and stored nothing. Fix the row, then clear\n// pubnio_status to send it again.\nelse reject(out);\nreturn { json: out };"},"id":"64359468-25ef-49e3-a505-a0503d9cae18","name":"Record PubNio answer","type":"n8n-nodes-base.code","typeVersion":2,"position":[3300,200]},{"parameters":{"mode":"runOnceForEachItem","jsCode":"// The request got no answer (timeout or network). PubNio may have accepted\n// it, so keep the key: the next run resends it and PubNio replays.\nconst plan = $('Plan action').item.json;\nconst error = $json.error || {};\nconst out = {\n  rowId: plan.rowId,\n  key: plan.key,\n  sentAt: plan.sentAt,\n  postId: plan.postId || '',\n  status: plan.postId ? plan.status : 'sending',\n  outcome: plan.outcome || '',\n  error: '',\n  revision: plan.draftRevision || '',\n  media: plan.sentMedia || '',\n  retryFailed: plan.retryFailed,\n};\nout.error = 'No answer from PubNio: ' + (error.message || error.description || 'request failed');\nreturn { json: out };"},"id":"0e221073-f1ca-4047-a7ac-0f0114542ff7","name":"Record missing answer","type":"n8n-nodes-base.code","typeVersion":2,"position":[3300,400]},{"parameters":{"operation":"update","documentId":{"__rl":true,"value":"","mode":"list"},"sheetName":{"__rl":true,"value":"","mode":"list"},"columns":{"mappingMode":"defineBelow","value":{"id":"={{ $json.rowId }}","pubnio_key":"={{ $json.key }}","pubnio_post_id":"={{ $json.postId }}","pubnio_status":"={{ $json.status }}","pubnio_outcome":"={{ $json.outcome }}","pubnio_error":"={{ $json.error }}","pubnio_sent_at":"={{ $json.sentAt }}"},"matchingColumns":["id"]},"options":{}},"id":"307f9de4-7975-49a3-a92d-a7aa719faa8d","name":"Write result to row","type":"n8n-nodes-base.googleSheets","typeVersion":4.7,"position":[3520,300]},{"parameters":{"operation":"update","documentId":{"__rl":true,"value":"","mode":"list"},"sheetName":{"__rl":true,"value":"","mode":"list"},"columns":{"mappingMode":"defineBelow","value":{"id":"={{ $('Plan action').item.json.rowId }}","pubnio_key":"={{ $('Plan action').item.json.key }}","pubnio_status":"sending","pubnio_sent_at":"={{ $('Plan action').item.json.sentAt }}","pubnio_error":""},"matchingColumns":["id"]},"options":{}},"id":"96bb0781-ac33-4825-a7de-49797108c7f4","name":"Save key before sending","type":"n8n-nodes-base.googleSheets","typeVersion":4.7,"position":[1560,100]},{"parameters":{"method":"POST","url":"={{ $('PubNio settings').first().json.baseUrl }}/api/v1/posts","authentication":"genericCredentialType","genericAuthType":"httpHeaderAuth","sendQuery":true,"queryParameters":{"parameters":[{"name":"workspaceId","value":"={{ $('PubNio settings').first().json.workspaceId }}"}]},"sendHeaders":true,"headerParameters":{"parameters":[{"name":"Idempotency-Key","value":"={{ $('Plan action').item.json.key }}"},{"name":"PubNio-Version","value":"2026-12-02"}]},"sendBody":true,"specifyBody":"json","jsonBody":"={{ JSON.stringify($('Plan action').item.json.body) }}","options":{"response":{"response":{"fullResponse":true,"neverError":true,"responseFormat":"json"}},"timeout":30000}},"id":"7037486d-9775-4140-a894-9a1d78de2222","name":"Create post in PubNio","type":"n8n-nodes-base.httpRequest","typeVersion":4.2,"position":[2880,100],"onError":"continueErrorOutput"},{"parameters":{"method":"GET","url":"={{ $('PubNio settings').first().json.baseUrl }}/api/v1/posts/{{ $('Plan action').item.json.postId }}","authentication":"genericCredentialType","genericAuthType":"httpHeaderAuth","sendQuery":true,"queryParameters":{"parameters":[{"name":"workspaceId","value":"={{ $('PubNio settings').first().json.workspaceId }}"}]},"sendHeaders":true,"headerParameters":{"parameters":[{"name":"PubNio-Version","value":"2026-12-02"}]},"options":{"response":{"response":{"fullResponse":true,"neverError":true,"responseFormat":"json"}},"timeout":30000}},"id":"f28ecaee-ff32-4964-af25-bc02d5310fb4","name":"Read post status","type":"n8n-nodes-base.httpRequest","typeVersion":4.2,"position":[1780,500],"onError":"continueErrorOutput"},{"parameters":{"mode":"runOnceForEachItem","jsCode":"// Nothing is sent for these rows. A key that went out more than 23 hours ago\n// (or whose send time is unreadable) without a post ID may already have\n// posted, and PubNio replays a key for 24 hours only. A row the workflow\n// cannot send as written is rejected until a person fixes it.\nconst plan = $json;\nconst out = {\n  rowId: plan.rowId,\n  key: plan.key,\n  sentAt: plan.sentAt,\n  postId: plan.postId || '',\n  status: 'needs_review',\n  outcome: plan.outcome || '',\n  error: '',\n  revision: plan.draftRevision || '',\n  media: plan.sentMedia || '',\n  retryFailed: plan.retryFailed,\n};\nif (plan.action === 'invalid') {\n  Object.assign(out, { status: 'rejected', key: '', sentAt: '', error: plan.problem });\n  return { json: out };\n}\nout.error = 'Key ' + plan.key + ' was sent at ' + (plan.sentAt || 'an unknown time') + ' and no post ID came back. Find the post in PubNio and paste its ID into pubnio_post_id, or clear pubnio_key, pubnio_sent_at, and pubnio_status to send it as a new post.';\nreturn { json: out };"},"id":"1401d456-1bf3-4d28-a453-6454ce55a9ba","name":"Record row problem","type":"n8n-nodes-base.code","typeVersion":2,"position":[1780,700]},{"parameters":{},"id":"d202e208-1237-4e60-a002-3b33a35bee51","name":"Nothing to do","type":"n8n-nodes-base.noOp","typeVersion":1,"position":[1780,900]},{"parameters":{"content":"## Google Sheets → Instagram\n\nPublishes each row whose **approved** box is ticked, once.\n\n**Sheet columns:** `id` (unique, never reused), `approved` (checkbox), `caption`, `image_url` (public HTTPS), `publish_at` (optional, ISO 8601 with offset, e.g. `2026-12-08T09:00:00+01:00`), then the columns the workflow fills: `pubnio_key`, `pubnio_post_id`, `pubnio_status`, `pubnio_outcome`, `pubnio_error`, `pubnio_sent_at`.\n\n**Setup:** set `workspaceId` in **PubNio settings**; create a Header Auth credential named `PubNio API key` (name `Authorization`, value `Bearer <key with publish scope>`); pick your sheet in the three Google Sheets nodes.","height":340,"width":620},"id":"353726e1-68df-45d1-a85a-ed526b58313d","name":"Setup","type":"n8n-nodes-base.stickyNote","typeVersion":1,"position":[-40,-80]}],"connections":{"Every 15 minutes":{"main":[[{"node":"PubNio settings","type":"main","index":0}]]},"PubNio settings":{"main":[[{"node":"Read rows","type":"main","index":0}]]},"Read rows":{"main":[[{"node":"Normalize row","type":"main","index":0}]]},"Normalize row":{"main":[[{"node":"Approved?","type":"main","index":0}]]},"Approved?":{"main":[[{"node":"Plan action","type":"main","index":0}],[{"node":"Not approved: skip","type":"main","index":0}]]},"Plan action":{"main":[[{"node":"Route by action","type":"main","index":0}]]},"Record PubNio answer":{"main":[[{"node":"Write result to row","type":"main","index":0}]]},"Record missing answer":{"main":[[{"node":"Write result to row","type":"main","index":0}]]},"Route by action":{"main":[[{"node":"Save key before sending","type":"main","index":0}],[{"node":"Read post status","type":"main","index":0}],[{"node":"Record row problem","type":"main","index":0}],[{"node":"Record row problem","type":"main","index":0}],[{"node":"Nothing to do","type":"main","index":0}]]},"Save key before sending":{"main":[[{"node":"Create post in PubNio","type":"main","index":0}]]},"Create post in PubNio":{"main":[[{"node":"Record PubNio answer","type":"main","index":0}],[{"node":"Record missing answer","type":"main","index":0}]]},"Read post status":{"main":[[{"node":"Record PubNio answer","type":"main","index":0}],[{"node":"Record missing answer","type":"main","index":0}]]},"Record row problem":{"main":[[{"node":"Write result to row","type":"main","index":0}]]}},"settings":{"executionOrder":"v1"},"pinData":{},"meta":{"templateCredsSetupCompleted":false}}