{
  "info": {
    "name": "Rebilix · Erste Sandbox-Rechnung",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "description": "RE-000001 erzeugen, Original und Prüfbericht herunterladen. Den Sandbox-Schlüssel als lokale geheime Umgebungsvariable apiKey setzen."
  },
  "auth": {
    "type": "bearer",
    "bearer": [
      {
        "key": "token",
        "value": "{{apiKey}}",
        "type": "string"
      }
    ]
  },
  "variable": [
    {
      "key": "baseUrl",
      "value": "https://app.rebilix.de",
      "type": "string"
    },
    {
      "key": "requestId",
      "value": "",
      "type": "string"
    },
    {
      "key": "documentId",
      "value": "",
      "type": "string"
    },
    {
      "key": "invoiceJson",
      "value": "",
      "type": "string"
    }
  ],
  "item": [
    {
      "name": "1 · Neue Beispielrechnung laden",
      "request": {
        "method": "GET",
        "url": "{{baseUrl}}/api/examples/invoice.json",
        "description": "Lädt die gemeinsame Beispielrechnung. Erneut ausführen, um einen neuen Vorgang zu beginnen.",
        "auth": {
          "type": "noauth"
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Beispiel geladen', () => pm.response.to.have.status(200));",
              "if (pm.response.code === 200) {",
              "  pm.collectionVariables.set('invoiceJson', JSON.stringify(pm.response.json()));",
              "  pm.collectionVariables.set('requestId', pm.variables.replaceIn('{{$guid}}'));",
              "  pm.collectionVariables.unset('documentId');",
              "}"
            ]
          }
        }
      ]
    },
    {
      "name": "2 · XRechnung erzeugen",
      "request": {
        "method": "POST",
        "url": "{{baseUrl}}/api/v1/sandbox/documents/generate",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Idempotency-Key",
            "value": "{{requestId}}"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{{invoiceJson}}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "description": "Erneut senden, um dieselbe Anfrage mit identischer ID zu wiederholen. Für einen neuen Vorgang Schritt 1 ausführen."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Dokument gespeichert', () => pm.response.to.have.status(201));",
              "pm.collectionVariables.unset('documentId');",
              "if (pm.response.code === 201) {",
              "  const doc = pm.response.json();",
              "  pm.collectionVariables.set('documentId', doc.id);",
              "  pm.test('Beispiel technisch gültig', () => pm.expect(doc.validationStatus).to.eql('valid'));",
              "}"
            ]
          }
        }
      ]
    },
    {
      "name": "3 · Original herunterladen",
      "request": {
        "method": "GET",
        "url": "{{baseUrl}}/api/v1/sandbox/documents/{{documentId}}/original",
        "description": "Unveränderte XRechnung-XML; Antwort als Datei speichern."
      }
    },
    {
      "name": "4 · Prüfbericht herunterladen",
      "request": {
        "method": "GET",
        "url": "{{baseUrl}}/api/v1/sandbox/documents/{{documentId}}/report",
        "description": "Technischen Prüfbericht separat speichern."
      }
    }
  ]
}
