{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/spicyapi/json-schema/spicyapi-balance-envelope-schema.json",
  "title": "BalanceEnvelope",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/spicyapi-account-api-openapi.yml#/components/schemas/BalanceEnvelope",
  "allOf": [
    {
      "$ref": "#/$defs/EnvelopeBase"
    },
    {
      "type": "object",
      "required": [
        "data"
      ],
      "properties": {
        "code": {
          "type": "integer",
          "const": 200
        },
        "msg": {
          "type": "string",
          "const": "success"
        },
        "data": {
          "$ref": "#/$defs/Balance"
        }
      }
    }
  ],
  "$defs": {
    "Balance": {
      "type": "object",
      "required": [
        "available",
        "held",
        "total"
      ],
      "properties": {
        "funding": {
          "$ref": "#/$defs/FundingOverview"
        },
        "available": {
          "$ref": "#/$defs/USDString"
        },
        "held": {
          "$ref": "#/$defs/USDString"
        },
        "total": {
          "$ref": "#/$defs/USDString"
        }
      }
    },
    "CreditFacilityItem": {
      "type": "object",
      "required": [
        "enabled",
        "limitUsd",
        "availableUsd",
        "usedUsd",
        "heldUsd",
        "status",
        "version"
      ],
      "properties": {
        "enabled": {
          "type": "boolean"
        },
        "limitUsd": {
          "type": "string",
          "description": "Approved ceiling",
          "not wallet funds.": null
        },
        "availableUsd": {
          "type": "string"
        },
        "usedUsd": {
          "type": "string",
          "description": "Outstanding settled principal."
        },
        "heldUsd": {
          "type": "string",
          "description": "Credit reserved for accepted tasks."
        },
        "expiresAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "status": {
          "type": "string"
        },
        "version": {
          "type": "integer",
          "format": "int64"
        }
      }
    },
    "EnvelopeBase": {
      "type": "object",
      "required": [
        "code",
        "msg",
        "request_id"
      ],
      "properties": {
        "code": {
          "type": "integer"
        },
        "msg": {
          "type": "string"
        },
        "request_id": {
          "type": "string",
          "description": "Correlation identifier. Include it in support requests."
        }
      }
    },
    "FundingGrantItem": {
      "type": "object",
      "required": [
        "id",
        "name",
        "amountUsd",
        "availableUsd",
        "heldUsd",
        "spentUsd",
        "status",
        "startsAt",
        "modelSlugs",
        "customerMemo",
        "createdAt"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "amountUsd": {
          "type": "string"
        },
        "availableUsd": {
          "type": "string"
        },
        "heldUsd": {
          "type": "string"
        },
        "spentUsd": {
          "type": "string"
        },
        "status": {
          "type": "string"
        },
        "startsAt": {
          "type": "string",
          "format": "date-time"
        },
        "expiresAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "modelSlugs": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Empty means all models."
        },
        "customerMemo": {
          "type": "string"
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "FundingOverview": {
      "type": "object",
      "required": [
        "balanceUsd",
        "heldUsd",
        "prepaidAvailableUsd",
        "grantAvailableUsd",
        "cashShortfallUsd",
        "credit",
        "grants",
        "grantsHasMore"
      ],
      "properties": {
        "balanceUsd": {
          "type": "string",
          "description": "Net wallet available balance; may be negative for approved credit consumption or external payment recovery."
        },
        "heldUsd": {
          "type": "string"
        },
        "prepaidAvailableUsd": {
          "type": "string",
          "description": "Unrestricted prepaid or legacy available funds."
        },
        "grantAvailableUsd": {
          "type": "string",
          "description": "Active available grants; model applicability is checked at task admission."
        },
        "cashShortfallUsd": {
          "type": "string",
          "description": "External payment recovery shortfall that cannot be covered by grants or credit."
        },
        "credit": {
          "$ref": "#/$defs/CreditFacilityItem"
        },
        "grants": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/FundingGrantItem"
          }
        },
        "grantsHasMore": {
          "type": "boolean"
        }
      }
    },
    "USDString": {
      "type": "string",
      "pattern": "^-?[0-9]+(?:\\.[0-9]+)?$",
      "description": "Decimal USD amount encoded as a string to avoid binary floating-point loss."
    }
  }
}