{
  "name": "Gmail Reply",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "gmail-reply",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "message",
        "operation": "get",
        "messageId": "={{ $json.message_id }}",
        "options": {}
      },
      "name": "Get Original Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        450,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "sendTo": "={{ $json.payload.headers.find(h => h.name === 'From').value }}",
        "subject": "=Re: {{ $json.payload.headers.find(h => h.name === 'Subject').value }}",
        "message": "={{ $('Webhook').item.json.reply_text }}",
        "options": {
          "threadId": "={{ $json.threadId }}"
        }
      },
      "name": "Send Reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        650,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { status: 'sent', message_id: $json.id, thread_id: $json.threadId } }}"
      },
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Get Original Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Original Email": {
      "main": [
        [
          {
            "node": "Send Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Reply": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}