{
  "info": {
    "name": "Timbrale API",
    "description": "Facturación electrónica SIFEN. La variable apiKey trae la clave demo pública: emití sin crear cuenta. Para tus propios datos, poné tu clave (Panel → Claves de API). Referencia: https://timbrale.com.py/docs — en Markdown para agentes de IA: https://timbrale.com.py/llms.txt",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    {
      "key": "baseUrl",
      "value": "https://api.timbrale.com.py",
      "type": "string"
    },
    {
      "key": "apiKey",
      "value": "sk_test_PTBJ4kRP.bI1MOdIBMdFJ2lUVhPbHbPHKpfMkr2kj5uM-lKb9WTs",
      "type": "string"
    },
    {
      "key": "establecimiento",
      "value": "",
      "type": "string"
    },
    {
      "key": "puntoExpedicion",
      "value": "",
      "type": "string"
    },
    {
      "key": "documentoId",
      "value": "",
      "type": "string"
    }
  ],
  "auth": {
    "type": "bearer",
    "bearer": [
      {
        "key": "token",
        "value": "{{apiKey}}",
        "type": "string"
      }
    ]
  },
  "item": [
    {
      "name": "Emitir un documento",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Idempotency-Key",
            "value": "{{$guid}}",
            "description": "Obligatorio. Reenviar la misma clave devuelve el documento anterior en lugar de emitir otro."
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos"
          ]
        },
        "body": {
          "mode": "raw",
          "options": {
            "raw": {
              "language": "json"
            }
          },
          "raw": "{\n  \"tipoDocumento\": \"factura\",\n  \"establecimiento\": \"{{establecimiento}}\",\n  \"puntoExpedicion\": \"{{puntoExpedicion}}\",\n  \"moneda\": \"PYG\",\n  \"tipoTransaccion\": 2,\n  \"condicion\": {\n    \"tipo\": 1,\n    \"entregas\": [\n      {\n        \"tipo\": 1,\n        \"monto\": \"450000\",\n        \"moneda\": \"PYG\"\n      }\n    ]\n  },\n  \"factura\": {\n    \"presencia\": 1\n  },\n  \"cliente\": {\n    \"contribuyente\": true,\n    \"ruc\": \"80069590-9\",\n    \"razonSocial\": \"EMPRESA DE PRUEBA S.A.\",\n    \"tipoOperacion\": 1,\n    \"email\": \"facturacion@demo.com.py\"\n  },\n  \"items\": [\n    {\n      \"codigo\": \"SRV-01\",\n      \"descripcion\": \"Servicio mensual\",\n      \"cantidad\": 1,\n      \"precioUnitario\": \"450000\",\n      \"iva\": 10\n    }\n  ]\n}"
        },
        "description": "202 Accepted: el documento tiene número y CDC y quedó en cola para firma y transmisión. condicion.tipo=1 (contado) exige condicion.entregas[] con la forma de pago; tipo=2 (crédito) exige condicion.credito."
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "// Llena establecimiento y punto con los que tu timbrado autoriza (GET /v1/documentos/opciones), en cada envío: cambiar de clave no deja un par viejo.",
              "// En sandbox la DNIT solo acepta el establecimiento 001 con los puntos 001 a 003.",
              "pm.sendRequest({",
              "  url: pm.variables.get('baseUrl') + '/v1/documentos/opciones',",
              "  header: { Authorization: 'Bearer ' + pm.variables.get('apiKey') },",
              "}, (err, res) => {",
              "  // Sin respuesta útil queda 001-001, el par del ejemplo; el 422 dice qué falta.",
              "  pm.collectionVariables.set('establecimiento', '001');",
              "  pm.collectionVariables.set('puntoExpedicion', '001');",
              "  if (err || res.code !== 200) return;",
              "  const sandbox = String(pm.variables.get('apiKey')).startsWith('sk_test_');",
              "  for (const e of res.json()) {",
              "    if (sandbox && e.codigo !== '001') continue;",
              "    const p = e.expeditionPoints.find((p) => p.tipos.includes('factura') && (!sandbox || ['001', '002', '003'].includes(p.codigo)));",
              "    if (!p) continue;",
              "    pm.collectionVariables.set('establecimiento', e.codigo);",
              "    pm.collectionVariables.set('puntoExpedicion', p.codigo);",
              "    return;",
              "  }",
              "});"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const b = pm.response.json();",
              "if (b && b.id) pm.collectionVariables.set('documentoId', b.id);"
            ]
          }
        }
      ]
    },
    {
      "name": "Listar documentos",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos?take=25",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos"
          ],
          "query": [
            {
              "key": "q",
              "value": "",
              "description": "CDC o documento del receptor",
              "disabled": true
            },
            {
              "key": "status",
              "value": "",
              "disabled": true
            },
            {
              "key": "tipoDocumento",
              "value": "",
              "disabled": true
            },
            {
              "key": "take",
              "value": "25",
              "description": "1-100"
            },
            {
              "key": "skip",
              "value": "0",
              "disabled": true
            }
          ]
        }
      }
    },
    {
      "name": "Consultar un documento",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos/{{documentoId}}",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos",
            "{{documentoId}}"
          ]
        },
        "description": "Se consulta hasta que status sea approved o rejected."
      }
    },
    {
      "name": "XML del documento",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/xml"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos/{{documentoId}}/xml",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos",
            "{{documentoId}}",
            "xml"
          ]
        }
      }
    },
    {
      "name": "KuDE en PDF",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/pdf"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos/{{documentoId}}/kude.pdf",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos",
            "{{documentoId}}",
            "kude.pdf"
          ]
        },
        "description": "El documento que recibe el receptor. 404 hasta que el SIFEN aprueba y el worker genera el PDF; el cuerpo dice cuál de las dos cosas falta."
      }
    },
    {
      "name": "KuDE en HTML (vista previa)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "text/html"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos/{{documentoId}}/kude",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos",
            "{{documentoId}}",
            "kude"
          ]
        },
        "description": "Disponible desde el primer segundo. Antes de la aprobación sale con la franja DOCUMENTO EN PROCESO DE APROBACIÓN — SIN VALIDEZ FISCAL."
      }
    },
    {
      "name": "Contexto de la clave",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/v1/contexto",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "contexto"
          ]
        },
        "description": "Ambiente de la clave, empresa emisora, establecimientos y timbrados vigentes."
      }
    },
    {
      "name": "Opciones (establecimientos y puntos)",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/v1/documentos/opciones",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v1",
            "documentos",
            "opciones"
          ]
        }
      }
    }
  ]
}