How to Integrate CrewAI for payments with FastAPI for AI agents
Combining CrewAI for payments with FastAPI gives you a clean way to expose payment-aware agent workflows over HTTP. You get the orchestration layer from CrewAI and the API layer from FastAPI, which is useful when an agent needs to create invoices, confirm payment status, or trigger payment-related actions from another service.
Prerequisites
- •Python 3.10+
- •
fastapi - •
uvicorn - •
crewai - •A CrewAI project configured for your payment workflow
- •Access credentials or API keys for the payment backend your CrewAI agent uses
- •Basic familiarity with async Python and REST APIs
Install the core packages:
pip install fastapi uvicorn crewai
Integration Steps
- •
Create a FastAPI app that will host your agent endpoints
Start with a minimal API server. This gives you one place to receive requests and forward them into your CrewAI payment workflow.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI(title="CrewAI Payments API") class PaymentRequest(BaseModel): customer_id: str amount: float currency: str = "USD" description: str @app.get("/health") def health(): return {"status": "ok"} - •
Define your CrewAI agents and task for payment handling
In CrewAI, the usual pattern is to create an
Agent, aTask, and aCrew. For payments, keep the agent focused on one job: validate the request, prepare the payment action, and return structured output.from crewai import Agent, Task, Crew, Process payment_agent = Agent( role="Payment Operations Agent", goal="Prepare and validate payment instructions for downstream execution", backstory="You handle payment requests safely and return structured results.", verbose=True, allow_delegation=False, ) payment_task = Task( description=( "Process a payment request for customer_id {customer_id}, " "amount {amount}, currency {currency}, description {description}. " "Return JSON with status, reference_id, and notes." ), expected_output="A JSON object with payment status and reference details.", agent=payment_agent, ) crew = Crew( agents=[payment_agent], tasks=[payment_task], process=Process.sequential, verbose=True, ) - •
Expose a POST endpoint that runs the CrewAI workflow
This is where FastAPI and CrewAI meet. The endpoint receives the request body, passes it into
crew.kickoff(inputs=...), then returns the result to the caller.@app.post("/payments/process") def process_payment(req: PaymentRequest): result = crew.kickoff( inputs={ "customer_id": req.customer_id, "amount": req.amount, "currency": req.currency, "description": req.description, } ) return { "success": True, "crew_output": str(result), } - •
Add a real integration layer for execution control
In production, don’t let the agent directly “decide” money movement without guardrails. Use FastAPI to enforce validation before calling CrewAI, then map the agent output to your actual payment service.
from fastapi import HTTPException def validate_payment_request(req: PaymentRequest): if req.amount <= 0: raise HTTPException(status_code=400, detail="Amount must be greater than zero") if len(req.customer_id) < 3: raise HTTPException(status_code=400, detail="Invalid customer_id") @app.post("/payments/authorize") def authorize_payment(req: PaymentRequest): validate_payment_request(req) crew_result = crew.kickoff( inputs={ "customer_id": req.customer_id, "amount": req.amount, "currency": req.currency, "description": req.description, } ) # Replace this with your actual PSP/bank transfer call. return { "status": "authorized", "agent_result": str(crew_result), } - •
Run the API server
Use Uvicorn to serve the FastAPI app.
uvicorn main:app --reload --host 0.0.0.0 --port 8000
Testing the Integration
Send a request to the endpoint with curl or Postman. This verifies that FastAPI receives input and that CrewAI executes inside the request path.
curl -X POST "http://127.0.0.1:8000/payments/process" \
-H "Content-Type: application/json" \
-d '{
"customer_id": "cust_12345",
"amount": 125.50,
"currency": "USD",
"description": "Invoice #INV-1001"
}'
Expected output:
{
"success": true,
"crew_output": "{\"status\":\"approved\",\"reference_id\":\"pay_9f2a1c\",\"notes\":\"Payment request validated\"}"
}
If you want a quick in-process test, use FastAPI’s TestClient:
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_process_payment():
response = client.post(
"/payments/process",
json={
"customer_id": "cust_12345",
"amount": 125.50,
"currency": "USD",
"description": "Invoice #INV-1001",
},
)
assert response.status_code == 200
assert response.json()["success"] is True
Real-World Use Cases
- •Payment approval assistants that review incoming invoices and route only valid ones into your PSP or banking rail.
- •Collections workflows where an AI agent checks overdue accounts, generates reminders, and triggers authorized payment links through an API.
- •Back-office reconciliation services that compare ledger entries against agent-generated payment events and flag mismatches for review.
The important part is not just wiring two libraries together. It’s using FastAPI as the control plane and CrewAI as the orchestration layer so you can keep payment logic observable, testable, and safe enough for production systems.
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