Skip to main content

Flask

Use Flask when you want synchronous Python route handlers with 4Mica payment protection. The 4mica-x402 Flask middleware handles HTTP 402 responses, guarantee verification, and settlement for protected routes.

Prerequisites

  • Python 3.10 or later.
  • A Flask app.
  • A seller wallet address for payTo.
  • A buyer wallet with deposited 4Mica collateral for paid requests.

Install

Create wallet

Create a dedicated EVM wallet for the buyer client and use a seller address for payTo. For wallet setup, see Wallet.
Use the private key only for buyer-side signing. Use PAY_TO as the seller address advertised by protected routes.

Deposit collateral

Deposit collateral for the buyer wallet before it calls the protected route. Use the same network in the buyer scheme and route configuration. To learn more about collateral setup, see Deposits and Withdrawals.

Protect a route

Support payment

Use the synchronous requests wrapper in Flask clients, scripts, workers, or any blocking Python code.

What happens

  1. The buyer requests /premium-content without payment.
  2. Flask returns HTTP 402 with payment requirements.
  3. The requests wrapper creates a unique request ID, signs a guarantee, and retries with a payment header.
  4. The middleware verifies and settles the payment before your handler serves the response.