{
  "name": "Domain Status API",
  "nodes": [
    {
      "parameters": {
        "path": "domain-status",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "const authHeader = $input.first().json.headers?.authorization || '';\nconst expectedKey = 'demo-key-change-me';\n\nif (authHeader !== `Bearer ${expectedKey}`) {\n  return [{ json: { authorized: false, error: 'Unauthorized' } }];\n}\nreturn [{ json: { authorized: true } }];"
      },
      "id": "auth-check",
      "name": "Auth Check",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        464,
        400
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.authorized }}",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "is-auth",
      "name": "Is Authorized?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        688,
        400
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ error: 'Unauthorized' }) }}",
        "options": {
          "responseCode": 401
        }
      },
      "id": "respond-unauth",
      "name": "Respond Unauthorized",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        912,
        560
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT DISTINCT ON (d.id) d.domain, dc.checked_at, dc.spf_valid, dc.dkim_valid, dc.dmarc_valid, dc.overall_status as status, jsonb_array_length(COALESCE(dc.blacklist_hits, '[]'::jsonb)) as blacklist_hits, jsonb_array_length(COALESCE(dc.mx_records, '[]'::jsonb)) as mx_count FROM monitored_domains d LEFT JOIN domain_checks dc ON dc.domain_id = d.id WHERE d.is_active = true ORDER BY d.id, dc.checked_at DESC",
        "options": {}
      },
      "id": "query-status",
      "name": "Query Latest Status",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        912,
        304
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const rows = $input.all().map(item => item.json);\nreturn [{ json: { domains: rows, checked_count: rows.length, last_updated: new Date().toISOString() } }];"
      },
      "id": "format-response",
      "name": "Format Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        304
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "respond-ok",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1344,
        304
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Auth Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Auth Check": {
      "main": [
        [
          {
            "node": "Is Authorized?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Authorized?": {
      "main": [
        [
          {
            "node": "Query Latest Status",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Unauthorized",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Query Latest Status": {
      "main": [
        [
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true,
    "saveExecutionProgress": true
  }
}