{
  "name": "Save completed Volai call results to Google Sheets",
  "nodes": [
    {
      "parameters": {
        "content": "## 1. Set up your sheet and try the sample\n\n1. Create a **Call results** tab. Use these headers in row 1, in order:\n   `Call ID` · `Event time (UTC)` · `Direction` · `Status` · `Duration (s)` · `Mode`\n2. In **Write call result to Google Sheets**, select your Google Sheets OAuth2 credential and enter the spreadsheet ID and tab name.\n3. Disable **Confirm successful write** if you do not have a Volai API key. Run **Test with a sample call**. The same sample ID updates one row on repeated runs. No call is placed.\n\n[Setup guide and Volai signup](https://volai.eu/integrations/n8n-google-sheets)",
        "width": 570,
        "height": 325
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -660,
        -440
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000001",
      "name": "1. Set up your sheet and try the sample"
    },
    {
      "parameters": {
        "content": "## 2. Connect completed Volai calls\n\nUse a Volai API key in a **Header Auth** credential (`Authorization`, `Bearer <API key>`) on **Read Volai call event** and **Confirm successful write**. Create a `call.completed` subscription using the webhook's **Production URL**. Put its `deliveryToken` into a separate **Header Auth** credential named by header `X-Volai-Webhook-Token`, select it on the webhook, then publish. Change the webhook path if this instance already uses it.\n\nThe workflow verifies the incoming token and fetches the event from Volai. Only the six sheet columns are written. Keep one workflow per tab. Concurrent writes to the same Call ID can require duplicate cleanup. See the guide for the subscription request.",
        "width": 705,
        "height": 320
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -660,
        95
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000002",
      "name": "2. Connect completed Volai calls"
    },
    {
      "parameters": {
        "content": "## 3. Check the write and reporting\n\nA sheet error stops the run before confirmation. Check the credential, spreadsheet ID, tab name and headers. Execution data is not stored by default. To troubleshoot a live run, temporarily enable saving failed executions in workflow settings.\n\n**Confirm successful write** optionally reports the run type to Volai after the write. Live reports include event and subscription IDs for verification. No sheet rows are sent back. Volai counts only eligible registrations with measurement consent. Disable this node to opt out or run the sample without a Volai API key. A reporting error leaves the sheet write intact.\n\nTested with n8n **2.48.5**. Uses built-in nodes.",
        "width": 610,
        "height": 345
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        580,
        -440
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000003",
      "name": "3. Check the write and reporting"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -615,
        -65
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000004",
      "name": "Test with a sample call"
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "{\n  \"event\": \"call.completed\",\n  \"data\": {\n    \"id\": \"DEMO-call-result-001\",\n    \"direction\": \"in\",\n    \"status\": \"completed\",\n    \"durationSecs\": 42\n  },\n  \"ts\": 1794981600000,\n  \"sample\": true\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -365,
        -65
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000005",
      "name": "Sample call data"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "volai-call-result-to-sheets",
        "authentication": "headerAuth",
        "responseMode": "lastNode",
        "responseData": "noData",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -615,
        470
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000006",
      "name": "Volai call completed",
      "webhookId": "cf114fcb-49d7-4945-bbb0-0c5607eb4693"
    },
    {
      "parameters": {
        "jsCode": "const { body } = $input.first().json;\nconst subscriptionId = body?.subscription_id;\nconst eventId = body?.event_id;\nif (typeof subscriptionId !== 'string' || !/^whs_[a-f0-9]{32}$/.test(subscriptionId) ||\n    typeof eventId !== 'string' || !/^evt_[a-f0-9]{32}$/.test(eventId)) {\n  throw new Error('Volai delivery is missing valid subscription and event IDs.');\n}\nreturn [{ json: { subscriptionId, eventId } }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -365,
        470
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000007",
      "name": "Check delivery references"
    },
    {
      "parameters": {
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "url": "={{ 'https://volai.cz/v1/webhook-subscriptions/' + $json.subscriptionId + '/events/' + $json.eventId }}",
        "options": {
          "timeout": 3500,
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -105,
        470
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000008",
      "name": "Read Volai call event"
    },
    {
      "parameters": {
        "jsCode": "const payload = $input.first().json;\nconst { event, data, ts } = payload;\nif (event !== 'call.completed' || data?.status !== 'completed') {\n  throw new Error('This workflow accepts completed Volai calls.');\n}\nif (typeof data.id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(data.id) ||\n    !['in', 'out'].includes(data.direction) ||\n    !Number.isFinite(ts) || !Number.isFinite(new Date(ts).valueOf()) ||\n    !Number.isFinite(data.durationSecs) || data.durationSecs < 0) {\n  throw new Error('The Volai call result has an invalid ID, direction, timestamp, or duration.');\n}\nconst sample = payload.sample === true;\nreturn [{ json: {\n  row: {\n    'Call ID': data.id,\n    'Event time (UTC)': new Date(ts).toISOString(),\n    'Direction': data.direction === 'in' ? 'Inbound' : 'Outbound',\n    'Status': data.status,\n    'Duration (s)': data.durationSecs,\n    'Mode': sample ? 'sample' : 'live',\n  },\n  receipt: sample\n    ? { mode: 'sample' }\n    : { mode: 'live', subscriptionId: payload.subscription_id, eventId: payload.event_id },\n} }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        160,
        -65
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000009",
      "name": "Map call result"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Call results",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Call ID": "={{ $json.row['Call ID'] }}",
            "Event time (UTC)": "={{ $json.row['Event time (UTC)'] }}",
            "Direction": "={{ $json.row['Direction'] }}",
            "Status": "={{ $json.row['Status'] }}",
            "Duration (s)": "={{ $json.row['Duration (s)'] }}",
            "Mode": "={{ $json.row['Mode'] }}"
          },
          "matchingColumns": [
            "Call ID"
          ],
          "schema": [
            {
              "id": "Call ID",
              "displayName": "Call ID",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Event time (UTC)",
              "displayName": "Event time (UTC)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Direction",
              "displayName": "Direction",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Duration (s)",
              "displayName": "Duration (s)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true
            },
            {
              "id": "Mode",
              "displayName": "Mode",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "cellFormat": "RAW",
          "useAppend": true
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        440,
        -65
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000010",
      "name": "Write call result to Google Sheets"
    },
    {
      "parameters": {
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "method": "POST",
        "url": "https://volai.cz/api/integrations/n8n/call-sheet-run",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($('Map call result').first().json.receipt) }}",
        "options": {
          "timeout": 3500,
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        745,
        -65
      ],
      "id": "49ffc654-3eb1-4aa1-affe-000000000011",
      "name": "Confirm successful write",
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Test with a sample call": {
      "main": [
        [
          {
            "node": "Sample call data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample call data": {
      "main": [
        [
          {
            "node": "Map call result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Volai call completed": {
      "main": [
        [
          {
            "node": "Check delivery references",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check delivery references": {
      "main": [
        [
          {
            "node": "Read Volai call event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Volai call event": {
      "main": [
        [
          {
            "node": "Map call result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map call result": {
      "main": [
        [
          {
            "node": "Write call result to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write call result to Google Sheets": {
      "main": [
        [
          {
            "node": "Confirm successful write",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "none",
    "saveDataSuccessExecution": "none",
    "saveManualExecutions": false,
    "saveExecutionProgress": false
  }
}
