{
  "info": {
    "name": "eFacture Connect API",
    "description": "Collection Postman pour l'integration de l'API eFacture Connect (TEIF v1.8.8 + XAdES + depot TTN).\n\nDocumentation : https://efacturetn.com/documentation-api\nGuide : https://efacturetn.com/documentation-api/guide",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "version": "1.0.0"
  },
  "variable": [
    {
      "key": "base_url",
      "value": "https://efacturetn.com",
      "description": "URL de base (https://efacturetn.com en prod, http://localhost:8000 en dev)"
    },
    {
      "key": "api_key",
      "value": "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx",
      "description": "Cle API : sk_test_xxx (test) ou sk_live_xxx (prod)"
    },
    {
      "key": "signing_id",
      "value": "",
      "description": "Rempli automatiquement apres /einvoices/prepare (test script)"
    }
  ],
  "auth": {
    "type": "apikey",
    "apikey": [
      {"key": "key", "value": "X-Api-Key", "type": "string"},
      {"key": "value", "value": "{{api_key}}", "type": "string"},
      {"key": "in", "value": "header", "type": "string"}
    ]
  },
  "item": [
    {
      "name": "0. Status — Health check",
      "request": {
        "method": "GET",
        "header": [],
        "url": {"raw": "{{base_url}}/api/v1/status", "host": ["{{base_url}}"], "path": ["api", "v1", "status"]},
        "description": "Verifie que l'API est en ligne. Pas d'authentification requise."
      }
    },
    {
      "name": "1. Validate — Verifier le payload (0 credit)",
      "request": {
        "method": "POST",
        "header": [{"key": "Content-Type", "value": "application/json"}],
        "url": {"raw": "{{base_url}}/api/v1/einvoices/validate", "host": ["{{base_url}}"], "path": ["api", "v1", "einvoices", "validate"]},
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_number\": \"F-2026-001\",\n  \"date\": \"2026-06-13\",\n  \"seller\": {\n    \"name\": \"Mon Entreprise SARL\",\n    \"tax_id\": \"1234567MAM000\",\n    \"address\": \"Tunis\"\n  },\n  \"buyer\": {\n    \"name\": \"Client SARL\",\n    \"tax_id\": \"7654321XAM000\"\n  },\n  \"items\": [\n    {\n      \"description\": \"Service de conseil\",\n      \"quantity\": 1,\n      \"unit_price\": 500,\n      \"vat_rate\": 19\n    }\n  ]\n}"
        },
        "description": "Valide la structure du payload sans signer. Aucun credit consomme. Utile pour debugger l'integration."
      }
    },
    {
      "name": "2a. SEAL — Process (sign + TTN, synchrone)",
      "request": {
        "method": "POST",
        "header": [{"key": "Content-Type", "value": "application/json"}],
        "url": {"raw": "{{base_url}}/api/v1/einvoices/process", "host": ["{{base_url}}"], "path": ["api", "v1", "einvoices", "process"]},
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_number\": \"F-2026-001\",\n  \"date\": \"2026-06-13\",\n  \"due_date\": \"2026-07-13\",\n  \"seller\": {\n    \"name\": \"Mon Entreprise SARL\",\n    \"tax_id\": \"1234567MAM000\",\n    \"address\": \"Tunis\"\n  },\n  \"buyer\": {\n    \"name\": \"Client SARL\",\n    \"tax_id\": \"7654321XAM000\"\n  },\n  \"items\": [\n    {\n      \"description\": \"Audit comptable 2026\",\n      \"quantity\": 1,\n      \"unit_price\": 3500,\n      \"vat_rate\": 19\n    },\n    {\n      \"description\": \"Formation equipe (3 jours)\",\n      \"quantity\": 3,\n      \"unit_price\": 800,\n      \"vat_rate\": 19\n    }\n  ],\n  \"stamp_duty\": true\n}"
        },
        "description": "Mode SEAL — synchrone. Signature automatique (cachet electronique) + depot TTN. Reponse contient xml_base64 + pdf_base64 + ttn_reference.\n\nEn mode test (sk_test_), signature mockee et ttn_reference au format TTN-TEST-XXXXX."
      }
    },
    {
      "name": "2b. SEAL — Avec retenue a la source",
      "request": {
        "method": "POST",
        "header": [{"key": "Content-Type", "value": "application/json"}],
        "url": {"raw": "{{base_url}}/api/v1/einvoices/process", "host": ["{{base_url}}"], "path": ["api", "v1", "einvoices", "process"]},
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_number\": \"F-2026-042\",\n  \"date\": \"2026-06-13\",\n  \"seller\": {\n    \"name\": \"Cabinet Conseil\",\n    \"tax_id\": \"1234567MAM000\",\n    \"address\": \"Tunis\"\n  },\n  \"buyer\": {\n    \"name\": \"Societe Industrielle SA\",\n    \"tax_id\": \"7654321XAM000\"\n  },\n  \"items\": [\n    {\n      \"description\": \"Consultation expert-comptable\",\n      \"quantity\": 10,\n      \"unit_price\": 250,\n      \"vat_rate\": 19\n    }\n  ],\n  \"stamp_duty\": true,\n  \"withholding_tax_rate\": 1.5\n}"
        },
        "description": "Exemple avec retenue a la source 1.5% (Personne physique IR). Taux possibles : 1.5, 3, 10, 15, 20."
      }
    },
    {
      "name": "3a. DigiGo — Prepare (async)",
      "event": [
        {
          "listen": "test",
          "script": {
            "exec": [
              "if (pm.response.code === 200) {",
              "    const data = pm.response.json().data;",
              "    if (data && data.signing_id) {",
              "        pm.collectionVariables.set('signing_id', data.signing_id);",
              "        console.log('signing_id sauvegarde : ' + data.signing_id);",
              "        console.log('Rediriger le client vers : ' + data.signing_url);",
              "    }",
              "}"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [{"key": "Content-Type", "value": "application/json"}],
        "url": {"raw": "{{base_url}}/api/v1/einvoices/prepare", "host": ["{{base_url}}"], "path": ["api", "v1", "einvoices", "prepare"]},
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_number\": \"F-2026-001\",\n  \"date\": \"2026-06-13\",\n  \"seller\": {\n    \"name\": \"Mon Entreprise SARL\",\n    \"tax_id\": \"1234567MAM000\",\n    \"address\": \"Tunis\"\n  },\n  \"buyer\": {\n    \"name\": \"Client SARL\",\n    \"tax_id\": \"7654321XAM000\"\n  },\n  \"items\": [\n    {\n      \"description\": \"Service de conseil\",\n      \"quantity\": 1,\n      \"unit_price\": 500,\n      \"vat_rate\": 19\n    }\n  ],\n  \"method\": \"digigo\",\n  \"return_url\": \"https://votre-erp.tn/factures/F-2026-001/retour-signature\"\n}"
        },
        "description": "Mode DigiGo — asynchrone. Retourne signing_id + signing_url. Le test script sauvegarde automatiquement signing_id dans la variable de collection pour les requetes suivantes.\n\nVotre ERP doit ensuite rediriger le client vers signing_url. Le client saisit PIN + OTP, puis est redirige vers return_url."
      }
    },
    {
      "name": "3b. DigiGo — Polling status",
      "request": {
        "method": "GET",
        "header": [],
        "url": {"raw": "{{base_url}}/api/v1/einvoices/{{signing_id}}", "host": ["{{base_url}}"], "path": ["api", "v1", "einvoices", "{{signing_id}}"]},
        "description": "Polling sur signing_id (auto-rempli par 3a).\n\nStatuts possibles :\n- pending_signature : client n'a pas encore signe\n- signed : signature OK, depot TTN en cours\n- submitted_ttn : depose, attente validation TTN\n- validated : termine — xml_base64 + pdf_base64 + ttn_reference disponibles\n- rejected : annule par le client ou rejete par TTN\n\nBackoff recommande : 5s, 10s, 20s, 30s, 60s..."
      }
    },
    {
      "name": "4. Credits — Balance",
      "request": {
        "method": "GET",
        "header": [],
        "url": {"raw": "{{base_url}}/api/v1/credits/balance", "host": ["{{base_url}}"], "path": ["api", "v1", "credits", "balance"]},
        "description": "Retourne le solde courant de credits e-facture, le total utilise et le total achete."
      }
    }
  ]
}
