openapi: 3.0.3
info:
  title: Sergios Integration Hub API
  version: "1.0"
  description: |
    APIs for integrating with Sergios: the KYC gateway (Spin), billing and subscriptions, core banking (CBS)
    callbacks and loan actions. A starting reference; more endpoints will be added.

    **Authentication.** Every request is a `POST` with a JSON body that carries the user's session `token`
    (issued at sign-in): `{"token": "<token>", ...}`. The server works out the organization from the token
    (or the organization a super admin has switched to); organization ids sent by a client are ignored.

    **Responses.** Enterprise Lending, KYC, billing and subscription endpoints (`/los/...`) answer
    `{"status": "success" | "failed", "message": "<what happened>", "data": {...}}`, with the HTTP status of the
    outcome (400 invalid, 403 not allowed, 404 not found). Older endpoints answer `{"code": "800.200.001", "message", "data"}`
    with HTTP 200; `800.200.001` is success.

    **Billing rule.** Every successful live KYC / CRB query is billed exactly once, at the organization's price plus
    VAT on the query date. Failed queries (including an HTTP 200 whose body carries a Spin error code) and sample
    queries are never billed. Send a `request_id` to make a retried request safe.

    **Support:** WhatsApp +254 721 765579.
  contact:
    name: Sergios support (WhatsApp)
    url: https://wa.me/254721765579
servers:
  - url: https://api.sergios.africa/api
    description: Production
tags:
  - name: Integration hub
    description: How each integration connects, and where it's configured.
  - name: KYC gateway (Spin)
    description: KYC checks and queries through Spin V2. Sample (mock) or live is decided by the organization's saved gateway mode, never by the request.
  - name: SPIN configuration
    description: Super admins only. The Spin base URL and the platform's Spin account.
  - name: Billing
    description: Query logs, usage and statements. Customers see their own organization only.
  - name: Billing (super admin)
    description: Consolidated reporting, pricing (amount + VAT), billing rules, reconciliation and direct Spin usage.
  - name: Subscriptions
    description: KYC services and CRB verifications an organization subscribes to, with its prices.
  - name: Core banking (CBS)
    description: Disbursement requests sent to the CBS, and the signed callbacks it sends back.
  - name: Loans
    description: Loan Book actions.
