{
  "openapi": "3.0.3",
  "info": {
    "title": "SUST | API de integraciones",
    "version": "1.0.0",
    "description": "Contrato de las cuatro operaciones externas de SUST API v1. Cada credencial pertenece a una aplicación; los permisos READ, CREATE y EVENTS se administran en SUST. Las claves se envían desde el servidor del cliente. Los ejemplos son ficticios; reemplácelos por identificadores propios. Las fechas de negocio se expresan en hora local de Lima. Swagger permite únicamente consultas GET en producción. Todos los errores utilizan el esquema Error: code, status, message, field, action, retryable y correlationId. El identificador también se devuelve en la cabecera X-Correlation-ID. No se devuelven trazas ni secretos."
  },
  "servers": [
    {
      "url": "/",
      "description": "Mismo origen HTTPS"
    }
  ],
  "paths": {
    "/api/v1/seguimientos": {
      "post": {
        "operationId": "crearSeguimiento",
        "tags": [
          "Seguimientos"
        ],
        "summary": "Crear seguimiento",
        "description": "Requiere CREATE. El flujo debe estar activo, pertenecer a la aplicación y tener estado inicial.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "x-sust-required-scope": "CREATE",
        "responses": {
          "201": {
            "description": "Operación correcta",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SeguimientoCreado"
                }
              }
            },
            "headers": {
              "Idempotency-Replayed": {
                "description": "true si se recuperó el resultado original",
                "schema": {
                  "type": "string",
                  "enum": [
                    "true",
                    "false"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Solicitud o clave de idempotencia inválida",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Credencial ausente, inválida, revocada o aplicación/organización inactiva",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente o aplicación no autorizada",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso inexistente o perteneciente a otra aplicación",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Conflicto de estado, integridad o clave reutilizada con otro contenido",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Solicitud demasiado grande en el gateway",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Se requiere application/json en el gateway",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Fallo interno",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Respuesta no válida del servicio",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "504": {
            "description": "Tiempo de espera agotado; verifique o reintente con la misma clave",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Reutilice exactamente la misma clave y el mismo contenido si no recibió confirmación. Alcance: aplicación, incluso entre credenciales. Una clave no se puede compartir entre operaciones diferentes. Sin cabecera no se evita el duplicado. La retención aún no tiene SLA.",
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[A-Za-z0-9._:-]+$"
            },
            "example": "550e8400-e29b-41d4-a716-446655440000"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CrearSeguimiento"
              },
              "example": {
                "idApplication": "11111111-1111-4111-8111-111111111111",
                "idFlow": "22222222-2222-4222-8222-222222222222",
                "externalReference": "REF-DEMO-001",
                "title": "Solicitud de prueba",
                "visibility": "PUBLIC"
              }
            }
          }
        }
      }
    },
    "/api/v1/seguimientos/{code}": {
      "get": {
        "operationId": "consultarSeguimiento",
        "tags": [
          "Seguimientos"
        ],
        "summary": "Consultar seguimiento por código",
        "description": "Requiere READ. Consulta únicamente registros de la aplicación propietaria, incluido su historial.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "x-sust-required-scope": "READ",
        "responses": {
          "200": {
            "description": "Operación correcta",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SeguimientoConsulta"
                }
              }
            }
          },
          "400": {
            "description": "Solicitud o clave de idempotencia inválida",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Credencial ausente, inválida, revocada o aplicación/organización inactiva",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente o aplicación no autorizada",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso inexistente o perteneciente a otra aplicación",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Fallo interno",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Respuesta no válida del servicio",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "504": {
            "description": "Tiempo de espera agotado; verifique o reintente con la misma clave",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "code",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "example": "SUST-2026-DEMO00000000"
          }
        ]
      }
    },
    "/api/v1/seguimientos/{id}/eventos": {
      "post": {
        "operationId": "agregarEvento",
        "tags": [
          "Seguimientos"
        ],
        "summary": "Registrar cambio de estado",
        "description": "Requiere EVENTS. id es el UUID interno devuelto al crear, no el código público. La transición debe estar permitida por el flujo vigente.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "x-sust-required-scope": "EVENTS",
        "responses": {
          "200": {
            "description": "Operación correcta",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EventoResultado"
                }
              }
            },
            "headers": {
              "Idempotency-Replayed": {
                "description": "true si se recuperó el resultado original",
                "schema": {
                  "type": "string",
                  "enum": [
                    "true",
                    "false"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Solicitud o clave de idempotencia inválida",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Credencial ausente, inválida, revocada o aplicación/organización inactiva",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente o aplicación no autorizada",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso inexistente o perteneciente a otra aplicación",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Conflicto de estado, integridad o clave reutilizada con otro contenido",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Solicitud demasiado grande en el gateway",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Se requiere application/json en el gateway",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Fallo interno",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Respuesta no válida del servicio",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "504": {
            "description": "Tiempo de espera agotado; verifique o reintente con la misma clave",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            },
            "example": "33333333-3333-4333-8333-333333333333"
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Reutilice exactamente la misma clave y el mismo contenido si no recibió confirmación. Alcance: aplicación, incluso entre credenciales. Una clave no se puede compartir entre operaciones diferentes. Sin cabecera no se evita el duplicado. La retención aún no tiene SLA.",
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[A-Za-z0-9._:-]+$"
            },
            "example": "550e8400-e29b-41d4-a716-446655440000"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CrearEvento"
              },
              "example": {
                "idToState": "22222222-2222-4222-8222-222222222222",
                "publicDescription": "Solicitud en revisión",
                "isPublic": true
              }
            }
          }
        }
      }
    },
    "/api/v1/configuracion": {
      "get": {
        "operationId": "consultarConfiguracion",
        "tags": [
          "Integraciones"
        ],
        "summary": "Obtener configuración de la aplicación",
        "description": "Requiere READ. No devuelve credenciales ni borradores; active indica si permite crear nuevos seguimientos. Conserve idFlow de cada seguimiento para actualizar versiones anteriores.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "x-sust-required-scope": "READ",
        "responses": {
          "200": {
            "description": "Configuración exclusiva de la aplicación; versiones publicadas, incluso históricas",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConfiguracionIntegracion"
                }
              }
            }
          },
          "400": {
            "description": "Solicitud o clave de idempotencia inválida",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Credencial ausente, inválida, revocada o aplicación/organización inactiva",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente o aplicación no autorizada",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso inexistente o perteneciente a otra aplicación",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Fallo interno",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Respuesta no válida del servicio",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "504": {
            "description": "Tiempo de espera agotado; verifique o reintente con la misma clave",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Credencial por aplicación, emitida en Integraciones. No compartir ni guardar en frontend público."
      }
    },
    "schemas": {
      "CrearSeguimiento": {
        "type": "object",
        "properties": {
          "idApplication": {
            "type": "string",
            "format": "uuid"
          },
          "idFlow": {
            "type": "string",
            "format": "uuid"
          },
          "externalReference": {
            "type": "string",
            "minLength": 1,
            "maxLength": 80
          },
          "title": {
            "type": "string",
            "minLength": 5,
            "maxLength": 160
          },
          "publicDescription": {
            "type": "string",
            "maxLength": 700,
            "nullable": true
          },
          "internalNote": {
            "type": "string",
            "maxLength": 700,
            "nullable": true
          },
          "visibility": {
            "type": "string",
            "enum": [
              "PUBLIC",
              "RESTRICTED",
              "INTERNAL"
            ]
          }
        },
        "required": [
          "idApplication",
          "idFlow",
          "externalReference",
          "title",
          "visibility"
        ]
      },
      "CrearEvento": {
        "type": "object",
        "properties": {
          "idToState": {
            "type": "string",
            "format": "uuid"
          },
          "publicDescription": {
            "type": "string",
            "minLength": 5,
            "maxLength": 700
          },
          "internalNote": {
            "type": "string",
            "maxLength": 700,
            "nullable": true
          },
          "isPublic": {
            "type": "boolean"
          }
        },
        "required": [
          "idToState",
          "publicDescription",
          "isPublic"
        ]
      },
      "Evento": {
        "type": "object",
        "properties": {
          "idTrackingEvent": {
            "type": "string",
            "format": "uuid"
          },
          "fromState": {
            "type": "string",
            "nullable": true
          },
          "toState": {
            "type": "string"
          },
          "publicDescription": {
            "type": "string"
          },
          "internalNote": {
            "type": "string",
            "nullable": true
          },
          "isPublic": {
            "type": "boolean"
          },
          "actor": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "description": "Fecha local de America/Lima, sin sufijo de zona horaria.",
            "example": "2026-11-28T10:30:00"
          }
        },
        "required": [
          "idTrackingEvent",
          "fromState",
          "toState",
          "publicDescription",
          "internalNote",
          "isPublic",
          "actor",
          "createdAt"
        ]
      },
      "EstadoPermitido": {
        "type": "object",
        "properties": {
          "idFlowState": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          }
        },
        "required": [
          "idFlowState",
          "name"
        ]
      },
      "SeguimientoCreado": {
        "type": "object",
        "properties": {
          "idTracking": {
            "type": "string",
            "format": "uuid"
          },
          "code": {
            "type": "string"
          },
          "externalReference": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "publicDescription": {
            "type": "string",
            "nullable": true
          },
          "internalNote": {
            "type": "string",
            "nullable": true
          },
          "visibility": {
            "type": "string",
            "enum": [
              "PUBLIC",
              "RESTRICTED",
              "INTERNAL"
            ]
          },
          "organizationName": {
            "type": "string"
          },
          "applicationName": {
            "type": "string"
          },
          "flowName": {
            "type": "string"
          },
          "stateName": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "description": "Fecha local de America/Lima, sin sufijo de zona horaria.",
            "example": "2026-11-28T10:30:00"
          },
          "updatedAt": {
            "type": "string",
            "description": "Fecha local de America/Lima, sin sufijo de zona horaria.",
            "example": "2026-11-28T10:30:00"
          },
          "events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Evento"
            }
          },
          "allowedTransitions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/EstadoPermitido"
            }
          }
        },
        "required": [
          "idTracking",
          "code",
          "externalReference",
          "title",
          "publicDescription",
          "internalNote",
          "visibility",
          "organizationName",
          "applicationName",
          "flowName",
          "stateName",
          "createdAt",
          "updatedAt",
          "events",
          "allowedTransitions"
        ]
      },
      "EventoResultado": {
        "type": "object",
        "properties": {
          "idTracking": {
            "type": "string",
            "format": "uuid"
          },
          "code": {
            "type": "string"
          },
          "stateName": {
            "type": "string"
          },
          "updatedAt": {
            "type": "string",
            "description": "Fecha local de America/Lima, sin sufijo de zona horaria.",
            "example": "2026-11-28T10:30:00"
          }
        },
        "required": [
          "idTracking",
          "code",
          "stateName",
          "updatedAt"
        ]
      },
      "Historial": {
        "type": "object",
        "properties": {
          "stateName": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "description": "Fecha local de America/Lima, sin sufijo de zona horaria.",
            "example": "2026-11-28T10:30:00"
          }
        },
        "required": [
          "stateName",
          "description",
          "createdAt"
        ]
      },
      "SeguimientoConsulta": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "state": {
            "type": "string"
          },
          "history": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Historial"
            }
          }
        },
        "required": [
          "code",
          "title",
          "state",
          "history"
        ]
      },
      "Error": {
        "type": "object",
        "description": "Respuesta uniforme de error. Nunca incluye contraseñas, credenciales, tokens ni trazas internas.",
        "properties": {
          "code": {
            "type": "string",
            "description": "Código estable para que el cliente pueda clasificar el error.",
            "example": "VALIDATION_ERROR"
          },
          "status": {
            "type": "integer",
            "minimum": 400,
            "maximum": 599,
            "description": "Código HTTP devuelto."
          },
          "message": {
            "type": "string",
            "description": "Explicación segura y legible del problema."
          },
          "field": {
            "type": "string",
            "nullable": true,
            "description": "Campo que debe revisarse, cuando aplica.",
            "example": "externalReference"
          },
          "action": {
            "type": "string",
            "description": "Acción recomendada para continuar."
          },
          "retryable": {
            "type": "boolean",
            "description": "Indica si tiene sentido volver a intentar después de atender la causa."
          },
          "correlationId": {
            "type": "string",
            "format": "uuid",
            "description": "Identificador para localizar el evento en los registros de SUST."
          }
        },
        "required": [
          "code",
          "status",
          "message",
          "field",
          "action",
          "retryable",
          "correlationId"
        ],
        "example": {
          "code": "VALIDATION_ERROR",
          "status": 400,
          "message": "Revisa el campo «title».",
          "field": "title",
          "action": "Corrige el campo indicado y vuelve a enviar la solicitud.",
          "retryable": false,
          "correlationId": "2f1eaa70-2a9b-4a3b-9f5b-1aef47d67b11"
        }
      },
      "ConfiguracionEstado": {
        "type": "object",
        "properties": {
          "idFlow": {
            "type": "string",
            "format": "uuid"
          },
          "idFlowState": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "isInitial": {
            "oneOf": [
              {
                "type": "boolean"
              },
              {
                "type": "integer",
                "enum": [
                  0,
                  1
                ]
              }
            ]
          },
          "isFinal": {
            "oneOf": [
              {
                "type": "boolean"
              },
              {
                "type": "integer",
                "enum": [
                  0,
                  1
                ]
              }
            ]
          },
          "sortOrder": {
            "type": "integer"
          }
        },
        "required": [
          "idFlow",
          "idFlowState",
          "name",
          "isInitial",
          "isFinal",
          "sortOrder"
        ]
      },
      "ConfiguracionTransicion": {
        "type": "object",
        "properties": {
          "idFlow": {
            "type": "string",
            "format": "uuid"
          },
          "idFromState": {
            "type": "string",
            "format": "uuid"
          },
          "idToState": {
            "type": "string",
            "format": "uuid"
          }
        },
        "required": [
          "idFlow",
          "idFromState",
          "idToState"
        ]
      },
      "ConfiguracionFlujo": {
        "type": "object",
        "properties": {
          "idFlow": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "versionNumber": {
            "type": "integer"
          },
          "active": {
            "oneOf": [
              {
                "type": "boolean"
              },
              {
                "type": "integer",
                "enum": [
                  0,
                  1
                ]
              }
            ]
          },
          "states": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ConfiguracionEstado"
            }
          },
          "transitions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ConfiguracionTransicion"
            }
          }
        },
        "required": [
          "idFlow",
          "name",
          "versionNumber",
          "active",
          "states",
          "transitions"
        ]
      },
      "ConfiguracionIntegracion": {
        "type": "object",
        "properties": {
          "application": {
            "type": "object",
            "properties": {
              "idApplication": {
                "type": "string",
                "format": "uuid"
              },
              "name": {
                "type": "string"
              },
              "defaultVisibility": {
                "type": "string"
              }
            },
            "required": [
              "idApplication",
              "name",
              "defaultVisibility"
            ]
          },
          "flows": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ConfiguracionFlujo"
            }
          }
        },
        "required": [
          "application",
          "flows"
        ]
      }
    }
  }
}
