Pix Payment Agent
The simplest agent you can build with CodeSpar. Create a Pix charge, generate a QR code, and send it to the customer via WhatsApp — in ~50 lines of code.
Create a Pix charge, generate the QR code, and send it to the customer via WhatsApp. The simplest agent you can build with CodeSpar — two steps, one conversation.
Conversation preview
What the agent does when you call pixAgent("+5511...", 15000, "Pro Plan").
Prerequisites
Install the SDK and the OpenAI provider adapter:
npm install @codespar/sdk @codespar/openai openaiSet your environment variables:
CODESPAR_API_KEY=csk_live_...
OPENAI_API_KEY=sk-...You'll need active accounts on Asaas and Twilio. CodeSpar will prompt you to authenticate both on first run.
Full agent code
import OpenAI from "openai";
import { CodeSpar } from "@codespar/sdk";
import { getTools, handleToolCall } from "@codespar/openai";
const openai = new OpenAI();
const codespar = new CodeSpar({ apiKey: process.env.CODESPAR_API_KEY });
async function pixAgent(phone: string, amount: number, description: string) {
const session = await codespar.create("user_123", {
servers: ["asaas", "twilio"],
});
try {
const tools = await getTools(session);
const messages = [
{ role: "system", content: "You are a Brazilian payment assistant..." },
{
role: "user",
content: `Create Pix of R$${amount / 100} for "${description}", send to ${phone}`,
},
];
let response = await openai.chat.completions.create({ model: "gpt-4o", tools, messages });
while (response.choices[0].message.tool_calls?.length) {
// ... handle tool calls, feed results back, call LLM again
}
return response.choices[0].message.content;
} finally {
await session.close();
}
}Variations
Add boleto fallback
Pass payment_methods: ["pix", "boleto"] to codespar_pay and let the customer pick at checkout.
Chain an invoice
After payment confirmation, chain codespar_invoice to issue an NF-e automatically.
Swap to Claude
Replace @codespar/openai with @codespar/claude. The agent logic is identical.
Python version
For FastAPI services or async jobs, the same flow in Python — no framework-adapter needed, session.send() runs the agent loop on the backend:
from codespar import CodeSpar
cs = CodeSpar() # reads CODESPAR_API_KEY
def pix_agent(phone: str, amount: int, description: str) -> str:
with cs.create("user_123", servers=["asaas", "twilio"]) as session:
result = session.send(
f'Create a Pix of R${amount / 100:.2f} for "{description}", '
f"send the QR code to {phone} via WhatsApp."
)
return result.messageSwap CodeSpar for AsyncCodeSpar and with for async with to run inside FastAPI. See Quickstart (Python).
Next steps
Last updated on
Cookbooks
End-to-end recipes you can copy into production. Each cookbook is a small working agent that solves one real commerce problem, wired through the CodeSpar SDK.
E-Commerce Checkout
Build an AI-powered checkout flow that handles product discovery, payment, invoicing, and shipping — in a single conversation.