Build a Stripe Payment Agent with ASI:One and uAgents
This guide demonstrates how to create an intelligent Stripe payment agent using ASI:One for natural language processing and the official Stripe API, deployed via uAgents.
Overview
The Stripe ASI:One Agent provides:
- AI-powered payment link creation with natural-language requests
- Direct Stripe API integration (products, prices, payment links)
- ASI:One reasoning for natural language understanding
- uAgents deployment for 24/7 availability
Prerequisites
Before you begin, ensure you have:
- Python 3.11+ installed
- Stripe account with API keys (Get sandbox keys)
- ASI:One API key (Get API key)
Installation
1. Create Project Directory
mkdir stripe_asi_agent
cd stripe_asi_agent
2. Set Up Virtual Environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
3. Install Dependencies
Create a requirements.txt file:
# Core uAgents
uagents==0.22.5
# Stripe
stripe==12.2.0
# Environment and utilities
python-dotenv>=1.0.0
requests>=2.31.0
Install the packages:
pip install -r requirements.txt
Environment Setup
1. Create Environment File
Create a .env file in your project directory:
# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key_here
# ASI:One Configuration
ASI_API_KEY=your_asi_api_key_here
2. Get Your API Keys
Stripe Keys:
- Go to Stripe Dashboard
- Copy your Secret key (starts with
sk_test_)
ASI:One Key:
- Go to ASI:One Platform
- Create a new API key
Create the Agent
Create stripe_llm_agent.py:
#!/usr/bin/env python3
"""Stripe Payment-Link Agent using ASI:One function-calling and uAgents.
Send any message like:
"Create a payment link for a $1200 laptop"
The LLM extracts {product, amount} via structured output, we call
Stripe to create Product, Price, and PaymentLink, then return the URL.
"""
import os, json, stripe
from uuid import uuid4
from datetime import datetime, timezone
from dotenv import load_dotenv
import requests
from uagents import Agent, Context, Protocol
from uagents_core.contrib.protocols.chat import (
ChatAcknowledgement,
ChatMessage,
TextContent,
chat_protocol_spec,
)
# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
load_dotenv()
stripe.api_key = os.getenv("STRIPE_SECRET_KEY")
ASI_API_KEY = os.getenv("ASI_API_KEY")
if not stripe.api_key or not ASI_API_KEY:
raise RuntimeError("STRIPE_SECRET_KEY and ASI_API_KEY must be set in environment")
# ASI:One API settings
BASE_URL = "https://api.asi1.ai/v1"
headers = {
"Authorization": f"Bearer {ASI_API_KEY}",
"Content-Type": "application/json"
}
# ---------------------------------------------------------------------------
# ASI:One tool / function definition
# ---------------------------------------------------------------------------
tool_def = {
"type": "function",
"function": {
"name": "create_payment_link",
"description": "Create a one-off Stripe payment link",
"parameters": {
"type": "object",
"properties": {
"product": {
"type": "string",
"description": "Product or service name"
},
"amount": {
"type": "number",
"description": "Price as a decimal number"
},
"currency": {
"type": "string",
"description": "ISO currency code (e.g. usd, inr, eur). Default usd",
"enum": ["usd", "inr", "eur", "gbp", "aud", "cad"]
}
},
"required": ["product", "amount"],
"additionalProperties": False
},
"strict": True
}
}
def is_payment_related(text: str) -> bool:
"""Check if the message is related to creating a payment link."""
prompt = f"""Analyze if the following message is about creating a payment link or processing a payment.
Return only 'true' if it's payment-related, 'false' otherwise.
Message: {text}
Examples of payment-related messages:
- "Create a payment link for a $100 product"
- "I want to sell my laptop for $500"
- "Generate a payment link for consulting services"
Examples of non-payment messages:
- "Hello, how are you?"
- "What's the weather like?"
- "Tell me a joke"
"""
payload = {
"model": "asi1",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0
}
response = requests.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=payload
)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"].strip().lower() == 'true'
def parse_with_llm(user_text: str):
"""Returns dict with product & amount extracted by LLM."""
payload = {
"model": "asi1",
"messages": [{"role": "user", "content": user_text}],
"tools": [tool_def],
"tool_choice": {
"type": "function",
"function": {"name": "create_payment_link"}
},
"temperature": 0
}
response = requests.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=payload
)
response.raise_for_status()
response_json = response.json()
# Handle tool calls from ASI:One response
tool_calls = response_json["choices"][0]["message"].get("tool_calls") or []
if not tool_calls:
raise ValueError("No payment details found in the message")
tool_call = tool_calls[0]
return json.loads(tool_call["function"]["arguments"])
# ---------------------------------------------------------------------------
# Stripe helper
# ---------------------------------------------------------------------------
def create_payment_link(product: str, amount_usd: float, currency: str):
prod = stripe.Product.create(name=product)
price = stripe.Price.create(unit_amount=int(amount_usd * 100), currency=currency, product=prod.id)
link = stripe.PaymentLink.create(line_items=[{"price": price.id, "quantity": 1}])
return link
# ---------------------------------------------------------------------------
# uAgents setup
# ---------------------------------------------------------------------------
chat_proto = Protocol(spec=chat_protocol_spec)
@chat_proto.on_message(ChatMessage)
async def handler(ctx: Context, sender: str, msg: ChatMessage):
# ack
await ctx.send(sender, ChatAcknowledgement(timestamp=datetime.now(timezone.utc), acknowledged_msg_id=msg.msg_id))
text = " ".join(c.text for c in msg.content if hasattr(c, "text")).strip()
if not text:
return
try:
# First check if the message is payment-related
if not is_payment_related(text):
reply = (
"