Flask Server Integration

Start accepting x402 payments in your Flask server in 2 minutes

Step 1: Install dependencies

pip install x402 flask python-dotenv

Step 2: Set environment variables

ADDRESS=2HLDaAizrVqA7LzScQcCJeefAFmVKsCTPN8Y8H3t7MCD
FACILITATOR_URL=https://getconduit.io/api/facilitator
NETWORK=solana-devnet

Step 3: Create Flask app

import os
from flask import Flask, jsonify
from dotenv import load_dotenv
from x402.facilitator import FacilitatorConfig
from x402.flask.middleware import PaymentMiddleware

load_dotenv()

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

facilitator_config = FacilitatorConfig(url=FACILITATOR_URL)
app = Flask(__name__)
payment_middleware = PaymentMiddleware(app)

payment_middleware.add(
    path="/weather",
    price="$0.001",
    pay_to_address=ADDRESS,
    network="solana-devnet",
    facilitator_config=facilitator_config,
)

@app.route("/weather")
def get_weather():
    return jsonify({"weather": "sunny", "temperature": 70})

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

Step 4: Run the server

flask run

Your server is now accepting x402 payments!