paths:
  /los/integrations/contract/:
    post:
      tags: [Integration hub, Core banking (CBS)]
      summary: The CBS integration contract
      description: The organization's CBS integration as a step-by-step contract (requests, signed headers and sample payloads), generated from its saved configuration.
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
  /los/kyc_settings/:
    post:
      tags: [Integration hub, KYC gateway (Spin)]
      summary: The organization's KYC gateway and checks
      description: Whose Spin credentials are used (`PLATFORM` or `OWN`), sample or live mode, and the minimums each check must meet. Secrets are never returned.
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
  /los/kyc_settings/test_gateway/:
    post:
      tags: [KYC gateway (Spin)]
      summary: Test the Spin connection
      description: Requests an access token with the typed (or saved) credentials. Nothing is saved; the token is never returned.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  properties:
                    gateway:
                      type: object
                      properties:
                        source: { type: string, enum: [PLATFORM, OWN] }
                        consumer_key: { type: string }
                        consumer_secret: { type: string, format: password }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
  /los/customers/kyc/:
    post:
      tags: [KYC gateway (Spin)]
      summary: Run a KYC check
      description: |
        Runs the built-in checks and queries the chosen subscribed services for a customer. Live queries are billed
        once each when successful. Repeat the same `request_id` (e.g. after a timeout) to get the same check back
        without querying Spin, or billing, again.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [customer_id, run]
                  properties:
                    customer_id: { type: string, format: uuid }
                    run: { type: boolean, example: true }
                    service_codes: { type: array, items: { type: string }, example: [IPRS] }
                    consent: { type: boolean, description: The customer's consent; required when any service is queried. }
                    inputs: { type: object, additionalProperties: { type: string }, description: Identifiers not on the customer record. }
                    request_id: { $ref: '#/components/schemas/RequestId' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
  /los/customers/kyc/query/:
    post:
      tags: [KYC gateway (Spin)]
      summary: Query one service on an existing KYC check
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [check_id, service_code]
                  properties:
                    check_id: { type: string, format: uuid }
                    service_code: { type: string, example: PASSPORTCHECK }
                    consent: { type: boolean }
                    inputs: { type: object, additionalProperties: { type: string } }
                    request_id: { $ref: '#/components/schemas/RequestId' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
  /los/kyc_platform/:
    post:
      tags: [SPIN configuration]
      summary: The Spin base URL and platform account
      description: Effective base URL (saved here, else the server's SPIN_BASE_URL), approved hosts, last connection and the platform account (no secrets).
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '403': { $ref: '#/components/responses/Forbidden' }
  /los/kyc_platform/save/:
    post:
      tags: [SPIN configuration]
      summary: Save the Spin base URL / platform account
      description: The URL must be https, on an approved Spin host, and not resolve to a private address. The next query uses it, without a restart. Audited.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  properties:
                    base_url: { type: string, example: 'https://stage-radicrunch.spinmobile.co/api', description: 'Blank: use the server default.' }
                    environment_label: { type: string, enum: ['', STAGING, SANDBOX, PRODUCTION] }
                    name: { type: string, example: SpinCrunch }
                    consumer_key: { type: string }
                    consumer_secret: { type: string, format: password, description: Write-only. }
                    organization_code: { type: string, example: LP }
                    timeout_seconds: { type: integer, minimum: 5, maximum: 60 }
                    is_active: { type: boolean }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
        '403': { $ref: '#/components/responses/Forbidden' }
  /los/kyc_platform/test/:
    post:
      tags: [SPIN configuration]
      summary: Test a Spin base URL and credentials
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
  /los/billing/queries/:
    post:
      tags: [Billing]
      summary: The organization's query log
      description: Paged queries with the customer queried, outcome, billing status and charge (amount, VAT, total), plus totals, trend and top services for the same filters.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - $ref: '#/components/schemas/QueryFilters'
      responses:
        '200':
          description: Query page
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/Envelope'
                  - type: object
                    properties:
                      data:
                        type: object
                        properties:
                          entries: { type: array, items: { $ref: '#/components/schemas/QueryRow' } }
                          total: { type: integer }
                          page: { type: integer }
                          page_size: { type: integer }
  /los/billing/queries/export/:
    post:
      tags: [Billing]
      summary: Export the query log (CSV)
      requestBody:
        content:
          application/json:
            schema: { allOf: [ { $ref: '#/components/schemas/Token' }, { $ref: '#/components/schemas/QueryFilters' } ] }
      responses:
        '200': { description: CSV file, content: { text/csv: {} } }
  /los/billing/statements/:
    post:
      tags: [Billing]
      summary: Finalized statements (or one, with statement_id)
      description: A statement's lines (amount, VAT, total) and the exact queries it bills; plus the unbilled estimate.
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
  /los/billing/dispute/:
    post:
      tags: [Billing]
      summary: Dispute a charge
      description: Flags one of the organization's billable charges for review. The charge doesn't change until a super admin reviews it.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [charge_id, note]
                  properties:
                    charge_id: { type: string, format: uuid }
                    note: { type: string, maxLength: 300 }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '404': { $ref: '#/components/responses/Failed' }
  /los/subscriptions/:
    post:
      tags: [Subscriptions]
      summary: Services, subscriptions and prices
      description: KYC services and CRB verifications (kind KYC / CRB) with this organization's price per query (amount, VAT rate, VAT, total) and this period's usage.
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
  /los/subscriptions/change/:
    post:
      tags: [Subscriptions]
      summary: Subscribe or cancel
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [code, subscribed]
                  properties:
                    code: { type: string, example: Metropol }
                    subscribed: { type: boolean }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '403': { $ref: '#/components/responses/Forbidden' }
  /los/billing/admin/queries/:
    post:
      tags: [Billing (super admin)]
      summary: Consolidated query log (all organizations)
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - $ref: '#/components/schemas/QueryFilters'
                - type: object
                  properties:
                    corporate_id: { type: string, format: uuid }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '403': { $ref: '#/components/responses/Forbidden' }
  /los/billing/admin/prices/add/:
    post:
      tags: [Billing (super admin)]
      summary: Add a price (amount + VAT)
      description: Price per successful live query for a service, from a date, for the platform or one organization. Prices never change once saved; a new price ends the previous one. VAT defaults to 16%.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [service_code, unit_price, effective_from]
                  properties:
                    service_code: { type: string, example: Metropol }
                    corporate_id: { type: string, format: uuid, description: 'Blank: the platform price.' }
                    unit_price: { type: string, example: '50.00', description: Before VAT. }
                    vat_rate: { type: string, example: '16', default: '16' }
                    currency: { type: string, example: KES }
                    effective_from: { type: string, format: date }
                    note: { type: string }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
        '403': { $ref: '#/components/responses/Forbidden' }
  /los/billing/admin/policy/:
    post:
      tags: [Billing (super admin)]
      summary: Billing rules
      description: 'Successful live queries are always billable and failed or sample ones never are. Partial, not-found and asynchronous outcomes can be set per platform or organization (send save = true).'
      requestBody: { $ref: '#/components/requestBodies/TokenOnly' }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
  /los/billing/admin/finalize/:
    post:
      tags: [Billing (super admin)]
      summary: Finalize a billing period
      description: Freezes the period's charges onto a statement (subtotal, VAT, total) and optionally creates an unpaid invoice. Blocked while charges are pending review, disputed or unpriced.
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [corporate_id, period_start, period_end]
                  properties:
                    corporate_id: { type: string, format: uuid }
                    period_start: { type: string, format: date }
                    period_end: { type: string, format: date }
                    create_invoice: { type: boolean }
                    due_date: { type: string, format: date }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
        '400': { $ref: '#/components/responses/Failed' }
  /los/billing/admin/direct/import/:
    post:
      tags: [Billing (super admin), Integration hub]
      summary: Import direct Spin usage (CSV)
      description: For organizations that call Spin directly. Columns reference, occurred_at, service_code, status (FOUND, NOT_FOUND, PARTIAL, ERROR, COMPLETED), environment (LIVE, SAMPLE). Re-importing skips references already imported.
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required: [token, corporate_id, period_start, period_end, file]
              properties:
                token: { type: string }
                corporate_id: { type: string, format: uuid }
                period_start: { type: string, format: date }
                period_end: { type: string, format: date }
                file: { type: string, format: binary }
      responses:
        '200': { $ref: '#/components/responses/Ok' }
  /los/integrations/cbs/disbursement/callback/:
    post:
      tags: [Core banking (CBS), Integration hub]
      summary: CBS disbursement callback (from the CBS)
      description: Signed by the CBS with the integration's shared secret. Replays of the same `event_id` are accepted once.
      parameters:
        - { $ref: '#/components/parameters/Signature' }
        - { $ref: '#/components/parameters/Timestamp' }
        - { $ref: '#/components/parameters/IntegrationId' }
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required: [event_id, request_id, loan_reference, status]
              properties:
                event_id: { type: string, example: evt-20261019-0002 }
                request_id: { type: string }
                loan_reference: { type: string }
                correlation_id: { type: string }
                status: { type: string, enum: [PROCESSING, SUCCESSFUL, FAILED] }
                amount: { type: string, example: '500000.00' }
                currency: { type: string, example: KES }
      responses:
        '200': { description: 'Accepted: `{"status": "success", "code": "ACCEPTED", ...}`' }
        '401': { description: Signature or timestamp invalid. }
  /los/integrations/cbs/repayment/callback/:
    post:
      tags: [Core banking (CBS), Integration hub]
      summary: CBS repayment callback (from the CBS)
      parameters:
        - { $ref: '#/components/parameters/Signature' }
        - { $ref: '#/components/parameters/Timestamp' }
        - { $ref: '#/components/parameters/IntegrationId' }
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required: [event_id, repayment_id, loan_reference, amount, status]
              properties:
                event_id: { type: string, example: evt-rp-7781 }
                repayment_id: { type: string, example: RP-7781 }
                loan_reference: { type: string }
                external_transaction_reference: { type: string }
                amount: { type: string, example: '25000.00' }
                currency: { type: string, example: KES }
                transaction_date: { type: string, format: date-time }
                status: { type: string, enum: [SUCCESSFUL, REVERSED] }
                reverses_repayment_id: { type: string }
      responses:
        '200': { description: Accepted. }
        '401': { description: Signature or timestamp invalid. }
  /request_loan_activation/:
    post:
      tags: [Loans]
      summary: Request a loan activation (needs another user's approval)
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/Token'
                - type: object
                  required: [activity_id, receipt]
                  properties:
                    activity_id: { type: string, format: uuid }
                    receipt: { type: string, example: TW-622391 }
                    confirmation_key: { type: string }
      responses:
        '200': { $ref: '#/components/responses/Coded' }
  /approve_loan_activation/:
    post:
      tags: [Loans]
      summary: Approve a loan activation (a different user)
      requestBody:
        content:
          application/json:
            schema: { allOf: [ { $ref: '#/components/schemas/Token' }, { type: object, required: [activity_id], properties: { activity_id: { type: string, format: uuid } } } ] }
      responses:
        '200': { $ref: '#/components/responses/Coded' }
  /complete_loan/:
    post:
      tags: [Loans]
      summary: Complete a loan (all instalments Complete)
      requestBody:
        content:
          application/json:
            schema: { allOf: [ { $ref: '#/components/schemas/Token' }, { type: object, required: [activity_id], properties: { activity_id: { type: string, format: uuid } } } ] }
      responses:
        '200': { $ref: '#/components/responses/Coded' }
components:
  parameters:
    Signature:
      name: X-Zerahi-Signature
      in: header
      required: true
      description: '`sha256=<hex HMAC-SHA256 of "<timestamp>.<raw body>">` with the integration''s shared secret.'
      schema: { type: string }
    Timestamp:
      name: X-Zerahi-Timestamp
      in: header
      required: true
      description: Unix seconds; must be within the replay window.
      schema: { type: string }
    IntegrationId:
      name: X-Zerahi-Integration-Id
      in: header
      required: true
      schema: { type: string, format: uuid }
  requestBodies:
    TokenOnly:
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Token' }
  responses:
    Ok:
      description: Success
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Envelope' }
    Failed:
      description: Refused; `message` says why in plain language.
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Envelope' }
    Forbidden:
      description: "You don't have permission to do this. Contact your administrator if you think you should have access."
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Envelope' }
    Coded:
      description: '`code` 800.200.001 is success; any other code is a refusal explained in `message`.'
      content:
        application/json:
          schema:
            type: object
            properties:
              code: { type: string, example: '800.200.001' }
              message: { type: string }
              data: { type: object }
  schemas:
    Token:
      type: object
      required: [token]
      properties:
        token: { type: string, description: Session token from sign-in. }
    RequestId:
      type: string
      pattern: '^[A-Za-z0-9_-]{8,64}$'
      description: Idempotency key for this request (e.g. a UUID). Reuse it when retrying the same request.
      example: 6f2c9a1e-3b4d-4e5f-8a9b-0c1d2e3f4a5b
    Envelope:
      type: object
      properties:
        status: { type: string, enum: [success, failed] }
        message: { type: string, example: 'KYC check complete.' }
        data: { type: object }
    QueryFilters:
      type: object
      properties:
        date_from: { type: string, format: date }
        date_to: { type: string, format: date }
        service: { type: string, example: IPRS }
        environment: { type: string, enum: [LIVE, SAMPLE] }
        route: { type: string, enum: [PLATFORM_ROUTED, DIRECT_SPIN] }
        credentials: { type: string, enum: [PLATFORM, OWN] }
        status: { type: string, enum: [SUCCESSFUL, FAILED, NOT_FOUND, SUBMITTED] }
        billable: { type: string, enum: [BILLABLE, NON_BILLABLE, PENDING_REVIEW, UNPRICED, UNRATED] }
        search: { type: string, description: Customer name or phone, query reference, reference or masked identifier. }
        page: { type: integer, default: 1 }
        page_size: { type: integer, default: 25, maximum: 100 }
    QueryRow:
      type: object
      properties:
        query_reference: { type: string, example: Q-3F9A0C12B7D4E1 }
        at: { type: string, format: date-time }
        customer: { type: string, example: Amina Odhiambo, description: The customer the query was about. }
        identifier: { type: string, example: 'National ID number ••••5678', description: Masked. }
        service_name: { type: string, example: Metropol credit report }
        environment: { type: string, enum: [LIVE, SAMPLE] }
        route: { type: string, enum: [PLATFORM_ROUTED, DIRECT_SPIN] }
        credential_source: { type: string, enum: [PLATFORM, OWN, ''] }
        status: { type: string, example: FOUND }
        billable_status: { type: string, enum: [BILLABLE, NON_BILLABLE, PENDING_REVIEW, UNRATED] }
        reason: { type: string }
        unit_price: { type: string, example: '50.00' }
        vat_rate: { type: string, example: '16.00' }
        vat_amount: { type: string, example: '8.00' }
        total_amount: { type: string, example: '58.00' }
        currency: { type: string, example: KES }
        charge_reference: { type: string, example: CHG-Q-3F9A0C12B7D4E1 }
