{
  "name": "blue·auth · Dynamic (source system → send for signature)",
  "meta": {
    "templateCreatedBy": "blue·auth",
    "description": "Dynamic production workflow: your system sends a document and signers to an input webhook; n8n forwards them to blue·auth with {{ }} expressions and returns the requestId. A second lane receives signature events."
  },
  "nodes": [
    {
      "parameters": {
        "content": "## ⚙️ One-time setup\n1. Add a **Header Auth** credential to **Create in blue·auth**: `Authorization` = `Bearer ba_live_…`.\n2. Copy the **Production URL** from **Events** (`/webhook/blueauth-events`) and replace `EVENTS_WEBHOOK_URL` in the Create node, or let your system send `webhookUrl` in the request body.\n3. Set the workflow to **Active**.",
        "height": 190,
        "width": 980,
        "color": 7
      },
      "id": "note-setup",
      "name": "Sticky · Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [200, 20]
    },
    {
      "parameters": {
        "content": "## 🟦 Lane A · your system → blue·auth (dynamic)\nYour system sends a **POST** request to the input webhook with:\n```\n{\n  \"documentName\": \"Contract.pdf\",\n  \"pdfBase64\": \"data:application/pdf;base64,...\",\n  \"signers\": [\n    { \"email\": \"...\", \"fullName\": \"...\", \"idNumber\": \"...\", \"selfSign\": true,\n      \"zones\": [{ \"page\":1, \"x\":60, \"y\":700, \"width\":180, \"height\":60, \"type\":\"signature\" }] }\n  ]\n}\n```\nThe **Create** node uses `{{ $json.body.* }}` expressions, with no fixed values, and returns the `requestId` to your system.",
        "height": 380,
        "width": 980,
        "color": 5
      },
      "id": "note-laneA",
      "name": "Sticky · Lane A",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [200, 230]
    },
    {
      "parameters": {
        "content": "## 🟩 Lane B · Signature events\nblue·auth calls this webhook at each stage (sent/signed/completed/…). The workflow validates the HMAC and branches by event. `signature.completed` includes `signedPdfUrl`.",
        "height": 240,
        "width": 760,
        "color": 4
      },
      "id": "note-laneB",
      "name": "Sticky · Lane B",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [200, 640]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "send-for-signature",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-in",
      "name": "Input · Source system",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [260, 330],
      "webhookId": "blueauth-send-for-signature"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://blueauth.co/api/v1/signature-requests",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ documentName: $json.body.documentName, pdfBase64: $json.body.pdfBase64, storagePath: $json.body.storagePath, webhookUrl: ($json.body.webhookUrl || 'EVENTS_WEBHOOK_URL'), qr: ($json.body.qr || { enabled: true, placement: 'bottom-right', pages: 'first' }), signers: $json.body.signers }) }}",
        "options": {}
      },
      "id": "http-create",
      "name": "Create in blue·auth",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [560, 330],
      "credentials": { "httpHeaderAuth": { "id": "REPLACE", "name": "BlueAuth (Authorization: Bearer ba_…)" } }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ requestId: $json.requestId, status: $json.status, signers: $json.signers }) }}",
        "options": {}
      },
      "id": "respond",
      "name": "Return requestId",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [860, 330]
    },
    {
      "parameters": { "httpMethod": "POST", "path": "blueauth-events", "options": { "rawBody": true } },
      "id": "webhook-events",
      "name": "Events · blue·auth",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [260, 720],
      "webhookId": "blueauth-events"
    },
    {
      "parameters": {
        "jsCode": "const crypto = require('crypto');\nconst WEBHOOK_SECRET = $env.BLUEAUTH_WEBHOOK_SECRET || 'whsig_REPLACE';\nconst raw = JSON.stringify($input.first().json.body ?? $input.first().json);\nconst header = ($input.first().json.headers || {})['x-blueauth-signature'] || '';\nconst expected = crypto.createHmac('sha256', WEBHOOK_SECRET).update(raw, 'utf8').digest('hex');\nconst valid = header && expected.length === header.length && crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(header));\nconst event = ($input.first().json.headers || {})['x-blueauth-event'] || ($input.first().json.body || {}).event;\nreturn [{ json: { valid, event, payload: $input.first().json.body ?? $input.first().json } }];"
      },
      "id": "verify-hmac",
      "name": "Verify HMAC",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [560, 720]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            { "conditions": { "options": { "caseSensitive": true }, "conditions": [{ "leftValue": "={{ $json.event }}", "rightValue": "signature.completed", "operator": { "type": "string", "operation": "equals" } }] }, "outputKey": "completed" },
            { "conditions": { "options": { "caseSensitive": true }, "conditions": [{ "leftValue": "={{ $json.event }}", "rightValue": "signature.signed", "operator": { "type": "string", "operation": "equals" } }] }, "outputKey": "signed" }
          ]
        },
        "options": { "fallbackOutput": "extra" }
      },
      "id": "switch-event",
      "name": "Branch by event",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [820, 720]
    }
  ],
  "connections": {
    "Input · Source system": { "main": [[{ "node": "Create in blue·auth", "type": "main", "index": 0 }]] },
    "Create in blue·auth": { "main": [[{ "node": "Return requestId", "type": "main", "index": 0 }]] },
    "Events · blue·auth": { "main": [[{ "node": "Verify HMAC", "type": "main", "index": 0 }]] },
    "Verify HMAC": { "main": [[{ "node": "Branch by event", "type": "main", "index": 0 }]] }
  },
  "pinData": {},
  "settings": { "executionOrder": "v1" }
}
