Skip to content

AsterPay/fastapi-api-monetization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI API Monetization Starter

A complete template for monetizing your Python API with crypto payments via AsterPay.

Features

  • 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

Quick Start

1. Clone and Install

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.txt

2. Set Environment Variables

cp .env.example .env

Edit .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:8000

3. Run Server

uvicorn main:app --reload

Open http://localhost:8000/docs for interactive API documentation.

API Endpoints

GET / - API Info

Returns API information and pricing.

GET /credits - Check Balance

curl -H "X-API-Key: your_api_key" http://localhost:8000/credits

POST /generate - Generate Text (1 credit)

curl -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"}'

POST /buy-credits - Purchase Credits

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.

POST /webhook/asterpay - Payment Webhook

Automatically receives payment confirmations from AsterPay.

Pricing Structure

Credits Price Per Request
100 $5 $0.05
500 $20 $0.04
1000 $35 $0.035

Project Structure

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

How It Works

1. User Gets API Key

User signs up and receives an API key with free starter credits.

2. User Makes API Calls

Each /generate request costs 1 credit:

response = requests.post(
    "https://your-api.com/generate",
    headers={"X-API-Key": "user_key"},
    json={"prompt": "Hello world"}
)

3. Credits Run Out

API returns 402 error with purchase link:

{
  "error": "insufficient_credits",
  "message": "Not enough credits",
  "buy_credits_url": "/buy-credits"
}

4. User Buys Credits

User calls /buy-credits, gets checkout URL, pays with crypto.

5. Webhook Delivers Credits

AsterPay sends webhook → credits added automatically.

Customization

Add Your Own Endpoints

@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}

Use Database for Production

Replace in-memory storage in auth.py with:

  • PostgreSQL
  • MongoDB
  • Redis
  • SQLite

Change Pricing

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
}

Deployment

Deploy to Render

  1. Create new Web Service
  2. Connect GitHub repo
  3. Set environment variables
  4. Deploy

Deploy to Railway

railway login
railway init
railway up

Deploy to Fly.io

fly launch
fly secrets set ASTERPAY_API_KEY=sk_live_...
fly deploy

Support

License

MIT

About

Python FastAPI API monetization with AsterPay crypto payments

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages