{
  "info": {
    "name": "Facturador Bluivee",
    "description": "API de facturación electrónica Chile (SII). Variables: `baseUrl`, `token`, `tenantId`, `apiKey`, `dteId`.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "baseUrl", "value": "http://localhost:4100" },
    { "key": "token", "value": "" },
    { "key": "tenantId", "value": "" },
    { "key": "apiKey", "value": "" },
    { "key": "dteId", "value": "" },
    { "key": "webhookId", "value": "" }
  ],
  "auth": {
    "type": "bearer",
    "bearer": [{ "key": "token", "value": "{{token}}", "type": "string" }]
  },
  "item": [
    {
      "name": "Auth",
      "item": [
        {
          "name": "Login",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "const j = pm.response.json();",
                  "if (j.token) pm.collectionVariables.set('token', j.token);",
                  "if (j.tenants?.[0]?.id) pm.collectionVariables.set('tenantId', j.tenants[0].id);"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "auth": { "type": "noauth" },
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"email\": \"admin@facturacion.local\",\n  \"password\": \"demo1234\"\n}"
            },
            "url": "{{baseUrl}}/api/auth/login"
          }
        },
        {
          "name": "Me",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/auth/me"
          }
        }
      ]
    },
    {
      "name": "Tenants",
      "item": [
        {
          "name": "List tenants",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants"
          }
        },
        {
          "name": "Readiness",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/readiness"
          }
        },
        {
          "name": "Dashboard",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/dashboard"
          }
        }
      ]
    },
    {
      "name": "Maestros",
      "item": [
        {
          "name": "List customers",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/customers"
          }
        },
        {
          "name": "Create customer",
          "request": {
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"rut\": \"11.111.111-1\",\n  \"businessName\": \"Cliente API\"\n}"
            },
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/customers"
          }
        },
        {
          "name": "List products",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/products"
          }
        },
        {
          "name": "Create API key",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "const j = pm.response.json();",
                  "if (j.apiKey?.secret) pm.collectionVariables.set('apiKey', j.apiKey.secret);"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"name\": \"postman\"\n}"
            },
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/api-keys"
          }
        }
      ]
    },
    {
      "name": "DTE",
      "item": [
        {
          "name": "List DTE",
          "request": {
            "method": "GET",
            "url": {
              "raw": "{{baseUrl}}/api/dte?tenantId={{tenantId}}",
              "host": ["{{baseUrl}}"],
              "path": ["api", "dte"],
              "query": [{ "key": "tenantId", "value": "{{tenantId}}" }]
            }
          }
        },
        {
          "name": "Emit factura 33",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "const j = pm.response.json();",
                  "if (j.id) pm.collectionVariables.set('dteId', j.id);"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"tenantId\": \"{{tenantId}}\",\n  \"payload\": {\n    \"type\": \"FACTURA_33\",\n    \"issuer\": {\n      \"rut\": \"76.123.456-7\",\n      \"businessName\": \"Empresa Demo SpA\",\n      \"activity\": \"Servicios\",\n      \"address\": \"Av. Principal 123\",\n      \"commune\": \"Santiago\",\n      \"city\": \"Santiago\"\n    },\n    \"receiver\": {\n      \"rut\": \"11.111.111-1\",\n      \"businessName\": \"Cliente Demo\",\n      \"activity\": \"Comercio\",\n      \"address\": \"Calle 1\",\n      \"commune\": \"Providencia\",\n      \"city\": \"Santiago\"\n    },\n    \"items\": [\n      {\n        \"name\": \"Servicio mensual\",\n        \"quantity\": 1,\n        \"unitPrice\": 100000,\n        \"isExempt\": false\n      }\n    ]\n  }\n}"
            },
            "url": "{{baseUrl}}/api/dte"
          }
        },
        {
          "name": "Get DTE",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/dte/{{dteId}}"
          }
        },
        {
          "name": "Download PDF",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/dte/{{dteId}}/pdf"
          }
        },
        {
          "name": "Refresh SII status",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/dte/{{dteId}}/refresh-status"
          }
        }
      ]
    },
    {
      "name": "Webhooks",
      "item": [
        {
          "name": "List webhooks",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/webhooks"
          }
        },
        {
          "name": "Create webhook",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "const j = pm.response.json();",
                  "if (j.webhook?.id) pm.collectionVariables.set('webhookId', j.webhook.id);"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"url\": \"https://webhook.site/your-unique-id\",\n  \"events\": [\"dte.emitted\", \"dte.status_updated\", \"payment.completed\"]\n}"
            },
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/webhooks"
          }
        },
        {
          "name": "List deliveries",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/webhooks/deliveries?limit=20"
          }
        },
        {
          "name": "Deactivate webhook",
          "request": {
            "method": "DELETE",
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/webhooks/{{webhookId}}"
          }
        }
      ]
    },
    {
      "name": "API v1",
      "item": [
        {
          "name": "Emit factura 33 (idempotente)",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "exec": [
                  "pm.collectionVariables.set('idempotencyKey', 'emit-' + Date.now());"
                ],
                "type": "text/javascript"
              }
            },
            {
              "listen": "test",
              "script": {
                "exec": [
                  "const j = pm.response.json();",
                  "if (j.id) pm.collectionVariables.set('dteId', j.id);"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "Idempotency-Key", "value": "{{idempotencyKey}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"tenantId\": \"{{tenantId}}\",\n  \"payload\": {\n    \"type\": \"FACTURA_33\",\n    \"issuer\": {\n      \"rut\": \"76.123.456-7\",\n      \"businessName\": \"Empresa Demo SpA\",\n      \"activity\": \"Servicios\",\n      \"address\": \"Av. Principal 123\",\n      \"commune\": \"Santiago\",\n      \"city\": \"Santiago\"\n    },\n    \"receiver\": {\n      \"rut\": \"11.111.111-1\",\n      \"businessName\": \"Cliente Demo\",\n      \"activity\": \"Comercio\",\n      \"address\": \"Calle 1\",\n      \"commune\": \"Providencia\",\n      \"city\": \"Santiago\"\n    },\n    \"items\": [\n      {\n        \"name\": \"Servicio mensual\",\n        \"quantity\": 1,\n        \"unitPrice\": 100000,\n        \"isExempt\": false\n      }\n    ]\n  }\n}"
            },
            "url": "{{baseUrl}}/api/v1/dte"
          }
        },
        {
          "name": "Emit guía 52",
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "Idempotency-Key", "value": "guia-{{$timestamp}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"tenantId\": \"{{tenantId}}\",\n  \"payload\": {\n    \"type\": \"GUIA_52\",\n    \"issueDate\": \"2026-08-27\",\n    \"issuer\": {\n      \"rut\": \"76.123.456-7\",\n      \"businessName\": \"Empresa Demo SpA\",\n      \"activity\": \"Servicios\",\n      \"address\": \"Av. Principal 123\",\n      \"commune\": \"Santiago\",\n      \"city\": \"Santiago\"\n    },\n    \"receiver\": {\n      \"rut\": \"11.111.111-1\",\n      \"businessName\": \"Cliente Demo\",\n      \"activity\": \"Comercio\",\n      \"address\": \"Calle 1\",\n      \"commune\": \"Providencia\",\n      \"city\": \"Santiago\"\n    },\n    \"guia\": {\n      \"indTraslado\": 5,\n      \"transport\": {\n        \"plate\": \"ABCD12\",\n        \"destinationAddress\": \"Av. Destino 100\",\n        \"destinationCommune\": \"Providencia\"\n      }\n    },\n    \"items\": [\n      {\n        \"lineNumber\": 1,\n        \"name\": \"Mercadería traslado interno\",\n        \"quantity\": 10,\n        \"unitPrice\": 0,\n        \"exempt\": true\n      }\n    ]\n  }\n}"
            },
            "url": "{{baseUrl}}/api/v1/dte"
          }
        }
      ]
    },
    {
      "name": "Payments (public)",
      "auth": { "type": "noauth" },
      "item": [
        {
          "name": "Payment info",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/payments/{{dteId}}"
          }
        },
        {
          "name": "Init Webpay",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/payments/{{dteId}}/webpay/init"
          }
        }
      ]
    },
    {
      "name": "API Key auth example",
      "auth": { "type": "noauth" },
      "item": [
        {
          "name": "Readiness (X-Api-Key)",
          "request": {
            "method": "GET",
            "header": [{ "key": "X-Api-Key", "value": "{{apiKey}}" }],
            "url": "{{baseUrl}}/api/tenants/{{tenantId}}/readiness"
          }
        }
      ]
    }
  ]
}
