{
  "name": "Ticket Triage — Gorgias Webhook → Tag via API",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "gorgias-triage",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Gorgias Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "gorgias-triage"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ticket = $input.first().json;\nconst subject = (ticket.subject || '').toLowerCase();\nconst body = (ticket.messages?.[0]?.body_text || '').toLowerCase();\nconst combined = `${subject} ${body}`;\n\nreturn [{\n  json: {\n    ticketId: ticket.id,\n    existingTags: ticket.tags || [],\n    text: combined,\n  }\n}];"
      },
      "id": "extract-text",
      "name": "Extract Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const text = $input.first().json.text;\nconst ticketId = $input.first().json.ticketId;\nconst existingTags = $input.first().json.existingTags;\n\nlet tag = 'needs-triage';\n\nif (/invoice|payment|refund|overcharged/.test(text)) {\n  tag = 'billing';\n} else if (/shipping|tracking|delivery|package|lost/.test(text)) {\n  tag = 'shipping';\n} else if (/return|exchange|cancel|rma/.test(text)) {\n  tag = 'returns';\n} else if (/error|bug|broken|not working|crash/.test(text)) {\n  tag = 'technical';\n} else if (/login|password|account|locked/.test(text)) {\n  tag = 'account';\n}\n\nreturn [{\n  json: { ticketId, tag, existingTags }\n}];"
      },
      "id": "classify-ticket",
      "name": "Classify Ticket",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://your-store.gorgias.com/api/tickets/{{ $json.ticketId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "options": {}
      },
      "id": "fetch-ticket",
      "name": "Fetch Current Tags",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [660, 0],
      "credentials": {
        "httpBasicAuth": {
          "id": "credential-id",
          "name": "Gorgias API"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const currentTags = $input.first().json.tags || [];\nconst newTag = $('Classify Ticket').first().json.tag;\n\nconst merged = [...currentTags, { name: newTag }].filter(\n  (tag, i, arr) => arr.findIndex(t => t.name === tag.name) === i\n);\n\nreturn [{ json: { mergedTags: merged, ticketId: $('Classify Ticket').first().json.ticketId } }];"
      },
      "id": "merge-tags",
      "name": "Merge Tags",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [880, 0]
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://your-store.gorgias.com/api/tickets/{{ $json.ticketId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ tags: $json.mergedTags }) }}",
        "options": {}
      },
      "id": "apply-tag",
      "name": "Apply Tag",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1100, 0],
      "credentials": {
        "httpBasicAuth": {
          "id": "credential-id",
          "name": "Gorgias API"
        }
      }
    }
  ],
  "connections": {
    "Gorgias Webhook": {
      "main": [
        [
          {
            "node": "Extract Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Text": {
      "main": [
        [
          {
            "node": "Classify Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Ticket": {
      "main": [
        [
          {
            "node": "Fetch Current Tags",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Current Tags": {
      "main": [
        [
          {
            "node": "Merge Tags",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Tags": {
      "main": [
        [
          {
            "node": "Apply Tag",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-gorgias-ticket-triage"
  }
}
