{
  "openapi": "3.1.0",
  "info": {
    "title": "ClaimDesk Clearinghouse API",
    "version": "1.0.0",
    "description": "Independent Nigerian healthcare claims clearinghouse API for HMS platforms, hospitals, and HMOs. Enforces NHIA 1-hour Pre-Authorization SLAs, versioned date-of-care tariff schedules, integer kobo precision, ICD-10/ICD-11 medical-necessity scrubbing, dual-party tenant isolation, and HMAC-SHA256 webhooks.",
    "contact": {
      "name": "ClaimDesk API Platform",
      "url": "https://claimdesk.ng"
    }
  },
  "servers": [
    {
      "url": "https://api.claimdesk.ng",
      "description": "ClaimDesk Clearinghouse API"
    },
    {
      "url": "https://api.claimdesk.ng",
      "description": "Production API"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Use a live organization API key (cd_live_...), a ClaimDesk session JWT (eyJ...), or a sandbox token (cd_demo_portal)."
      }
    },
    "parameters": {
      "OrgIdHeader": {
        "name": "X-ClaimDesk-Org-ID",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        },
        "description": "Active organization UUID (Hospital, HMO, or HMS Partner)."
      },
      "HospitalIdHeader": {
        "name": "X-ClaimDesk-Hospital-ID",
        "in": "header",
        "required": false,
        "schema": {
          "type": "string",
          "format": "uuid"
        },
        "description": "Required when calling with an HMS_PARTNER key to act on behalf of a specific child hospital."
      },
      "IdempotencyKeyHeader": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": false,
        "schema": {
          "type": "string"
        },
        "description": "Unique client-generated key per mutation (POST/PATCH) to guarantee safe retries without duplicates."
      }
    }
  },
  "tags": [
    {
      "name": "System",
      "description": "Health, discovery, OpenAPI spec & SDK download"
    },
    {
      "name": "Authentication",
      "description": "Native ClaimDesk JWT registration, login & password management"
    },
    {
      "name": "Organizations & Partners",
      "description": "Directory, self-service org onboarding & HMS Partner child hospital provisioning"
    },
    {
      "name": "Tariffs",
      "description": "Versioned date-of-care HMO tariff schedules (zero overwrite)"
    },
    {
      "name": "Pre-Authorizations",
      "description": "NHIA 1-hour SLA PA workflow & 48-hour emergency retro"
    },
    {
      "name": "Claims & Scrubbing",
      "description": "Pre-flight scrubbing, auto-fix, submission, CSV import, batch exports & adjudication"
    },
    {
      "name": "Attachments",
      "description": "Dual-party clinical evidence vault (PDF/PNG/JPEG up to 2 MB)"
    },
    {
      "name": "Settlements & Analytics",
      "description": "NIP settlement batches, bank CSV auto-reconciler & HMO payer scorecard"
    },
    {
      "name": "API Keys & Webhooks",
      "description": "Self-service cd_live_... keys, HMAC-SHA256 webhooks & live delivery retries"
    }
  ],
  "paths": {
    "/v1/health": {
      "get": {
        "tags": [
          "System"
        ],
        "summary": "API health & compliance metadata",
        "security": [],
        "responses": {
          "200": {
            "description": "Service healthy"
          }
        }
      }
    },
    "/v1/openapi.json": {
      "get": {
        "tags": [
          "System"
        ],
        "summary": "Download OpenAPI 3.1 specification",
        "security": [],
        "responses": {
          "200": {
            "description": "OpenAPI 3.1 JSON document"
          }
        }
      }
    },
    "/v1/sdk/claimdesk-client.ts": {
      "get": {
        "tags": [
          "System"
        ],
        "summary": "Download zero-dependency TypeScript SDK & HMAC webhook verifier",
        "security": [],
        "parameters": [
          {
            "name": "org_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "uuid"
            },
            "description": "Optional Organization UUID to pre-fill in the SDK defaults."
          }
        ],
        "responses": {
          "200": {
            "description": "TypeScript SDK source file"
          }
        }
      }
    },
    "/v1/auth/register": {
      "post": {
        "tags": [
          "Authentication"
        ],
        "summary": "Register a ClaimDesk staff account (returns signed JWT immediately)",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email",
                  "password"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email"
                  },
                  "password": {
                    "type": "string",
                    "minLength": 6
                  },
                  "full_name": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Account created with JWT and org memberships"
          }
        }
      }
    },
    "/v1/auth/login": {
      "post": {
        "tags": [
          "Authentication"
        ],
        "summary": "Authenticate and receive a 7-day signed ClaimDesk JWT",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email",
                  "password"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email"
                  },
                  "password": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Authenticated session JWT"
          }
        }
      }
    },
    "/v1/organizations": {
      "get": {
        "tags": [
          "Organizations & Partners"
        ],
        "summary": "List HMOs, own hospital, or child hospitals (for HMS Partners)",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Accessible organizations"
          }
        }
      },
      "post": {
        "tags": [
          "Organizations & Partners"
        ],
        "summary": "Create an organization or programmatically provision a child HOSPITAL (HMS Partner)",
        "description": "When called by an HMS_PARTNER API key with type=\"HOSPITAL\", automatically links parent_hms_id to your partner org. Pass issue_api_key=true to receive a dedicated hospital-scoped cd_live_... API key immediately.",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKeyHeader"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "name",
                  "type"
                ],
                "properties": {
                  "name": {
                    "type": "string",
                    "example": "Lekki Specialist Hospital"
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "HOSPITAL",
                      "HMO",
                      "HMS_PARTNER"
                    ]
                  },
                  "nhia_code": {
                    "type": "string",
                    "example": "NHIA-LA-0991"
                  },
                  "tier": {
                    "type": "string",
                    "example": "TERTIARY"
                  },
                  "webhook_url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "live_webhook_enabled": {
                    "type": "boolean"
                  },
                  "issue_api_key": {
                    "type": "boolean",
                    "description": "Mint a scoped cd_live_... key for this new org immediately."
                  },
                  "api_key_label": {
                    "type": "string",
                    "example": "NaijaHealth Production Key"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Organization provisioned (+ optional initial_api_key & initial_webhook_secret)"
          }
        }
      }
    },
    "/v1/tariffs": {
      "get": {
        "tags": [
          "Tariffs"
        ],
        "summary": "Resolve active contracted HMO tariffs for a specific encounter_date",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          },
          {
            "name": "hmo_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "plan_tier",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "example": "GOLD"
            }
          },
          {
            "name": "encounter_date",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date",
              "example": "2026-12-01"
            }
          },
          {
            "name": "include_history",
            "in": "query",
            "required": false,
            "schema": {
              "type": "boolean"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Matched contracted tariffs in integer kobo"
          }
        }
      },
      "post": {
        "tags": [
          "Tariffs"
        ],
        "summary": "Publish or upload a versioned contracted HMO tariff (HMO or Hospital-owned)",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "201": {
            "description": "Versioned tariff created (never overwrites historical prices)"
          }
        }
      }
    },
    "/v1/tariffs/import-csv": {
      "post": {
        "tags": [
          "Tariffs"
        ],
        "summary": "Bulk import a hospital or HMO contracted tariff sheet via CSV",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "201": {
            "description": "Imported versioned tariffs summary"
          }
        }
      }
    },
    "/v1/pre-authorizations/{id}/record-response": {
      "patch": {
        "tags": [
          "Pre-Authorizations"
        ],
        "summary": "Record an external HMO PA code or rejection received via WhatsApp, Email, Phone, or HMO Portal",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "SLA timer closed and pa.approved / pa.rejected webhook pushed"
          }
        }
      }
    },
    "/v1/settlements/record-external": {
      "post": {
        "tags": [
          "Settlements & Analytics"
        ],
        "summary": "Record an external NIP bank payout received from an HMO (computes shortfall & updates AR)",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "201": {
            "description": "Settlement batch recorded & settlement.paid webhook dispatched"
          }
        }
      }
    },
    "/v1/pre-authorizations": {
      "get": {
        "tags": [
          "Pre-Authorizations"
        ],
        "summary": "List Pre-Authorizations with live NHIA 1-hour SLA status",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Pre-authorization records"
          }
        }
      },
      "post": {
        "tags": [
          "Pre-Authorizations"
        ],
        "summary": "Request HMO Pre-Authorization (starts 60-minute NHIA SLA or 48h emergency window)",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          },
          {
            "$ref": "#/components/parameters/HospitalIdHeader"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKeyHeader"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "hmo_org_id",
                  "enrollee_id",
                  "plan_tier",
                  "icd10_code",
                  "clinical_summary",
                  "requested_services"
                ],
                "properties": {
                  "hmo_org_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "external_ref_id": {
                    "type": "string",
                    "example": "NH-ENC-90512"
                  },
                  "enrollee_id": {
                    "type": "string",
                    "example": "AXA/NG/2026/041190"
                  },
                  "patient_consent_ref": {
                    "type": "string",
                    "example": "NDPA-CONSENT-2026-90512"
                  },
                  "plan_tier": {
                    "type": "string",
                    "example": "GOLD"
                  },
                  "icd10_code": {
                    "type": "string",
                    "example": "K35.80"
                  },
                  "clinical_summary": {
                    "type": "string"
                  },
                  "is_emergency": {
                    "type": "boolean"
                  },
                  "requested_services": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "service_code": {
                          "type": "string",
                          "example": "SURG-APP-001"
                        },
                        "service_name": {
                          "type": "string",
                          "example": "Emergency Appendectomy"
                        },
                        "quantity": {
                          "type": "integer",
                          "example": 1
                        },
                        "estimated_kobo": {
                          "type": "integer",
                          "example": 48000000
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "PA created with sla_deadline_at"
          }
        }
      }
    },
    "/v1/claims/scrub": {
      "post": {
        "tags": [
          "Claims & Scrubbing"
        ],
        "summary": "Pre-flight claim scrubber (PA, tariff ceiling, ICD-10 medical necessity & 90/10 co-pay)",
        "description": "Validates a draft claim before submission and returns scrub_errors, clinical_warnings, medical_necessity_score (0-100), exact kobo totals, and a ready-to-submit auto_fixed_payload.",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          },
          {
            "$ref": "#/components/parameters/HospitalIdHeader"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "hmo_org_id",
                  "enrollee_id",
                  "plan_tier",
                  "encounter_date",
                  "icd10_code",
                  "line_items"
                ],
                "properties": {
                  "hmo_org_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "external_ref_id": {
                    "type": "string",
                    "example": "NH-ENC-90512"
                  },
                  "enrollee_id": {
                    "type": "string",
                    "example": "AXA/NG/2026/041190"
                  },
                  "plan_tier": {
                    "type": "string",
                    "example": "GOLD"
                  },
                  "encounter_date": {
                    "type": "string",
                    "format": "date",
                    "example": "2026-12-01"
                  },
                  "icd10_code": {
                    "type": "string",
                    "example": "I10"
                  },
                  "pa_code": {
                    "type": "string",
                    "nullable": true
                  },
                  "patient_consent_ref": {
                    "type": "string",
                    "example": "NDPA-CONSENT-001"
                  },
                  "line_items": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "required": [
                        "service_code",
                        "quantity",
                        "billed_unit_kobo"
                      ],
                      "properties": {
                        "service_code": {
                          "type": "string",
                          "example": "DRUG-AML-005"
                        },
                        "quantity": {
                          "type": "integer",
                          "example": 1
                        },
                        "billed_unit_kobo": {
                          "type": "integer",
                          "example": 1800000
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Scrub report + auto_fixed_payload"
          }
        }
      }
    },
    "/v1/claims": {
      "get": {
        "tags": [
          "Claims & Scrubbing"
        ],
        "summary": "List claims visible to the authenticated Hospital, HMO, or HMS Partner",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Claims array"
          }
        }
      },
      "post": {
        "tags": [
          "Claims & Scrubbing"
        ],
        "summary": "Submit a claim (clean claims auto-approve straight-through)",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          },
          {
            "$ref": "#/components/parameters/HospitalIdHeader"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKeyHeader"
          }
        ],
        "responses": {
          "201": {
            "description": "Claim recorded (AUTO_APPROVED or IN_VETTING)"
          }
        }
      }
    },
    "/v1/claims/export": {
      "get": {
        "tags": [
          "Claims & Scrubbing"
        ],
        "summary": "Export claims batch as CSV, Excel (.xls), or HL7 FHIR R4 JSON Bundle",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          },
          {
            "name": "format",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "csv",
                "excel",
                "json"
              ],
              "default": "csv"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Downloadable batch file"
          }
        }
      }
    },
    "/v1/attachments": {
      "post": {
        "tags": [
          "Attachments"
        ],
        "summary": "Upload clinical attachment (PDF/PNG/JPEG/TXT up to 2 MB, base64) to a PA or Claim",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "201": {
            "description": "Attachment stored with SHA-256 checksum"
          }
        }
      }
    },
    "/v1/settlements": {
      "get": {
        "tags": [
          "Settlements & Analytics"
        ],
        "summary": "List NIP settlement batches and remittance advices",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Settlement batches"
          }
        }
      }
    },
    "/v1/settlements/reconcile-csv": {
      "post": {
        "tags": [
          "Settlements & Analytics"
        ],
        "summary": "Auto-reconcile bank credit CSV against NIP settlements and flag short-payments",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Variance reconciliation summary"
          }
        }
      }
    },
    "/v1/analytics/scorecard": {
      "get": {
        "tags": [
          "Settlements & Analytics"
        ],
        "summary": "HMO Payer Performance Scorecard (A-F grade) & 30/60/90+ day Aging Receivables",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Scorecard & aging buckets"
          }
        }
      }
    },
    "/v1/api-keys": {
      "get": {
        "tags": [
          "API Keys & Webhooks"
        ],
        "summary": "List organization API keys and webhook configuration",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "API key metadata and webhook config"
          }
        }
      },
      "post": {
        "tags": [
          "API Keys & Webhooks"
        ],
        "summary": "Generate a new live API key (cd_live_...)",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "201": {
            "description": "Plaintext API key returned once"
          }
        }
      }
    },
    "/v1/webhooks/test": {
      "post": {
        "tags": [
          "API Keys & Webhooks"
        ],
        "summary": "Send a live signed test webhook (webhook.test) to verify receiver",
        "parameters": [
          {
            "$ref": "#/components/parameters/OrgIdHeader"
          }
        ],
        "responses": {
          "200": {
            "description": "Live delivery diagnostics"
          }
        }
      }
    }
  }
}