openapi: 3.1.0
info:
  title: OHN-02 Health ID Management
  version: 1.0.0
  description: >
    Written from the observed behaviour of the TypeScript reference (backend/modules/ohn-02, 2026-11-17).
    The by-subject operation depends on OHN-03 through a port; until the service-token decision (OWNER-BLOCKERS B38) it fails closed with 503.
servers: [{ url: /api/ohn/v1 }]
security: [{ gatewayToken: [] }]
tags: [{ name: health-ids }, { name: reports }]
paths:
  /patients:
    post:
      operationId: issueHealthId
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/IssueHealthIdCommand' } } } }
      responses:
        '201': { description: Identity and first health id created, content: { application/json: { schema: { $ref: '#/components/schemas/HealthIdentifier' } } } }
        '422': { $ref: '#/components/responses/Problem' }
        '503': { $ref: '#/components/responses/Problem' }
  /patients/{id}/health-ids:
    post:
      operationId: issueAdditionalHealthId
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/IssueAdditionalIdCommand' } } } }
      responses:
        '201': { description: Additional id, content: { application/json: { schema: { $ref: '#/components/schemas/HealthIdentifier' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /patients/by-subject/{subject}:
    get:
      operationId: getPatientBySubject
      tags: [health-ids]
      parameters: [{ name: subject, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: Binding, content: { application/json: { schema: { $ref: '#/components/schemas/SubjectBinding' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '503': { $ref: '#/components/responses/Problem' }
  /health-ids:
    get:
      operationId: listHealthIds
      tags: [health-ids]
      responses:
        '200': { description: Identifiers with their patient, content: { application/json: { schema: { $ref: '#/components/schemas/HealthIdentifierList' } } } }
  /health-ids/resolve:
    post:
      operationId: resolveHealthId
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: true } } } }
      responses:
        '200': { description: Patient reference, content: { application/json: { schema: { $ref: '#/components/schemas/Resolution' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /health-ids/{id}/verify:
    get:
      operationId: verifyHealthId
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Active identifier, content: { application/json: { schema: { $ref: '#/components/schemas/HealthIdentifier' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /health-ids/{id}/recover:
    post:
      operationId: recoverHealthId
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RecoveryCommand' } } } }
      responses:
        '201': { description: Alias recorded and identifier set to recovered, content: { application/json: { schema: { $ref: '#/components/schemas/Recovery' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /health-ids/{id}/status:
    post:
      operationId: changeHealthIdStatus
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/StatusChangeCommand' } } } }
      responses:
        '200': { description: Updated, content: { application/json: { schema: { $ref: '#/components/schemas/HealthIdentifier' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /health-ids/{id}/history:
    get:
      operationId: getHealthIdHistory
      tags: [health-ids]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Status history oldest first, content: { application/json: { schema: { $ref: '#/components/schemas/HistoryList' } } } }
  /identifier-aliases:
    get:
      operationId: listIdentifierAliases
      tags: [health-ids]
      responses:
        '200': { description: Aliases, content: { application/json: { schema: { $ref: '#/components/schemas/RecoveryList' } } } }
  /health-ids/reports:
    get:
      operationId: listReports
      tags: [reports]
      responses:
        '200': { description: Reports, content: { application/json: { schema: { $ref: '#/components/schemas/ReportList' } } } }
        '403': { $ref: '#/components/responses/Problem' }
  /health-ids/issuance-exceptions:
    get:
      operationId: listIssuanceExceptions
      tags: [reports]
      responses:
        '200': { description: Failed or retried issuances without identifiers, content: { application/json: { schema: { type: object, additionalProperties: true } } } }
        '403': { $ref: '#/components/responses/Problem' }
  /health-ids/reports/{reportId}:
    get:
      operationId: runReport
      tags: [reports]
      parameters:
        - { name: reportId, in: path, required: true, schema: { type: string } }
        - { name: jurisdiction, in: query, schema: { type: string } }
        - { name: channel, in: query, schema: { type: string } }
        - { name: status, in: query, schema: { type: string } }
        - { name: period_from, in: query, schema: { type: string } }
        - { name: period_to, in: query, schema: { type: string } }
        - { name: date, in: query, schema: { type: string } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Report, content: { application/json: { schema: { $ref: '#/components/schemas/Report' } } } }
        '403': { $ref: '#/components/responses/Problem' }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /health-ids/reports/{reportId}/export:
    post:
      operationId: exportReport
      tags: [reports]
      parameters: [{ name: reportId, in: path, required: true, schema: { type: string } }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ExportCommand' } } } }
      responses:
        '201': { description: Export created - single use and valid for five minutes, content: { application/json: { schema: { $ref: '#/components/schemas/ExportReceipt' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /health-ids/report-exports/{token}:
    get:
      operationId: downloadReportExport
      tags: [reports]
      parameters: [{ name: token, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: The file as csv or pdf, content: { text/csv: { schema: { type: string, format: binary } }, application/pdf: { schema: { type: string, format: binary } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '410': { $ref: '#/components/responses/Problem' }
components:
  securitySchemes:
    gatewayToken: { type: http, scheme: bearer, bearerFormat: JWT, description: ES256 gateway token with audience ohn-02 }
  parameters:
    Id: { name: id, in: path, required: true, schema: { type: string } }
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, maxLength: 512 } }
  responses:
    Problem:
      description: application/problem+json with the legacy error code
      content: { application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } } }
  schemas:
    Problem:
      type: object
      required: [error]
      properties:
        error: { type: string }
        message: { type: string }
        details: { type: object, additionalProperties: true }
        trace_id: { type: string }
    IssueHealthIdCommand:
      type: object
      required: [jurisdiction_id, patient_identity_ref, proofing_case_id, registration_channel, language]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        patient_identity_ref: { type: string, format: uuid }
        proofing_case_id: { type: string, format: uuid }
        registration_channel: { type: string, minLength: 1, maxLength: 128 }
        language: { type: string, maxLength: 32, pattern: '^[a-z]{2}(-[A-Z]{2})?$' }
        idempotency_key: { type: string, minLength: 1, maxLength: 512 }
    IssueAdditionalIdCommand:
      type: object
      required: [jurisdiction_id, verification_purpose, requesting_facility_id]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        verification_purpose: { type: string, minLength: 1, maxLength: 128 }
        requesting_facility_id: { type: string, format: uuid }
        patient_disclosure_authority: { type: string, format: uuid }
    HealthIdentifier:
      type: object
      properties:
        id: { type: string, format: uuid }
        health_identity_id: { type: string, format: uuid }
        health_id: { type: string }
        jurisdiction_id: { type: string, format: uuid }
        verification_purpose: { type: string }
        requesting_facility_id: { type: string, format: uuid }
        patient_disclosure_authority: { type: [string, 'null'] }
        status: { type: string }
        version: { type: integer }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
        patient_id: { type: [string, 'null'] }
    HealthIdentifierList:
      type: object
      properties: { data: { type: array, items: { $ref: '#/components/schemas/HealthIdentifier' } } }
    SubjectBinding:
      type: object
      properties:
        patient_id: { type: string }
        health_id: { type: [string, 'null'] }
        status: { type: string, enum: [active, suspended] }
    Resolution:
      type: object
      properties:
        patient_id: { type: string }
        status: { type: string }
    RecoveryCommand:
      type: object
      required: [identity_evidence_ref, recovery_reason, reviewer_id]
      properties:
        claimed_id: { type: string, maxLength: 512 }
        identity_evidence_ref: { type: string, format: uuid }
        recovery_reason: { type: string, minLength: 10, maxLength: 4096 }
        verified_contact: { type: string, maxLength: 512 }
        reviewer_id: { type: string, format: uuid }
    Recovery:
      type: object
      properties:
        id: { type: string, format: uuid }
        health_identifier_id: { type: string, format: uuid }
        claimed_id: { type: [string, 'null'] }
        identity_evidence_ref: { type: string, format: uuid }
        recovery_reason: { type: string }
        verified_contact: { type: [string, 'null'] }
        reviewer_id: { type: string, format: uuid }
        status: { type: string }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    RecoveryList:
      type: object
      properties: { data: { type: array, items: { $ref: '#/components/schemas/Recovery' } } }
    StatusChangeCommand:
      type: object
      required: [requested_status, reason, effective_at, approver_id]
      properties:
        requested_status: { type: string, enum: [suspended, retired, active] }
        reason: { type: string, minLength: 10, maxLength: 4096 }
        effective_at: { type: string, format: date-time }
        approver_id: { type: string, format: uuid }
    HistoryEntry:
      type: object
      properties:
        id: { type: string, format: uuid }
        health_identifier_id: { type: string, format: uuid }
        previous_status: { type: string }
        new_status: { type: string }
        reason: { type: string }
        effective_at: { type: string, format: date-time }
        approver_id: { type: string, format: uuid }
        created_at: { type: string, format: date-time }
    HistoryList:
      type: object
      properties: { data: { type: array, items: { $ref: '#/components/schemas/HistoryEntry' } } }
    ReportList:
      type: object
      properties: { reports: { type: array, items: { type: object, properties: { id: { type: string }, title: { type: string } } } } }
    Report:
      type: object
      required: [report_id, title, columns, rows, summary, state, generated_at, next_cursor]
      properties:
        report_id: { type: string }
        title: { type: string }
        columns: { type: array, items: { type: string } }
        rows: { type: array, items: { type: object, additionalProperties: true } }
        summary: { type: object, additionalProperties: true }
        state: { type: string, enum: [rendered, no_authorized_data] }
        generated_at: { type: string, format: date-time }
        next_cursor: { type: [string, 'null'] }
    ExportCommand:
      type: object
      required: [format]
      properties:
        format: { type: string, enum: [csv, pdf] }
        filters: { type: object, additionalProperties: true }
    ExportReceipt:
      type: object
      properties:
        export_id: { type: string, format: uuid }
        report_id: { type: string }
        format: { type: string }
        rows: { type: integer }
        expires_at: { type: string, format: date-time }
        single_use: { type: boolean }
        download_path: { type: string }
