How to Integrate Next.js for lending with Vercel AI SDK for startups
Combining Next.js for lending with Vercel AI SDK gives you a practical path to ship lending workflows that feel conversational without turning your backend into a mess. The pattern is simple: use Next.js for the lending app and orchestration, then let Vercel AI SDK handle the agent layer for summarization, classification, and borrower-facing responses.
Prerequisites
- •Python 3.10+
- •Node.js 18+ for the Next.js app
- •A working Next.js lending project with API routes or server actions
- •A Vercel project with AI SDK installed
- •API keys for your model provider, stored in environment variables
- •A lending backend or sandbox API that exposes application, underwriting, or document endpoints
- •
requestsandpydanticinstalled in your Python environment
Integration Steps
- •Define the lending payload you want the agent to understand
Start by normalizing loan applications into a strict schema. This keeps the agent from guessing and makes downstream decisions deterministic.
from pydantic import BaseModel, EmailStr, Field
from typing import Literal
class LoanApplication(BaseModel):
applicant_name: str
email: EmailStr
loan_amount: float = Field(gt=0)
annual_income: float = Field(gt=0)
credit_score: int = Field(ge=300, le=850)
purpose: Literal["working_capital", "equipment", "inventory", "refinance"]
This model becomes the contract between your Next.js frontend, your Python service, and any agent logic you run behind it.
- •Expose a Python service that forwards lending data to Next.js
If your Next.js app owns the lending workflow, use a small Python integration service to post normalized applications into a Next.js route handler. In practice, this is where you trigger application creation or status updates.
import os
import requests
from loan_models import LoanApplication
NEXTJS_API_BASE = os.getenv("NEXTJS_API_BASE", "http://localhost:3000")
def submit_application(app: LoanApplication) -> dict:
url = f"{NEXTJS_API_BASE}/api/lending/applications"
payload = app.model_dump()
response = requests.post(url, json=payload, timeout=30)
response.raise_for_status()
return response.json()
application = LoanApplication(
applicant_name="Amina Patel",
email="amina@example.com",
loan_amount=50000,
annual_income=180000,
credit_score=742,
purpose="equipment",
)
result = submit_application(application)
print(result)
On the Next.js side, this usually maps to an API route like app/api/lending/applications/route.ts. Your Python code only needs a stable endpoint and predictable response shape.
- •Call Vercel AI SDK from Python through an agent gateway
Vercel AI SDK is JavaScript-first, so don’t try to pretend it has a native Python client. The clean production pattern is to wrap it in a thin Next.js endpoint and call that endpoint from Python.
import os
import requests
VERCEL_AGENT_URL = os.getenv("VERCEL_AGENT_URL", "http://localhost:3000/api/agent/loan-summary")
def generate_loan_summary(application_id: str) -> dict:
payload = {"applicationId": application_id}
response = requests.post(VERCEL_AGENT_URL, json=payload, timeout=30)
response.raise_for_status()
return response.json()
summary = generate_loan_summary("app_12345")
print(summary["message"])
That endpoint can internally use Vercel AI SDK’s streamText, generateText, or tool-calling APIs. Your Python layer stays focused on integration instead of model plumbing.
- •Use the agent output to drive lending workflow decisions
Once the AI SDK returns structured output, feed it back into your lending system as metadata or action suggestions. Keep humans in the loop for anything that affects approval.
import os
import requests
LENDING_WORKFLOW_URL = os.getenv("LENDING_WORKFLOW_URL", "http://localhost:4000")
def update_workflow(application_id: str, ai_decision: dict) -> dict:
url = f"{LENDING_WORKFLOW_URL}/applications/{application_id}/ai-review"
response = requests.patch(url, json={
"risk_level": ai_decision["risk_level"],
"missing_docs": ai_decision.get("missing_docs", []),
"recommended_action": ai_decision["recommended_action"],
"reasoning": ai_decision["reasoning"],
}, timeout=30)
response.raise_for_status()
return response.json()
A good agent response schema looks like this:
- •
risk_level: low / medium / high - •
recommended_action: approve / manual_review / request_docs / reject - •
missing_docs: list of strings - •
reasoning: short explanation for ops teams
- •Stream borrower-facing explanations through the Next.js UI
Use Vercel AI SDK inside Next.js to stream plain-language explanations back to applicants while your Python service handles orchestration. This keeps latency low and UX responsive.
import requests
def get_borrower_explanation(application_id: str) -> str:
response = requests.get(
f"http://localhost:3000/api/agent/explain/{application_id}",
timeout=30,
stream=False,
)
response.raise_for_status()
return response.json()["explanation"]
In production, this is where you’d connect the streamed text from streamText() into a chat panel or status page in your lending portal.
Testing the Integration
Run a smoke test against both services with one application payload and verify you get a workflow update plus an explanation.
from loan_models import LoanApplication
app = LoanApplication(
applicant_name="Jordan Lee",
email="jordan@example.com",
loan_amount=25000,
annual_income=95000,
credit_score=715,
purpose="inventory",
)
created = submit_application(app)
app_id = created["id"]
summary = generate_loan_summary(app_id)
workflow_update = update_workflow(app_id, summary)
print("APPLICATION:", created["status"])
print("AI ACTION:", workflow_update["recommended_action"])
print("EXPLANATION:", summary["message"])
Expected output:
APPLICATION: submitted
AI ACTION: manual_review
EXPLANATION: Applicant meets baseline income criteria but document verification is incomplete.
Real-World Use Cases
- •Pre-screening assistant that checks income, credit score bands, and missing documents before an underwriter sees the file.
- •Borrower support copilot that answers status questions like “Why is my application pending?” using real workflow state.
- •Document triage pipeline that classifies uploaded files into pay stubs, bank statements, tax returns, and IDs before routing them downstream.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit