Photo restoration API: Production-ready Python example with x402 v2

Python 🐍 x402: Check out our new example showing how easy it is to build revenue-generating APIs using SKALE performance, with a photo restoration service that goes far beyond an LLM wrapper.

Photo restoration API: Production-ready Python example with x402 v2
Photo by Mitchell Orr / Unsplash

Also available in Português and Español.

🐍 The x402 protocol isn't just about payments. It's about making useful tools economically viable.

The API Paywall Cookbook now includes a production-ready Python example that demonstrates this: a photo restoration API that preserves memories using computer vision and AI. More importantly, it shows how developers can build real tools, monetize them with x402, and deploy them to production.

Why this matters

This example celebrates the Python x402 SDK's recent upgrade to support the v2 specification. The SDK now handles CAIP-2 network identifiers, standardized payment headers, and the modular architecture that v2 introduces. You get type safety, EIP-712 signature validation, and support for multiple blockchain networks in a Pythonic interface.

The photo restoration API processes images through a two-phase pipeline that combines OpenCV computer vision with Gemini AI. This approach delivers results that pure LLM wrappers can't match. OpenCV handles edge detection, perspective correction, and auto-rotation—critical when restoring photos of photos taken with a smartphone. The system analyzes dominant line orientation to straighten tilted images automatically. After this preprocessing, Gemini performs the actual restoration and colorization with historically accurate results.

This combination matters because feeding raw, uncorrected images directly to an AI model produces inferior results. The computer vision layer normalizes the input, and the AI layer handles the creative restoration work. The API verifies payment via x402 before processing, demonstrating how to gate access to compute-intensive operations.

Production-ready foundation

This isn't a toy example. The repository includes deployment configuration, security validations, integration tests, and Docker setup. You'll find:

  • FastAPI with async HTTP handling
  • Magic byte validation to prevent malicious uploads
  • EIP-712 typed signatures for payment verification
  • Multi-network support via CAIP-2 identifiers
  • Docker multi-stage builds for production deployment
  • Type-safe Pydantic models across all requests

The implementation shows you how to integrate the Python x402 SDK with FastAPI middleware, handle payment verification, and respond with proper 402 status codes. It works with any facilitator that implements the x402 v2 specification, not just Kobaru.

Developer-friendly revenue infrastructure

If you choose Kobaru as your facilitator, you get access to Console—a revenue command center designed for developers who don't want to spend time building analytics dashboards. Console shows you transaction history, payment status, network activity, and revenue metrics without requiring you to instrument your API with tracking code.

The example uses SKALE as the default network. SKALE is a high-end blockchain designed specifically for x402 performance, offering gasless transactions and the throughput needed for payment-gated APIs. The SDK also supports Base and Solana through the same interface. Network selection happens at the capability configuration level, not in your application code.

Use it as a foundation

The repository is structured as a reference implementation. You can clone it, replace the image processing logic with your own service, update the pricing configuration, and deploy. The payment handling, security validations, and Docker configuration are already solved problems.

For implementation details and deployment instructions, see the example repository. For SDK documentation and integration guides, visit docs.kobaru.io.