{
  "openapi": "3.1.0",
  "info": {
    "title": "Bank of Clawd Agent API",
    "description": "Financial infrastructure for AI agents. Programmatic access to credit scores, loans, and repayments.",
    "version": "1.0.0",
    "contact": {
      "name": "Bank of Clawd",
      "url": "https://bankofclawd.com"
    }
  },
  "servers": [
    {
      "url": "https://bankofclawd.com/api/v1",
      "description": "Production"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "paths": {
    "/agent/auth/challenge": {
      "post": {
        "operationId": "getAuthChallenge",
        "summary": "Request authentication challenge",
        "description": "Get a challenge message to sign with your wallet for authentication",
        "tags": ["Authentication"],
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["wallet"],
                "properties": {
                  "wallet": {
                    "type": "string",
                    "description": "Solana wallet address (base58)"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Challenge generated",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "challenge": {
                      "type": "string",
                      "description": "Message to sign"
                    },
                    "expiresIn": {
                      "type": "integer",
                      "description": "Seconds until challenge expires"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/agent/auth/verify": {
      "post": {
        "operationId": "verifySignature",
        "summary": "Verify signature and get token",
        "description": "Submit signed challenge to receive JWT authentication token",
        "tags": ["Authentication"],
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["wallet", "signature", "message"],
                "properties": {
                  "wallet": {
                    "type": "string",
                    "description": "Solana wallet address"
                  },
                  "signature": {
                    "type": "string",
                    "description": "Base58 encoded signature"
                  },
                  "message": {
                    "type": "string",
                    "description": "The challenge message that was signed"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Authentication successful",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "token": {
                      "type": "string",
                      "description": "JWT token for API access"
                    },
                    "expiresIn": {
                      "type": "integer",
                      "description": "Token validity in seconds"
                    },
                    "wallet": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/agent/profile": {
      "get": {
        "operationId": "getAgentProfile",
        "summary": "Get credit profile",
        "description": "Retrieve your agent's credit score, risk category, and on-chain data analysis",
        "tags": ["Profile"],
        "responses": {
          "200": {
            "description": "Profile data",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AgentProfile"
                }
              }
            }
          }
        }
      }
    },
    "/agent/eligibility": {
      "get": {
        "operationId": "checkEligibility",
        "summary": "Check loan eligibility",
        "description": "Check if your agent is eligible for a loan and get suggested terms",
        "tags": ["Loans"],
        "parameters": [
          {
            "name": "amount",
            "in": "query",
            "description": "Requested loan amount in USD",
            "schema": {
              "type": "number"
            }
          },
          {
            "name": "purpose",
            "in": "query",
            "description": "Loan purpose",
            "schema": {
              "type": "string",
              "enum": ["task", "working_capital", "liquidity_provision", "market_neutral", "directional_trading", "speculative"]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Eligibility check result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EligibilityResult"
                }
              }
            }
          }
        }
      }
    },
    "/agent/loans": {
      "get": {
        "operationId": "listLoans",
        "summary": "List all loans",
        "description": "Get all loans for your agent",
        "tags": ["Loans"],
        "responses": {
          "200": {
            "description": "Loan list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "loans": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Loan"
                      }
                    },
                    "summary": {
                      "type": "object",
                      "properties": {
                        "totalLoans": { "type": "integer" },
                        "totalActive": { "type": "integer" },
                        "totalOutstanding": { "type": "number" }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "applyForLoan",
        "summary": "Apply for a loan",
        "description": "Submit a new loan application",
        "tags": ["Loans"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LoanApplication"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Application result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LoanApplicationResult"
                }
              }
            }
          }
        }
      }
    },
    "/agent/loans/{id}": {
      "get": {
        "operationId": "getLoan",
        "summary": "Get loan details",
        "description": "Get details of a specific loan",
        "tags": ["Loans"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Loan details",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Loan"
                }
              }
            }
          }
        }
      }
    },
    "/agent/loans/{id}/repay": {
      "post": {
        "operationId": "repayLoan",
        "summary": "Make a repayment",
        "description": "Make a payment towards a loan",
        "tags": ["Loans"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["amount"],
                "properties": {
                  "amount": {
                    "type": "number",
                    "description": "Payment amount in USD"
                  },
                  "txSignature": {
                    "type": "string",
                    "description": "Solana transaction signature (optional)"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Payment result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PaymentResult"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {
      "AgentProfile": {
        "type": "object",
        "properties": {
          "wallet": { "type": "string" },
          "creditScore": {
            "type": "object",
            "properties": {
              "score": { "type": "integer", "minimum": 0, "maximum": 1000 },
              "pdBand": { "type": "string" },
              "riskCategory": { "type": "string", "enum": ["Low", "Medium", "Elevated", "High"] },
              "htm": { "type": "number" },
              "creditLimit": { "type": "number" },
              "suggestedAPR": { "type": "number" },
              "decision": { "type": "string", "enum": ["APPROVE", "APPROVE_WITH_CONDITIONS", "DENY"] }
            }
          },
          "onChainData": {
            "type": "object",
            "properties": {
              "solBalance": { "type": "number" },
              "totalTransactions": { "type": "integer" },
              "accountAgeDays": { "type": "integer" }
            }
          }
        }
      },
      "EligibilityResult": {
        "type": "object",
        "properties": {
          "eligible": { "type": "boolean" },
          "decision": { "type": "string" },
          "maxAmount": { "type": "number" },
          "terms": {
            "type": "object",
            "properties": {
              "apr": { "type": "number" },
              "maxLTV": { "type": "number" },
              "maxDuration": { "type": "integer" }
            }
          },
          "requirements": {
            "type": "array",
            "items": { "type": "string" }
          }
        }
      },
      "LoanApplication": {
        "type": "object",
        "required": ["amount", "purpose"],
        "properties": {
          "amount": { "type": "number", "description": "Loan amount in USD" },
          "purpose": {
            "type": "string",
            "enum": ["task", "working_capital", "liquidity_provision", "market_neutral", "directional_trading", "speculative"]
          },
          "duration": { "type": "integer", "description": "Loan duration in days" },
          "collateral": {
            "type": "object",
            "properties": {
              "type": { "type": "string" },
              "amount": { "type": "number" }
            }
          }
        }
      },
      "LoanApplicationResult": {
        "type": "object",
        "properties": {
          "application": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "status": { "type": "string", "enum": ["approved", "pending_review", "denied"] },
              "amount": { "type": "number" },
              "apr": { "type": "number" },
              "terms": { "type": "object" }
            }
          }
        }
      },
      "Loan": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "status": { "type": "string" },
          "principal": { "type": "number" },
          "outstanding": { "type": "number" },
          "apr": { "type": "number" },
          "purpose": { "type": "string" },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "PaymentResult": {
        "type": "object",
        "properties": {
          "payment": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "amount": { "type": "number" },
              "remainingBalance": { "type": "number" },
              "status": { "type": "string" }
            }
          }
        }
      }
    }
  }
}
