{
  "name": "Shopping agent with spending limits (Sealgraf)",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ],
      "id": "start"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "item",
              "value": "Nitrile gloves, case of 1,000",
              "type": "string"
            },
            {
              "id": "a2",
              "name": "store",
              "value": "Example Supply Co",
              "type": "string"
            },
            {
              "id": "a3",
              "name": "amount_minor",
              "value": 2500,
              "type": "number"
            },
            {
              "id": "a4",
              "name": "order_ref",
              "value": "={{ 'order-' + $now.toFormat('yyyyMMddHHmmss') }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "name": "Purchase details",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ],
      "id": "purchase-details",
      "notes": "What the agent wants to buy. Amounts are in cents: 2500 = $25.00.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "operation": "checkPermission",
        "category": "office_supplies",
        "amountMinor": "={{ $json.amount_minor }}",
        "currency": "USD",
        "counterparty": "={{ $json.store }}",
        "itemDescription": "={{ $json.item }}",
        "idempotencyKey": "={{ $json.order_ref }}"
      },
      "name": "Check with Sealgraf",
      "type": "n8n-nodes-sealgraf.sealgraf",
      "typeVersion": 1.1,
      "position": [
        440,
        300
      ],
      "id": "check-with-sealgraf",
      "credentials": {
        "sealgrafApi": {
          "id": "",
          "name": "Sealgraf account"
        }
      },
      "notes": "Checks the purchase against the limits you set in Sealgraf. Never moves money.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "expression",
        "numberOutputs": 3,
        "output": "={{ { allow: 0, needs_human_approval: 1, deny: 2 }[$json.outcome] }}",
        "options": {}
      },
      "name": "Route by answer",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        660,
        300
      ],
      "id": "route-by-answer",
      "notes": "0 allowed · 1 needs your approval · 2 declined",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "name": "Buy it (allowed)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        900,
        120
      ],
      "id": "buy-it-allowed",
      "notes": "Replace with your real purchase step.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "operation": "completePurchase",
        "preauthId": "={{ $('Check with Sealgraf').item.json.preauth_id }}",
        "decisionToken": "={{ $('Check with Sealgraf').item.json.decision_token }}",
        "completionNonce": "={{ $('Purchase details').item.json.order_ref }}",
        "amountChargedMinor": "={{ $('Purchase details').item.json.amount_minor }}",
        "evidence": {
          "note": "={{ $('Purchase details').item.json.item }}"
        }
      },
      "name": "Report the purchase",
      "type": "n8n-nodes-sealgraf.sealgraf",
      "typeVersion": 1.1,
      "position": [
        1120,
        120
      ],
      "id": "report-the-purchase",
      "credentials": {
        "sealgrafApi": {
          "id": "",
          "name": "Sealgraf account"
        }
      },
      "notes": "Seals the receipt.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "amount": 10,
        "unit": "minutes"
      },
      "name": "Wait for your approval",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        900,
        300
      ],
      "id": "wait-for-your-approval",
      "notes": "Approve or decline on Sealgraf in the meantime (you get an alert).",
      "notesInFlow": true
    },
    {
      "parameters": {
        "operation": "claimDecisionToken",
        "preauthId": "={{ $('Check with Sealgraf').item.json.preauth_id }}"
      },
      "name": "Collect the approval",
      "type": "n8n-nodes-sealgraf.sealgraf",
      "typeVersion": 1.1,
      "position": [
        1120,
        300
      ],
      "id": "collect-the-approval",
      "credentials": {
        "sealgrafApi": {
          "id": "",
          "name": "Sealgraf account"
        }
      },
      "notes": "Fails if you declined or haven't decided; nothing is bought then.",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "name": "Buy it (approved)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ],
      "id": "buy-it-approved",
      "notes": "Replace with your real purchase step.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "operation": "completePurchase",
        "preauthId": "={{ $('Check with Sealgraf').item.json.preauth_id }}",
        "decisionToken": "={{ $('Collect the approval').item.json.decision_token }}",
        "completionNonce": "={{ $('Purchase details').item.json.order_ref }}",
        "amountChargedMinor": "={{ $('Purchase details').item.json.amount_minor }}",
        "evidence": {
          "note": "={{ $('Purchase details').item.json.item }}"
        }
      },
      "name": "Report the approved purchase",
      "type": "n8n-nodes-sealgraf.sealgraf",
      "typeVersion": 1.1,
      "position": [
        1560,
        300
      ],
      "id": "report-the-approved-purchase",
      "credentials": {
        "sealgrafApi": {
          "id": "",
          "name": "Sealgraf account"
        }
      },
      "notes": "Seals the receipt.",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "name": "Declined: don't buy",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        900,
        480
      ],
      "id": "declined:-dont-buy",
      "notes": "Outside your limits. Nothing is bought.",
      "notesInFlow": true
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Purchase details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Purchase details": {
      "main": [
        [
          {
            "node": "Check with Sealgraf",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check with Sealgraf": {
      "main": [
        [
          {
            "node": "Route by answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by answer": {
      "main": [
        [
          {
            "node": "Buy it (allowed)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait for your approval",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Declined: don't buy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buy it (allowed)": {
      "main": [
        [
          {
            "node": "Report the purchase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for your approval": {
      "main": [
        [
          {
            "node": "Collect the approval",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Collect the approval": {
      "main": [
        [
          {
            "node": "Buy it (approved)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buy it (approved)": {
      "main": [
        [
          {
            "node": "Report the approved purchase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
