A complete template for monetizing your Python API with crypto payments via AsterPay.
- FastAPI - Modern, fast Python web framework
- API Key Auth - Simple API key authentication
- Credit System - Per-request billing
- AsterPay Integration - Accept USDC/USDT payments
- Webhook Handling - Automatic credit delivery
- OpenAI Example - AI text generation endpoint
git clone https://github.com/AsterPay/fastapi-api-monetization.git
cd fastapi-api-monetization
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtcp .env.example .envEdit .env:
# AsterPay
ASTERPAY_API_KEY=sk_live_your_api_key
ASTERPAY_WEBHOOK_SECRET=whsec_your_webhook_secret
# OpenAI (optional)
OPENAI_API_KEY=sk-your-openai-key
# App
APP_URL=http://localhost:8000uvicorn main:app --reloadOpen http://localhost:8000/docs for interactive API documentation.
Returns API information and pricing.
curl -H "X-API-Key: your_api_key" http://localhost:8000/creditscurl -X POST http://localhost:8000/generate \
-H "X-API-Key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"prompt": "Write a haiku about coding"}'curl -X POST http://localhost:8000/buy-credits \
-H "X-API-Key: your_api_key" \
-H "Content-Type: application/json" \
-d '{"amount": 100}'Returns checkout URL for crypto payment.
Automatically receives payment confirmations from AsterPay.
| Credits | Price | Per Request |
|---|---|---|
| 100 | $5 | $0.05 |
| 500 | $20 | $0.04 |
| 1000 | $35 | $0.035 |
fastapi-api-monetization/
├── main.py # FastAPI app & endpoints
├── auth.py # API key auth & credit management
├── payments.py # AsterPay integration
├── ai_service.py # AI text generation
├── requirements.txt # Python dependencies
├── .env.example # Environment template
└── README.md
User signs up and receives an API key with free starter credits.
Each /generate request costs 1 credit:
response = requests.post(
"https://your-api.com/generate",
headers={"X-API-Key": "user_key"},
json={"prompt": "Hello world"}
)API returns 402 error with purchase link:
{
"error": "insufficient_credits",
"message": "Not enough credits",
"buy_credits_url": "/buy-credits"
}User calls /buy-credits, gets checkout URL, pays with crypto.
AsterPay sends webhook → credits added automatically.
@app.post("/your-feature")
async def your_feature(
request: YourRequest,
api_key: str = Depends(verify_api_key)
):
# Check credits
credits = get_user_credits(api_key)
if credits < 2: # This feature costs 2 credits
raise HTTPException(status_code=402, detail="Insufficient credits")
# Your logic here
result = do_something(request)
# Deduct credits
deduct_credits(api_key, 2)
return {"result": result}Replace in-memory storage in auth.py with:
- PostgreSQL
- MongoDB
- Redis
- SQLite
Edit the pricing dict in main.py:
pricing = {
50: "3.00", # 50 credits = $3
200: "10.00", # 200 credits = $10
500: "20.00", # 500 credits = $20
}- Create new Web Service
- Connect GitHub repo
- Set environment variables
- Deploy
railway login
railway init
railway upfly launch
fly secrets set ASTERPAY_API_KEY=sk_live_...
fly deploy- Documentation: asterpay.io/docs
- Email: support@asterpay.io
- Telegram: t.me/asterpaycommunity
MIT