{
  "openapi": "3.1.0",
  "info": {
    "title": "ApexSFA — API інтеграцій",
    "version": "v1",
    "description": "Ендпоінти ApexSFA для інтеграторів: обмін з обліковою системою, файли заказів виробника, перевірка доступності. Документація: https://apexsfa.com/developers"
  },
  "servers": [
    {
      "url": "https://api.apexsfa.com",
      "description": "Прод"
    }
  ],
  "tags": [
    {
      "name": "Обмін з обліковою системою",
      "description": "Підписані запити облікової системи: прийом даних і забір документів."
    },
    {
      "name": "Файли заказів виробника",
      "description": "Файли заказів через API-токен."
    },
    {
      "name": "Службові",
      "description": "Перевірка доступності."
    }
  ],
  "paths": {
    "/api/v1/webhooks/1c/inbound/batch": {
      "post": {
        "tags": [
          "Обмін з обліковою системою"
        ],
        "summary": "Прийняти пакет конвертів (до 5000)",
        "operationId": "inbound_webhook_batch_api_v1_webhooks_1c_inbound_batch_post",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Tenant-Id"
            }
          },
          {
            "name": "X-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Signature"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Успіх",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/AccountingInboundBatchResponse"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Невірний підпис / тенант / інтеграцію вимкнено"
          },
          "422": {
            "description": "Невалідний конверт або бізнес-помилка"
          },
          "429": {
            "description": "Перевищено ліміт; Retry-After"
          }
        },
        "security": [
          {
            "TenantId": [],
            "ExchangeSignature": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json; charset=utf-8": {
              "schema": {
                "$ref": "#/components/schemas/AccountingInboundBatchEnvelope"
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/1c/inbound": {
      "post": {
        "tags": [
          "Обмін з обліковою системою"
        ],
        "summary": "Прийняти один конверт від облікової системи",
        "operationId": "inbound_webhook_api_v1_webhooks_1c_inbound_post",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Tenant-Id"
            }
          },
          {
            "name": "X-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Signature"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Успіх",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/AccountingInboundResponse"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Невірний підпис / тенант / інтеграцію вимкнено"
          },
          "422": {
            "description": "Невалідний конверт або бізнес-помилка"
          },
          "429": {
            "description": "Перевищено ліміт; Retry-After"
          }
        },
        "security": [
          {
            "TenantId": [],
            "ExchangeSignature": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json; charset=utf-8": {
              "schema": {
                "$ref": "#/components/schemas/AccountingInboundEnvelope"
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/1c/outbox/pull": {
      "post": {
        "tags": [
          "Обмін з обліковою системою"
        ],
        "summary": "Забрати пачку документів для облікової системи",
        "operationId": "outbox_pull_api_v1_webhooks_1c_outbox_pull_post",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Tenant-Id"
            }
          },
          {
            "name": "X-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Signature"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Успіх",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/OutboxPullResponse"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Невірний підпис / тенант / інтеграцію вимкнено"
          },
          "422": {
            "description": "Невалідний конверт або бізнес-помилка"
          },
          "429": {
            "description": "Перевищено ліміт; Retry-After"
          }
        },
        "security": [
          {
            "TenantId": [],
            "ExchangeSignature": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json; charset=utf-8": {
              "schema": {
                "$ref": "#/components/schemas/OutboxPullRequest"
              }
            }
          }
        }
      }
    },
    "/api/v1/webhooks/1c/outbox/ack": {
      "post": {
        "tags": [
          "Обмін з обліковою системою"
        ],
        "summary": "Підтвердити обробку забраних документів",
        "operationId": "outbox_ack_api_v1_webhooks_1c_outbox_ack_post",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Tenant-Id"
            }
          },
          {
            "name": "X-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "null"
                }
              ],
              "title": "X-Signature"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Успіх",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/OutboxAckResponse"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Невірний підпис / тенант / інтеграцію вимкнено"
          },
          "422": {
            "description": "Невалідний конверт або бізнес-помилка"
          },
          "429": {
            "description": "Перевищено ліміт; Retry-After"
          }
        },
        "security": [
          {
            "TenantId": [],
            "ExchangeSignature": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json; charset=utf-8": {
              "schema": {
                "$ref": "#/components/schemas/OutboxAckRequest"
              }
            }
          }
        }
      }
    },
    "/api/v1/exchange/order-files": {
      "post": {
        "tags": [
          "Файли заказів виробника"
        ],
        "summary": "Покласти файл заказів",
        "operationId": "upload_order_file_api_v1_exchange_order_files_post",
        "responses": {
          "201": {
            "description": "Файл прийнято"
          },
          "401": {
            "description": "Немає/невірний токен"
          },
          "403": {
            "description": "Немає scope exchange:orders"
          },
          "413": {
            "description": "Понад 20 МБ"
          },
          "422": {
            "description": "Порожній файл"
          },
          "429": {
            "description": "Ліміт токена"
          }
        },
        "security": [
          {
            "ApiToken": []
          }
        ],
        "parameters": [
          {
            "name": "filename",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "orders.xml"
            },
            "description": "Імʼя файлу, якщо тіло передається сирими байтами"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  }
                },
                "required": [
                  "file"
                ]
              }
            },
            "application/octet-stream": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            }
          }
        }
      },
      "get": {
        "tags": [
          "Файли заказів виробника"
        ],
        "summary": "Статуси своїх файлів",
        "operationId": "list_order_files_api_v1_exchange_order_files_get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "additionalProperties": true,
                  "type": "object",
                  "title": "Response List Order Files Api V1 Exchange Order Files Get"
                }
              }
            }
          }
        },
        "security": [
          {
            "ApiToken": []
          }
        ]
      }
    },
    "/api/v1/exchange/order-files/{batch_id}": {
      "get": {
        "tags": [
          "Файли заказів виробника"
        ],
        "summary": "Статус файлу",
        "operationId": "get_order_file_api_v1_exchange_order_files__batch_id__get",
        "parameters": [
          {
            "name": "batch_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid",
              "title": "Batch Id"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true,
                  "title": "Response Get Order File Api V1 Exchange Order Files  Batch Id  Get"
                }
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        },
        "security": [
          {
            "ApiToken": []
          }
        ]
      }
    },
    "/api/v1/exchange/order-files/{batch_id}/response": {
      "get": {
        "tags": [
          "Файли заказів виробника"
        ],
        "summary": "Забрати файл-відповідь",
        "operationId": "download_order_file_response_api_v1_exchange_order_files__batch_id__response_get",
        "parameters": [
          {
            "name": "batch_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid",
              "title": "Batch Id"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Файл-відповідь (тип залежить від шаблону)"
          },
          "404": {
            "description": "Файл не знайдено або належить іншому токену"
          },
          "409": {
            "description": "Ще не всі заявки файлу оброблено"
          }
        },
        "security": [
          {
            "ApiToken": []
          }
        ]
      }
    },
    "/api/v1/health": {
      "get": {
        "tags": [
          "Службові"
        ],
        "summary": "Перевірка живості",
        "operationId": "health_api_v1_health_get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "additionalProperties": {
                    "type": "string"
                  },
                  "type": "object",
                  "title": "Response Health Api V1 Health Get"
                }
              }
            }
          }
        },
        "security": []
      }
    }
  },
  "components": {
    "schemas": {
      "AccountingInboundBatchEnvelope": {
        "description": "Тіло POST /webhooks/1c/inbound/batch — масив envelope-ів.\n\nТранзакційність: за замовчуванням `partial=True` — кожен item у власній\nтранзакції, помилка одного НЕ скасовує інших. Returns per-row results.\nЯкщо `partial=False`, весь батч іде у одній транзакції (атомарно).",
        "properties": {
          "items": {
            "items": {
              "$ref": "#/components/schemas/AccountingInboundEnvelope"
            },
            "maxItems": 5000,
            "minItems": 1,
            "title": "Items",
            "type": "array"
          },
          "partial": {
            "default": true,
            "title": "Partial",
            "type": "boolean"
          }
        },
        "required": [
          "items"
        ],
        "title": "AccountingInboundBatchEnvelope",
        "type": "object"
      },
      "AccountingInboundBatchResponse": {
        "properties": {
          "accepted_count": {
            "title": "Accepted Count",
            "type": "integer"
          },
          "failed_count": {
            "title": "Failed Count",
            "type": "integer"
          },
          "results": {
            "items": {
              "$ref": "#/components/schemas/AccountingInboundBatchRowResult"
            },
            "title": "Results",
            "type": "array"
          }
        },
        "required": [
          "accepted_count",
          "failed_count",
          "results"
        ],
        "title": "AccountingInboundBatchResponse",
        "type": "object"
      },
      "AccountingInboundBatchRowResult": {
        "description": "Результат для одного item у батчі.",
        "properties": {
          "index": {
            "title": "Index",
            "type": "integer"
          },
          "entity_type": {
            "title": "Entity Type",
            "type": "string"
          },
          "external_id": {
            "title": "External Id",
            "type": "string"
          },
          "accepted": {
            "title": "Accepted",
            "type": "boolean"
          },
          "entity_id": {
            "anyOf": [
              {
                "format": "uuid",
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Entity Id"
          },
          "operation_performed": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Operation Performed"
          },
          "error": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Error"
          },
          "details": {
            "anyOf": [
              {
                "additionalProperties": true,
                "type": "object"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Details"
          }
        },
        "required": [
          "index",
          "entity_type",
          "external_id",
          "accepted"
        ],
        "title": "AccountingInboundBatchRowResult",
        "type": "object"
      },
      "AccountingInboundEnvelope": {
        "description": "Тіло POST /webhooks/1c/inbound.",
        "properties": {
          "entity_type": {
            "pattern": "^(client|product|product_category|price_list_item|counterparty|manufacturer|sale|agent|driver|vehicle|transport_list|trip|client_assignment|equipment_type|equipment_unit|branch|warehouse|supplier|supplier_point|price_type|stock_document|wms_stock|excise_marks|product_stock|debt_document|debt_reset|logistics_manufacturer|logistics_counterparty|logistics_point|logistics_order|logistics_warehouse|logistics_own_manufacturer|logistics_product|logistics_product_category|logistics_product_code|logistics_stock|product_external_code|manufacturer_price_type|manufacturer_payment_term|goods_document|goods_period|product_stock_snapshot|cash_document|cash_balance|cash_period|order_state|order_state_period)$",
            "title": "Entity Type",
            "type": "string"
          },
          "operation": {
            "pattern": "^(create|update|upsert|delete)$",
            "title": "Operation",
            "type": "string"
          },
          "external_id": {
            "maxLength": 50,
            "minLength": 1,
            "title": "External Id",
            "type": "string"
          },
          "payload": {
            "additionalProperties": true,
            "title": "Payload",
            "type": "object"
          }
        },
        "required": [
          "entity_type",
          "operation",
          "external_id"
        ],
        "title": "AccountingInboundEnvelope",
        "type": "object"
      },
      "AccountingInboundResponse": {
        "properties": {
          "accepted": {
            "title": "Accepted",
            "type": "boolean"
          },
          "entity_id": {
            "anyOf": [
              {
                "format": "uuid",
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Entity Id"
          },
          "operation_performed": {
            "title": "Operation Performed",
            "type": "string"
          },
          "details": {
            "anyOf": [
              {
                "additionalProperties": true,
                "type": "object"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Details"
          }
        },
        "required": [
          "accepted",
          "operation_performed"
        ],
        "title": "AccountingInboundResponse",
        "type": "object"
      },
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "OutboxAckRequest": {
        "properties": {
          "tenant_id": {
            "anyOf": [
              {
                "format": "uuid",
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Tenant Id"
          },
          "results": {
            "items": {
              "$ref": "#/components/schemas/OutboxAckResult"
            },
            "maxItems": 200,
            "minItems": 1,
            "title": "Results",
            "type": "array"
          }
        },
        "required": [
          "results"
        ],
        "title": "OutboxAckRequest",
        "type": "object"
      },
      "OutboxAckResponse": {
        "properties": {
          "acked": {
            "title": "Acked",
            "type": "integer"
          },
          "synced": {
            "title": "Synced",
            "type": "integer"
          },
          "retrying": {
            "title": "Retrying",
            "type": "integer"
          },
          "failed": {
            "title": "Failed",
            "type": "integer"
          },
          "not_found": {
            "title": "Not Found",
            "type": "integer"
          }
        },
        "required": [
          "acked",
          "synced",
          "retrying",
          "failed",
          "not_found"
        ],
        "title": "OutboxAckResponse",
        "type": "object"
      },
      "OutboxAckResult": {
        "properties": {
          "queue_id": {
            "format": "uuid",
            "title": "Queue Id",
            "type": "string"
          },
          "status": {
            "pattern": "^(ok|error)$",
            "title": "Status",
            "type": "string"
          },
          "external_id": {
            "anyOf": [
              {
                "maxLength": 200,
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "External Id"
          },
          "error_message": {
            "anyOf": [
              {
                "maxLength": 2000,
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Error Message"
          },
          "permanent": {
            "default": false,
            "title": "Permanent",
            "type": "boolean"
          }
        },
        "required": [
          "queue_id",
          "status"
        ],
        "title": "OutboxAckResult",
        "type": "object"
      },
      "OutboxPullItem": {
        "properties": {
          "queue_id": {
            "format": "uuid",
            "title": "Queue Id",
            "type": "string"
          },
          "entity_type": {
            "title": "Entity Type",
            "type": "string"
          },
          "entity_id": {
            "format": "uuid",
            "title": "Entity Id",
            "type": "string"
          },
          "operation": {
            "title": "Operation",
            "type": "string"
          },
          "attempt": {
            "title": "Attempt",
            "type": "integer"
          },
          "payload": {
            "additionalProperties": true,
            "title": "Payload",
            "type": "object"
          }
        },
        "required": [
          "queue_id",
          "entity_type",
          "entity_id",
          "operation",
          "attempt"
        ],
        "title": "OutboxPullItem",
        "type": "object"
      },
      "OutboxPullRequest": {
        "description": "Тіло POST /webhooks/1c/outbox/pull (tenant_id резолвиться роутером).",
        "properties": {
          "tenant_id": {
            "anyOf": [
              {
                "format": "uuid",
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Tenant Id"
          },
          "batch_size": {
            "default": 50,
            "maximum": 100,
            "minimum": 1,
            "title": "Batch Size",
            "type": "integer"
          },
          "entity_types": {
            "anyOf": [
              {
                "items": {
                  "type": "string"
                },
                "maxItems": 20,
                "type": "array"
              },
              {
                "type": "null"
              }
            ],
            "default": null,
            "title": "Entity Types"
          }
        },
        "title": "OutboxPullRequest",
        "type": "object"
      },
      "OutboxPullResponse": {
        "properties": {
          "items": {
            "items": {
              "$ref": "#/components/schemas/OutboxPullItem"
            },
            "title": "Items",
            "type": "array"
          },
          "has_more": {
            "title": "Has More",
            "type": "boolean"
          }
        },
        "required": [
          "items",
          "has_more"
        ],
        "title": "OutboxPullResponse",
        "type": "object"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "integer"
                }
              ]
            },
            "type": "array",
            "title": "Location"
          },
          "msg": {
            "type": "string",
            "title": "Message"
          },
          "type": {
            "type": "string",
            "title": "Error Type"
          }
        },
        "type": "object",
        "required": [
          "loc",
          "msg",
          "type"
        ],
        "title": "ValidationError"
      }
    },
    "securitySchemes": {
      "ApiToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "API-токен asfa_…"
      },
      "TenantId": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Tenant-Id",
        "description": "UUID тенанта (точки синхронізації)"
      },
      "ExchangeSignature": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Signature",
        "description": "t=<unix>,v2=sha256(t + '.' + body + '.' + secret)"
      }
    }
  }
}
