{ "name": "Create, Update and Read a Strapi Post", "nodes": [ { "name": "On clicking 'execute'", "type": "n8n-nodes-base.manualTrigger", "position": [ 250, 300 ], "parameters": {}, "typeVersion": 1, "id": "create-update-and-read-strapi-post-0" }, { "name": "Set post details", "type": "n8n-nodes-base.code", "typeVersion": 2, "parameters": { "mode": "runOnceForAllItems", "jsCode": "const instanceUrl='',title='',content='',description='',slug='';if(!/^https:\\/\\/[^\\s@?#]+$/i.test(instanceUrl)||![title,content,description,slug].every(s=>s.trim()))throw Error('Enter an HTTPS instance and post fields');return [{json:{url:instanceUrl.replace(/\\/$/,'')+'/api/posts',body:{data:{Title:title,Content:content,Description:description}},slug}}];" }, "id": "create-update-and-read-strapi-post-1" }, { "name": "Create post", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "parameters": { "method": "POST", "url": "={{ $json.url }}", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "sendBody": true, "contentType": "json", "specifyBody": "json", "jsonBody": "={{ JSON.stringify($json.body) }}", "options": { "timeout": 30000 } }, "id": "create-update-and-read-strapi-post-2" }, { "name": "Prepare slug update", "type": "n8n-nodes-base.code", "typeVersion": 2, "parameters": { "jsCode": "const s=$('Set post details').first().json,id=$input.first().json.data?.documentId;if(typeof id!=='string'||!/^[A-Za-z0-9_-]+$/.test(id))throw Error('Missing document ID');return [{json:{url:s.url+'/'+encodeURIComponent(id),id,body:{data:{slug:s.slug}}}}];" }, "id": "create-update-and-read-strapi-post-3" }, { "name": "Update slug", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "parameters": { "method": "PUT", "url": "={{ $json.url }}", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "sendBody": true, "contentType": "json", "specifyBody": "json", "jsonBody": "={{ JSON.stringify($json.body) }}", "options": { "timeout": 30000 } }, "id": "create-update-and-read-strapi-post-4" }, { "name": "Confirm update", "type": "n8n-nodes-base.code", "typeVersion": 2, "parameters": { "jsCode": "const d=$input.first().json.data,p=$('Prepare slug update').first().json,s=$('Set post details').first().json;if(d?.documentId!==p.id||d?.slug!==s.slug)throw Error('Update not confirmed');return [{json:{url:p.url,id:p.id}}];" }, "id": "create-update-and-read-strapi-post-5" }, { "name": "Read post", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "parameters": { "method": "GET", "url": "={{ $json.url }}", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "options": { "timeout": 30000 } }, "id": "create-update-and-read-strapi-post-6" }, { "name": "Confirm post", "type": "n8n-nodes-base.code", "typeVersion": 2, "parameters": { "jsCode": "const d=$input.first().json.data,id=$('Prepare slug update').first().json.id;if(d?.documentId!==id)throw Error('Wrong document');return [{json:{post:d}}];" }, "id": "create-update-and-read-strapi-post-7" } ], "active": false, "settings": {}, "connections": { "On clicking 'execute'": { "main": [ [ { "node": "Set post details", "type": "main", "index": 0 } ] ] }, "Set post details": { "main": [ [ { "node": "Create post", "type": "main", "index": 0 } ] ] }, "Create post": { "main": [ [ { "node": "Prepare slug update", "type": "main", "index": 0 } ] ] }, "Prepare slug update": { "main": [ [ { "node": "Update slug", "type": "main", "index": 0 } ] ] }, "Update slug": { "main": [ [ { "node": "Confirm update", "type": "main", "index": 0 } ] ] }, "Confirm update": { "main": [ [ { "node": "Read post", "type": "main", "index": 0 } ] ] }, "Read post": { "main": [ [ { "node": "Confirm post", "type": "main", "index": 0 } ] ] } }, "pinData": {} }