{
  "info": {
    "name": "MagniFinance REST API - Invoicing & Waybills",
    "description": "Create, read and cancel invoices, credit/debit notes and waybills.\n\n1. Open the collection's **Variables** tab and fill in `email` and `token` (see https://developer.magnifinance.com/finance/getting-started/authentication/).\n2. Send **Create Document** or **Create Waybill**. The returned `DocumentId` is stored in the `documentId` variable, so **Get Document**, **Get Document Property** and **Cancel Document** work right after.\n\nDates are filled in at send time (`today`, `dueDate`, `now`).\n\nDocs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "baseUrl", "value": "https://bo.magnifinance.com" },
    { "key": "email", "value": "", "description": "Your API user email" },
    { "key": "token", "value": "", "type": "secret", "description": "Your subscription token" },
    { "key": "documentId", "value": "", "description": "Filled in by Create Document / Create Waybill" }
  ],
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "const d = new Date();",
          "pm.variables.set('now', d.toISOString().slice(0, 19));",
          "pm.variables.set('today', d.toISOString().slice(0, 10));",
          "d.setDate(d.getDate() + 30);",
          "pm.variables.set('dueDate', d.toISOString().slice(0, 10));"
        ]
      }
    },
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "pm.test('HTTP 200', () => pm.response.to.have.status(200));",
          "if ((pm.response.headers.get('Content-Type') || '').includes('json')) {",
          "    const body = pm.response.json();",
          "    const error = body.ErrorValue ? `${body.ErrorValue.Name}: ${JSON.stringify(body.ValidationErrors || body.ErrorHumanReadable)}` : '';",
          "    pm.test('Type is Success', () => pm.expect(body.IsError, error).to.be.false);",
          "    if (body.Object && body.Object.DocumentId) pm.collectionVariables.set('documentId', body.Object.DocumentId);",
          "}"
        ]
      }
    }
  ],
  "item": [
    {
      "name": "Create Document",
      "request": {
        "method": "POST",
        "header": [
          { "key": "email", "value": "{{email}}" },
          { "key": "token", "value": "{{token}}" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "url": { "raw": "{{baseUrl}}/api/v1.1/document", "host": ["{{baseUrl}}"], "path": ["api", "v1.1", "document"] },
        "body": {
          "mode": "raw",
          "options": { "raw": { "language": "json" } },
          "raw": "{\n  \"IsToClose\": true,\n  \"Client\": {\n    \"NIF\": \"166737755\",\n    \"Name\": \"MagniFinance\",\n    \"Address\": \"Av. Sidónio Pais, n.º 2, 2.º Esq. A\",\n    \"City\": \"Lisboa\",\n    \"PostCode\": \"1050-215\",\n    \"CountryCode\": \"PT\",\n    \"Email\": \"info@magnifinance.com\"\n  },\n  \"Document\": {\n    \"Type\": \"T\",\n    \"Date\": \"{{today}}\",\n    \"DueDate\": \"{{dueDate}}\",\n    \"Description\": \"Consulting services\",\n    \"ExternalId\": \"order-{{$timestamp}}\",\n    \"Lines\": [\n      {\n        \"Code\": \"CONSULT-H\",\n        \"Description\": \"Consulting hour\",\n        \"UnitPrice\": 45.00,\n        \"Quantity\": 2,\n        \"Unit\": \"Hour\",\n        \"Type\": \"S\",\n        \"TaxValue\": 23,\n        \"ProductDiscount\": 0\n      }\n    ]\n  }\n}"
        },
        "description": "Creates and closes an invoice-receipt (type `T`). Change `Type` for other document types: https://developer.magnifinance.com/finance/getting-started/appendix/#document-types\n\nDocs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/create-document/"
      }
    },
    {
      "name": "Get Document",
      "request": {
        "method": "GET",
        "header": [
          { "key": "email", "value": "{{email}}" },
          { "key": "token", "value": "{{token}}" }
        ],
        "url": { "raw": "{{baseUrl}}/api/v1.1/document?documentId={{documentId}}", "host": ["{{baseUrl}}"], "path": ["api", "v1.1", "document"], "query": [{ "key": "documentId", "value": "{{documentId}}" }] },
        "description": "Number, PDF link, payment status and details of a document.\n\nDocs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/get-document/"
      }
    },
    {
      "name": "Get Document Property",
      "request": {
        "method": "GET",
        "header": [
          { "key": "email", "value": "{{email}}" },
          { "key": "token", "value": "{{token}}" }
        ],
        "url": { "raw": "{{baseUrl}}/api/v1.1/document/property?documentId={{documentId}}", "host": ["{{baseUrl}}"], "path": ["api", "v1.1", "document", "property"], "query": [{ "key": "documentId", "value": "{{documentId}}" }] },
        "description": "Docs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/get-document-property/"
      }
    },
    {
      "name": "Cancel Document",
      "request": {
        "method": "PATCH",
        "header": [
          { "key": "email", "value": "{{email}}" },
          { "key": "token", "value": "{{token}}" }
        ],
        "url": { "raw": "{{baseUrl}}/api/v1.1/document?documentId={{documentId}}", "host": ["{{baseUrl}}"], "path": ["api", "v1.1", "document"], "query": [{ "key": "documentId", "value": "{{documentId}}" }] },
        "description": "Cancels a closed document.\n\nDocs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/cancel-document/"
      }
    },
    {
      "name": "Create Waybill",
      "request": {
        "method": "POST",
        "header": [
          { "key": "email", "value": "{{email}}" },
          { "key": "token", "value": "{{token}}" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "url": { "raw": "{{baseUrl}}/api/v1.1/document/waybill", "host": ["{{baseUrl}}"], "path": ["api", "v1.1", "document", "waybill"] },
        "body": {
          "mode": "raw",
          "options": { "raw": { "language": "json" } },
          "raw": "{\n  \"IsToClose\": true,\n  \"Client\": {\n    \"NIF\": \"166737755\",\n    \"Name\": \"MagniFinance\",\n    \"Address\": \"Av. Sidónio Pais, n.º 2, 2.º Esq. A\",\n    \"City\": \"Lisboa\",\n    \"PostCode\": \"1050-215\",\n    \"CountryCode\": \"PT\"\n  },\n  \"Waybill\": {\n    \"Type\": \"W\",\n    \"Date\": \"{{today}}\",\n    \"Description\": \"Delivery of order 10045\",\n    \"Lines\": [\n      {\n        \"Code\": \"BOX-12\",\n        \"Description\": \"Box of 12 bottles\",\n        \"UnitPrice\": 18.50,\n        \"Quantity\": 4,\n        \"Unit\": \"Box\",\n        \"Type\": \"P\",\n        \"TaxValue\": 23,\n        \"ProductDiscount\": 0\n      }\n    ],\n    \"LoadDateTime\": \"{{now}}\",\n    \"LoadAddress\": {\n      \"AddressDetail\": \"Rua do Armazém, 10\",\n      \"City\": \"Lisboa\",\n      \"PostalCode\": \"1000-001\",\n      \"CountryCode\": \"PT\"\n    },\n    \"DestinationAddress\": {\n      \"AddressDetail\": \"Av. Sidónio Pais, n.º 2, 2.º Esq. A\",\n      \"City\": \"Lisboa\",\n      \"PostalCode\": \"1050-215\",\n      \"CountryCode\": \"PT\"\n    },\n    \"VehicleLicence\": \"AA-16-AA\"\n  }\n}"
        },
        "description": "Creates and closes a waybill (type `W`; `Y` for a delivery note).\n\nDocs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/create-waybill/"
      }
    },
    {
      "name": "Get a Client",
      "request": {
        "method": "GET",
        "header": [
          { "key": "email", "value": "{{email}}" },
          { "key": "token", "value": "{{token}}" }
        ],
        "url": { "raw": "{{baseUrl}}/api/v1.1/client?countryCode=PT&taxId=599138645", "host": ["{{baseUrl}}"], "path": ["api", "v1.1", "client"], "query": [{ "key": "countryCode", "value": "PT" }, { "key": "taxId", "value": "599138645" }] },
        "description": "Docs: https://developer.magnifinance.com/finance/invoicing-waybills/rest/get-client/"
      }
    }
  ]
}
