{
  "openapi": "3.1.1",
  "info": {
    "title": "Factly API",
    "version": "1.0.0",
    "summary": "API para integrar sistemas externos con la facturacion electronica DGII por medio de Factly.",
    "description": "Factly API permite crear documentos desde JSON, consultar estados, descargar XML/PDF, revisar uso y probar webhooks. La emision DGII real es asincrona y reutiliza la tuberia central de Factly."
  },
  "servers": [
    {
      "url": "https://app.factly.do/api/public",
      "description": "Produccion"
    },
    {
      "url": "http://app_factly/api/public",
      "description": "Local o QA interno"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    },
    {
      "ApiKeyAuth": []
    }
  ],
  "tags": [
    {
      "name": "Catalogs",
      "description": "Catalogos publicos para integradores."
    },
    {
      "name": "Documents",
      "description": "Creacion, consulta, retry y descargas de documentos."
    },
    {
      "name": "Usage",
      "description": "Consumo y cuotas del tenant."
    },
    {
      "name": "Webhooks",
      "description": "Pruebas de callbacks configurados."
    }
  ],
  "paths": {
    "/v1/catalogs/document-types": {
      "get": {
        "tags": [
          "Catalogs"
        ],
        "summary": "Listar tipos de documento",
        "operationId": "listDocumentTypes",
        "responses": {
          "200": {
            "description": "Catalogo de tipos de documento.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentTypesResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/v1/{environment}/usage": {
      "get": {
        "tags": [
          "Usage"
        ],
        "summary": "Consultar uso y cuota",
        "operationId": "getUsage",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          }
        ],
        "responses": {
          "200": {
            "description": "Uso actual del ciclo.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UsageResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/v1/{environment}/documents": {
      "post": {
        "tags": [
          "Documents"
        ],
        "summary": "Crear documento JSON",
        "operationId": "createDocumentJson",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DocumentCreateRequest"
              },
              "examples": {
                "consumerSandbox": {
                  "$ref": "#/components/examples/ConsumerSandboxRequest"
                },
                "creditFiscalReal": {
                  "$ref": "#/components/examples/CreditFiscalRealRequest"
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Documento recibido, validado o encolado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "402": {
            "$ref": "#/components/responses/QuotaExceeded"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "422": {
            "$ref": "#/components/responses/ValidationError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/v1/{environment}/documents/xml": {
      "post": {
        "tags": [
          "Documents"
        ],
        "summary": "Crear documento XML directo",
        "description": "Disponible actualmente solo en sandbox interno y cuando el formato XML esta habilitado para el tenant. Para produccion usa el endpoint JSON.",
        "operationId": "createDocumentXml",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/xml": {
              "schema": {
                "type": "string"
              }
            },
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/XmlCreateRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "XML almacenado como request aprobado en sandbox interno.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/v1/{environment}/documents/{id}": {
      "get": {
        "tags": [
          "Documents"
        ],
        "summary": "Consultar documento",
        "operationId": "getDocument",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/DocumentId"
          }
        ],
        "responses": {
          "200": {
            "description": "Documento API con snapshot y datos DGII si existen.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/v1/{environment}/documents/{id}/status": {
      "get": {
        "tags": [
          "Documents"
        ],
        "summary": "Consultar estado del documento",
        "operationId": "getDocumentStatus",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/DocumentId"
          }
        ],
        "responses": {
          "200": {
            "description": "Estado resumido.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentStatusResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/v1/{environment}/documents/{id}/xml": {
      "get": {
        "tags": [
          "Documents"
        ],
        "summary": "Descargar XML",
        "operationId": "downloadDocumentXml",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/DocumentId"
          }
        ],
        "responses": {
          "200": {
            "description": "XML del documento o preview cuando aplique.",
            "content": {
              "application/xml": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/v1/{environment}/documents/{id}/pdf": {
      "get": {
        "tags": [
          "Documents"
        ],
        "summary": "Descargar PDF",
        "operationId": "downloadDocumentPdf",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/DocumentId"
          }
        ],
        "responses": {
          "200": {
            "description": "PDF preliminar o final.",
            "content": {
              "application/pdf": {
                "schema": {
                  "type": "string",
                  "contentEncoding": "base64"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/v1/{environment}/documents/{id}/retry": {
      "post": {
        "tags": [
          "Documents"
        ],
        "summary": "Reintentar documento",
        "operationId": "retryDocument",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          },
          {
            "$ref": "#/components/parameters/DocumentId"
          }
        ],
        "responses": {
          "202": {
            "description": "Documento reencolado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RetryResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/v1/{environment}/webhooks/test": {
      "post": {
        "tags": [
          "Webhooks"
        ],
        "summary": "Probar webhooks",
        "operationId": "testWebhook",
        "parameters": [
          {
            "$ref": "#/components/parameters/Environment"
          }
        ],
        "responses": {
          "200": {
            "description": "Evento de prueba enviado a los webhooks configurados.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookTestResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Envia `Authorization: Bearer TU_API_KEY`."
      },
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Alternativa a Bearer auth."
      }
    },
    "parameters": {
      "Environment": {
        "name": "environment",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "enum": [
            "sandbox",
            "production"
          ]
        }
      },
      "DocumentId": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "integer",
          "minimum": 1
        }
      },
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 191
        },
        "description": "Llave unica por intento logico de creacion."
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Solicitud invalida.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Falta API key o la API key es invalida.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Credencial, ambiente, tenant, modulo o suscripcion no permitido.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "QuotaExceeded": {
        "description": "Cuota agotada.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "Conflict": {
        "description": "Conflicto de estado, duplicado o recurso no disponible.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "ValidationError": {
        "description": "Payload invalido.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "NotFound": {
        "description": "Documento no encontrado.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "RateLimited": {
        "description": "Demasiadas solicitudes.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      }
    },
    "schemas": {
      "DocumentTypesResponse": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "code",
                "label"
              ],
              "properties": {
                "code": {
                  "type": "string",
                  "examples": [
                    "31"
                  ]
                },
                "label": {
                  "type": "string",
                  "examples": [
                    "Factura de Credito Fiscal Electronica"
                  ]
                }
              }
            }
          }
        }
      },
      "UsageResponse": {
        "type": "object",
        "properties": {
          "environment": {
            "type": "string",
            "enum": [
              "sandbox",
              "production"
            ]
          },
          "cycleStart": {
            "type": "string",
            "format": "date"
          },
          "cycleEnd": {
            "type": "string",
            "format": "date"
          },
          "documentsIncluded": {
            "type": "integer"
          },
          "documentsUsed": {
            "type": "integer"
          },
          "documentsOverage": {
            "type": "integer"
          },
          "amountEstimated": {
            "type": "number"
          },
          "billingStrategy": {
            "type": "string",
            "examples": [
              "monthly_quota"
            ]
          },
          "overagePolicy": {
            "type": "string",
            "examples": [
              "block"
            ]
          }
        }
      },
      "DocumentCreateRequest": {
        "type": "object",
        "required": [
          "externalId",
          "documentType",
          "lines"
        ],
        "properties": {
          "externalId": {
            "type": "string",
            "maxLength": 191
          },
          "documentType": {
            "type": "string",
            "examples": [
              "31",
              "32"
            ]
          },
          "encf": {
            "type": "string",
            "description": "Requerido en production y en sandbox QA DGII."
          },
          "issueDate": {
            "type": "string",
            "format": "date"
          },
          "fechaVencimientoSecuencia": {
            "type": "string",
            "format": "date"
          },
          "billingType": {
            "type": "string",
            "enum": [
              "CONTADO",
              "CREDITO"
            ]
          },
          "creditDays": {
            "type": "integer",
            "minimum": 0
          },
          "currency": {
            "type": "string",
            "default": "DOP"
          },
          "language": {
            "type": "string",
            "default": "ES"
          },
          "notes": {
            "type": "string"
          },
          "customerId": {
            "type": "integer"
          },
          "customerName": {
            "type": "string"
          },
          "customerDocument": {
            "type": "string"
          },
          "customerDocumentType": {
            "type": "string",
            "examples": [
              "rnc"
            ]
          },
          "customerEmail": {
            "type": "string",
            "format": "email"
          },
          "referenceEncf": {
            "type": "string"
          },
          "referenceIssueDate": {
            "type": "string",
            "format": "date"
          },
          "referenceModificationCode": {
            "type": "string"
          },
          "lines": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/components/schemas/DocumentLine"
            }
          }
        }
      },
      "DocumentLine": {
        "type": "object",
        "required": [
          "description",
          "qty",
          "unitPrice"
        ],
        "properties": {
          "itemId": {
            "type": "integer"
          },
          "description": {
            "type": "string"
          },
          "qty": {
            "type": "number",
            "exclusiveMinimum": 0
          },
          "unitPrice": {
            "type": "number",
            "minimum": 0
          },
          "taxRate": {
            "type": "number",
            "default": 18
          },
          "discountAmount": {
            "type": "number",
            "minimum": 0
          },
          "unidadMedida": {
            "type": "string",
            "default": "43"
          }
        }
      },
      "XmlCreateRequest": {
        "type": "object",
        "required": [
          "externalId",
          "xml"
        ],
        "properties": {
          "externalId": {
            "type": "string"
          },
          "xml": {
            "type": "string"
          }
        }
      },
      "DocumentResponse": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "externalId": {
            "type": "string"
          },
          "environment": {
            "type": "string"
          },
          "requestFormat": {
            "type": "string",
            "enum": [
              "json",
              "xml"
            ]
          },
          "status": {
            "$ref": "#/components/schemas/RequestStatus"
          },
          "invoiceId": {
            "type": "integer"
          },
          "receivedAt": {
            "type": "string"
          },
          "completedAt": {
            "type": "string"
          },
          "links": {
            "$ref": "#/components/schemas/DocumentLinks"
          },
          "document": {
            "$ref": "#/components/schemas/DocumentSummary"
          },
          "dgii": {
            "$ref": "#/components/schemas/DgiiDetails"
          },
          "validationErrors": {
            "oneOf": [
              {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              {
                "type": "object"
              }
            ]
          },
          "normalizedPayload": {
            "type": "object",
            "additionalProperties": true
          }
        }
      },
      "DocumentStatusResponse": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "externalId": {
            "type": "string"
          },
          "environment": {
            "type": "string"
          },
          "status": {
            "$ref": "#/components/schemas/RequestStatus"
          },
          "completedAt": {
            "type": "string"
          },
          "invoiceId": {
            "type": "integer"
          },
          "document": {
            "$ref": "#/components/schemas/DocumentSummary"
          },
          "dgii": {
            "$ref": "#/components/schemas/DgiiDetails"
          },
          "validationErrors": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      "DocumentLinks": {
        "type": "object",
        "properties": {
          "self": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "pdf": {
            "type": "string"
          },
          "xml": {
            "type": "string"
          }
        }
      },
      "DocumentSummary": {
        "type": "object",
        "properties": {
          "invoiceId": {
            "type": "integer"
          },
          "internalNumber": {
            "type": "string"
          },
          "encf": {
            "type": "string"
          },
          "dgiiStatus": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "examples": [
              "DRAFT",
              "QUEUED",
              "SENT",
              "APPROVED",
              "REJECTED"
            ]
          },
          "issueDate": {
            "type": "string",
            "format": "date"
          },
          "total": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          },
          "pdfFinalAvailable": {
            "type": "boolean"
          },
          "xmlPreviewAvailable": {
            "type": "boolean"
          }
        }
      },
      "DgiiDetails": {
        "type": "object",
        "properties": {
          "transmissionStatus": {
            "type": "string"
          },
          "trackId": {
            "type": "string"
          },
          "httpStatus": {
            "type": "integer"
          },
          "environment": {
            "type": "string"
          },
          "requestType": {
            "type": "string"
          },
          "stateText": {
            "type": "string"
          },
          "source": {
            "type": "string"
          },
          "isTrackingSimulated": {
            "type": "boolean"
          },
          "errorCode": {
            "type": "string"
          },
          "errorMessage": {
            "type": "string"
          },
          "messages": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/DgiiMessage"
            }
          }
        }
      },
      "DgiiMessage": {
        "type": "object",
        "properties": {
          "codigo": {
            "type": "string"
          },
          "valor": {
            "type": "string"
          }
        }
      },
      "RequestStatus": {
        "type": "string",
        "enum": [
          "received",
          "validated",
          "queued",
          "sent",
          "approved",
          "rejected",
          "failed"
        ]
      },
      "RetryResponse": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean"
          },
          "requestId": {
            "type": "integer"
          },
          "jobId": {
            "type": "integer"
          },
          "status": {
            "type": "string",
            "examples": [
              "queued"
            ]
          }
        }
      },
      "WebhookTestResponse": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean"
          },
          "eventType": {
            "type": "string",
            "examples": [
              "document.received"
            ]
          },
          "environment": {
            "type": "string"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": [
          "code",
          "message",
          "details"
        ],
        "properties": {
          "code": {
            "type": "string",
            "examples": [
              "API_DOCUMENT_VALIDATION_ERROR"
            ]
          },
          "message": {
            "type": "string"
          },
          "details": {
            "type": "object",
            "additionalProperties": true
          }
        }
      }
    },
    "examples": {
      "ConsumerSandboxRequest": {
        "summary": "Factura de consumo para sandbox interno",
        "value": {
          "externalId": "pedido-1001",
          "documentType": "32",
          "issueDate": "2026-05-08",
          "billingType": "CONTADO",
          "currency": "DOP",
          "customerName": "Consumidor final",
          "customerDocument": "",
          "customerDocumentType": "",
          "lines": [
            {
              "description": "Producto de prueba API",
              "qty": 1,
              "unitPrice": 1000,
              "taxRate": 18,
              "discountAmount": 0,
              "unidadMedida": "43"
            }
          ]
        }
      },
      "CreditFiscalRealRequest": {
        "summary": "Factura fiscal para QA DGII o produccion",
        "value": {
          "externalId": "pedido-1001",
          "documentType": "31",
          "encf": "E310000000018",
          "issueDate": "2026-05-08",
          "fechaVencimientoSecuencia": "2026-12-31",
          "billingType": "CREDITO",
          "creditDays": 30,
          "currency": "DOP",
          "customerName": "Cliente Ejemplo SRL",
          "customerDocument": "101123456",
          "customerDocumentType": "rnc",
          "customerEmail": "facturas@cliente-ejemplo.com",
          "lines": [
            {
              "description": "Servicio de integracion",
              "qty": 1,
              "unitPrice": 3500,
              "taxRate": 18,
              "discountAmount": 0,
              "unidadMedida": "43"
            }
          ]
        }
      }
    }
  }
}
