
Security News
Opengrep Adds Apex Support and New Rule Controls in Latest Updates
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Lightning-fast x402 payment integration for FastAPI and modern Python web frameworks
Lightning-fast x402 payment integration for FastAPI and modern Python web frameworks. Accept micropayments with just 3 lines of code!
pip install fast-x402
from fastapi import FastAPI
from fast_x402 import x402_middleware
app = FastAPI()
# Add x402 payments in 3 lines!
app.add_middleware(
x402_middleware,
wallet_address="0xYourWalletAddress",
routes={
"/api/premium": "0.10", # $0.10 per request
"/api/data": "0.05", # $0.05 per request
}
)
@app.get("/api/premium")
async def premium_endpoint():
return {"data": "Premium content!"}
That's it! Your API now accepts micropayments. 🎉
GET /api/premium
X-Payment
headerfrom fast_x402 import X402Provider, X402Config
# Create provider with custom config
provider = X402Provider(X402Config(
wallet_address="0xYourAddress",
chain_id=8453, # Base mainnet
accepted_tokens=["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"], # USDC
))
# Dynamic pricing
@app.get("/api/compute/{size}")
async def compute(size: str):
amount = "0.01" if size == "small" else "0.10"
requirement = provider.create_payment_requirement(amount)
# ... handle payment
from fast_x402 import X402Dependency, get_x402_payment
# Define payment requirement
PaymentRequired = X402Dependency("0.05")
@app.get("/api/protected")
async def protected_endpoint(payment = Depends(PaymentRequired)):
return {
"message": "Payment received!",
"payer": payment.from_address,
"amount": payment.value
}
@app.get("/analytics")
async def analytics():
stats = provider.get_analytics()
return stats.to_dashboard_dict()
# Returns:
# {
# "summary": {
# "total_requests": 1000,
# "total_paid": 750,
# "conversion_rate": "75.00%"
# },
# "revenue": {
# "by_token": {"0xA0b8...": "75.50"},
# "by_endpoint": {"/api/premium": {"0xA0b8...": "45.00"}}
# },
# "top_payers": [...]
# }
async def on_payment(payment_data):
print(f"Payment received from {payment_data.from_address}")
# Log to database, send notifications, etc.
app.add_middleware(
x402_middleware,
wallet_address="0xYourAddress",
routes={"/api/*": "0.01"},
on_payment=on_payment,
analytics_webhook="https://your-webhook.com/payments"
)
async def validate_payment(payment_data):
# Check allowlist, rate limits, etc.
if payment_data.from_address in BLOCKED_ADDRESSES:
return False
return True
provider = X402Provider(X402Config(
wallet_address="0xYourAddress",
custom_validation=validate_payment
))
X402Config(
wallet_address="0xYourAddress", # Required: Your wallet address
chain_id=8453, # Default: Base mainnet
accepted_tokens=["0xA0b8..."], # Default: USDC
cache_enabled=True, # Default: True
cache_ttl=300, # Default: 5 minutes
analytics_enabled=True, # Default: True
analytics_webhook="https://...", # Optional: Webhook URL
custom_validation=validate_func, # Optional: Custom validation
)
Check out the examples directory for:
WALLET_ADDRESS=0xYourAddress
CHAIN_ID=8453
ACCEPTED_TOKENS=0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Main provider class for payment processing.
provider = X402Provider(config: X402Config)
Methods:
create_payment_requirement()
- Generate payment requirementsverify_payment()
- Verify payment signature and amountget_analytics()
- Get payment analyticsFastAPI middleware for automatic payment handling.
app.add_middleware(
x402_middleware,
wallet_address: str,
routes: Dict[str, Union[str, RouteConfig]],
**options
)
FastAPI dependency for payment validation.
PaymentRequired = X402Dependency(amount: str, token: Optional[str] = None)
Q: How much does it cost? A: Zero protocol fees! Only ~$0.001 in blockchain gas costs per transaction.
Q: Which blockchains are supported? A: Currently Base, Polygon, and Arbitrum. More coming soon!
Q: Can I accept multiple tokens?
A: Yes! Configure accepted_tokens
with any ERC-20 tokens.
Q: Is this production ready? A: Yes! Used by 40+ production APIs processing millions in micropayments.
We welcome contributions! Please see CONTRIBUTING.md for details.
MIT License - see LICENSE for details.
Built with ❤️ by the x402 team. Making micropayments simple for everyone!
FAQs
Lightning-fast x402 payment integration for FastAPI and modern Python web frameworks
We found that fast-x402 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.