Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions examples/python/servers/fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def async_create_headers():
# Custom dynamic pricing middleware for /acp-budget
async def dynamic_price_middleware(request: Request, call_next):
"""Middleware that reads price from X-Budget header"""
if not request.url.path.startswith("/acp-budget"):
if not request.url.path.startswith("/acp-budget") and not request.url.path.startswith("/acp-budget/v2"):
return await call_next(request)

# Read dynamic price from X-Budget header
Expand All @@ -124,10 +124,13 @@ async def dynamic_price_middleware(request: Request, call_next):
pay_to_address = ACP_V2_RECEIVER_ADDRESS
else:
pay_to_address = ACP_V1_RECEIVER_ADDRESS

if request.url.path.startswith("/acp-budget/v2"):
pay_to_address = ACP_V2_RECEIVER_ADDRESS

# Use the standard require_payment middleware with dynamic price
payment_middleware = require_payment(
path="/acp-budget",
path=["/acp-budget", "/acp-budget/v2"],
price=budget, # ⭐ dynamic price
pay_to_address=pay_to_address,
network=NETWORK,
Expand Down Expand Up @@ -257,6 +260,32 @@ async def acp_budget(request: Request) -> Dict[str, Any]:
}
)

@app.api_route("/acp-budget/v2", methods=["GET", "POST"])
async def acp_budget(request: Request) -> Dict[str, Any]:
"""
Handle both GET and POST requests for ACP budget payment.
x402scan may use either method depending on the request.
"""
from fastapi.responses import JSONResponse

response_data = {
"message": "pay acp job budget",
"token": "acp job payment token",
"protocol": "x402",
"utility": "none",
"vibes": "acp early adopter",
"advice": "not financial advice",
"method": request.method # Show which method was used
}

# Explicitly disable compression for x402scan compatibility
return JSONResponse(
content=response_data,
headers={
"Content-Encoding": "identity", # Tell CloudFlare: no compression
"Cache-Control": "no-transform", # Prevent any transformation
}
)

# @app.get("/premium/content")
# async def get_premium_content() -> Dict[str, Any]:
Expand Down
Binary file modified examples/python/servers/fastapi/static/favicon.ico
Binary file not shown.