{
  "openapi": "3.1.0",
  "info": {
    "title": "Lendtrain Chat API",
    "description": "Conversational mortgage refinance advisor powered by Claude. Send messages, receive expert mortgage guidance with real-time pricing, itemized closing costs, and compliance enforcement. This is the public API for AI agents and web applications. Operated by Atlantic Home Mortgage, LLC dba Lendtrain (NMLS# 1844873). Licensed in AL, FL, GA, KY, NC, OR, SC, TN, TX, UT. Lendtrain also publishes 34 SEO refinance landing pages at /refinance/{slug}, including 10 state guides, 11 Utah cities, 10 Utah counties, and 3 loan-type pages (cash-out, VA IRRRL, jumbo).",
    "version": "2.0.0",
    "contact": {
      "name": "Tony Davis",
      "email": "team@lendtrain.com",
      "url": "https://www.lendtrain.com"
    }
  },
  "servers": [
    {
      "url": "https://www.lendtrain.com",
      "description": "Production"
    }
  ],
  "paths": {
    "/api/chat": {
      "post": {
        "operationId": "chat",
        "summary": "Send a message to the Lendtrain mortgage advisor",
        "description": "Conversational endpoint for mortgage refinance guidance. The advisor collects loan details, calculates real-time pricing (via server-side MCP tools), provides savings analysis with itemized closing costs, and generates a recommendation score. Supports multi-turn conversations.\n\nThe response is streamed as Server-Sent Events because the pricing tool loop can run 30-90 seconds for multi-product comparisons. Each event is `data: {\"text\": \"chunk\"}` and the stream ends with `data: [DONE]`. Clients should accumulate the text deltas to reconstruct the full assistant message.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatRequest"
              },
              "example": {
                "messages": [
                  {
                    "role": "user",
                    "content": "I want to check refinance rates for my home in Georgia"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Streamed assistant response as Server-Sent Events. Concatenate the text fields from each data event in order; the stream terminates with `data: [DONE]`.",
            "content": {
              "text/event-stream": {
                "schema": {
                  "$ref": "#/components/schemas/ChatStreamEvent"
                },
                "example": "data: {\"text\":\"Hello, I am the Lendtrain AI Mortgage Advisor.\"}\n\ndata: {\"text\":\" NMLS# 1844873.\"}\n\ndata: [DONE]\n\n"
              }
            }
          },
          "400": {
            "description": "Invalid request (missing or malformed messages array)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden (origin not allowed)"
          },
          "413": {
            "description": "Request body too large (max 100KB)"
          },
          "429": {
            "description": "Rate limited"
          },
          "502": {
            "description": "AI service error"
          },
          "504": {
            "description": "Request timed out"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ChatRequest": {
        "type": "object",
        "required": [
          "messages"
        ],
        "properties": {
          "messages": {
            "type": "array",
            "minItems": 1,
            "maxItems": 50,
            "items": {
              "$ref": "#/components/schemas/Message"
            }
          }
        }
      },
      "Message": {
        "type": "object",
        "required": [
          "role",
          "content"
        ],
        "properties": {
          "role": {
            "type": "string",
            "enum": [
              "user",
              "assistant"
            ]
          },
          "content": {
            "type": "string",
            "minLength": 1,
            "maxLength": 10000
          }
        }
      },
      "ChatResponse": {
        "type": "object",
        "properties": {
          "role": {
            "type": "string",
            "enum": [
              "assistant"
            ]
          },
          "content": {
            "type": "string"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        }
      },
      "ChatStreamEvent": {
        "type": "object",
        "description": "A single SSE event payload emitted by /api/chat. Each event line in the stream is either `data: {\"text\":\"<delta>\"}` or the literal terminator `data: [DONE]`.",
        "properties": {
          "text": {
            "type": "string",
            "description": "An incremental text delta. Concatenate all text fields in order to reconstruct the full assistant message."
          }
        }
      }
    }
  }
}
