Skip to content

Flask Integration

This guide walks you through adding x402 payment requirements to a Flask server. By the end, you will have a running API that charges $0.001 per request on a protected route, with payment verification and settlement handled by the facilitator.

Installation

bash
pip install x402 flask python-dotenv

Environment Setup

Create a .env file in your project root:

bash
ADDRESS=<your-solana-wallet-address>
FACILITATOR_URL=https://facilitator.svmacc.tech
VariableDescription
ADDRESSThe Solana wallet address where you want to receive payments.
FACILITATOR_URLThe URL of the x402 facilitator service that verifies and settles payments.

Server Code

Create a file called server.py:

python
import os
from dotenv import load_dotenv
from flask import Flask, jsonify
from x402.middleware.flask import require_payment
from x402.types import FacilitatorConfig

load_dotenv()

app = Flask(__name__)

ADDRESS = os.getenv("ADDRESS")
FACILITATOR_URL = os.getenv("FACILITATOR_URL")

facilitator = FacilitatorConfig(url=FACILITATOR_URL)

@app.route("/weather")
@require_payment(
    address=ADDRESS,
    facilitator=facilitator,
    price="$0.001",
    network="solana-devnet",
    description="Get weather data",
)
def get_weather():
    return jsonify({"report": {"weather": "sunny", "temperature": 70}})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=4021)

How It Works

  1. FacilitatorConfig creates a configuration object pointing at the facilitator service at https://facilitator.svmacc.tech.
  2. The @require_payment decorator wraps your route handler. When a request arrives without a valid X-PAYMENT header, it returns a 402 Payment Required response containing the payment terms (price, network, facilitator URL, and your wallet address).
  3. When a client includes a valid payment header, the decorator forwards the payment proof to the facilitator for on-chain verification and settlement. If the payment is valid, your route handler executes normally.
  4. The network parameter is set to solana-devnet for testing. Switch to solana-mainnet for production.

Running the Server

bash
python server.py

The server will start on http://localhost:4021. Requests to GET /weather without payment will receive a 402 response with payment instructions.

Protecting Multiple Routes

Apply the @require_payment decorator to any route you want to charge for:

python
@app.route("/weather")
@require_payment(
    address=ADDRESS,
    facilitator=facilitator,
    price="$0.001",
    network="solana-devnet",
    description="Get weather data",
)
def get_weather():
    return jsonify({"report": {"weather": "sunny", "temperature": 70}})

@app.route("/forecast")
@require_payment(
    address=ADDRESS,
    facilitator=facilitator,
    price="$0.005",
    network="solana-devnet",
    description="Get 7-day forecast",
)
def get_forecast():
    return jsonify({"forecast": [{"day": "Monday", "weather": "sunny"}]})

Routes without the decorator remain free and accessible without payment.

Powered by SVM Facilitator