Pay for AI Image Generation with Web3

Seamlessly integrate cryptocurrency payments into your AI workflows using Coinbase’s x402 protocol

View x402 Documentation

What is x402?

X402 is a revolutionary payment protocol developed by Coinbase that enables programmatic payments over HTTP using Web3 technologies. It allows applications to request payments for resources in a simple, stateless manner using standard HTTP status codes and headers. Our integration brings x402 payments to AI image generation, making it possible to pay for premium AI services using cryptocurrency directly through HTTP requests.

HTTP-Native Payments

Uses standard HTTP 402 status codes and headers for seamless payment integration into existing web infrastructure.

Built for AI Agents

Perfect for autonomous AI systems that need to make payments programmatically without human intervention.

Stateless Protocol

No sessions or complex authentication required. Each payment is self-contained and verifiable.

Developer Friendly

Simple integration with standard HTTP libraries and familiar request-response patterns.

How X402 Works with Our AI Mystic Endpoint

Our Mystic model now supports x402 payments, enabling you to pay for AI image generation using cryptocurrency. Here’s how the payment flow works:

Payment Flow

1

Make Initial Request

Send a standard HTTP POST request to /v1/x402/ai/mystic with your prompt and model parameters.
2

Receive Payment Requirements

Server responds with HTTP 402 Payment Required status and payment details including amount, recipient, and network.
3

Create Payment

Your configured wallet automatically creates and signs a payment payload based on the requirements.
4

Submit Payment

The SDK automatically resubmits your request with the X-PAYMENT header containing the signed payment.
5

Verification & Settlement

Server verifies the payment with the facilitator and processes it on-chain for transparent settlement.
6

Receive Content

Get your AI-generated image along with settlement details in the X-PAYMENT-RESPONSE header.

Integration Benefits

Instant Payments

No waiting for traditional payment processing. Cryptocurrency payments are verified and settled quickly on-chain.

Global Access

Accept payments from anywhere in the world without traditional banking restrictions or currency conversions.

Lower Fees

Reduced transaction costs compared to traditional payment processors, especially for micro-payments.

Transparent Settlement

All payments are verifiable on-chain, providing complete transparency and audit trails.

Getting Started

Set Up Your Wallet

Create or connect a Web3 wallet and fund it with USDC on the supported network. Ensure you have enough balance for both payments and gas fees.
New to Web3 wallets? Check out the Coinbase guide for creating a wallet.

Install the SDK

Add the x402 client library for your programming language. The SDK handles all payment complexity automatically.
npm
npm install x402-fetch x402-axios viem
pip
pip install x402 eth-account

Configure Your Client

Initialize your wallet and wrap your HTTP client with the x402 adapter:
const account = privateKeyToAccount("0xYourPrivateKey");
const fetchWithPayment = wrapFetchWithPayment(fetch, account);

Make Your First Request

Send a request to /v1/x402/ai/mystic - the SDK handles payment automatically:
const response = await fetchWithPayment(
  "https://api.freepik.com/v1/x402/ai/mystic",
  { method: "POST", body: JSON.stringify({...}) }
);

Implementation Examples

Using the x402 SDKs, payment handling is completely automatic. Just set up your wallet and make requests:
// Using x402-fetch (Node.js)
import { wrapFetchWithPayment, decodeXPaymentResponse } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";

// Set up your wallet
const account = privateKeyToAccount("0xYourPrivateKey"); // Use env variable

// Create fetch with automatic payment handling
const fetchWithPayment = wrapFetchWithPayment(fetch, account);

// Make request to AI endpoint - payment is automatic!
const response = await fetchWithPayment("https://api.freepik.com/v1/x402/ai/mystic", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    prompt: "A futuristic city at sunset",
    model: "realism"
  })
});

// Get your generated image
const result = await response.json();
console.log("Generated image:", result);

// Optional: Get payment details
const paymentInfo = decodeXPaymentResponse(
  response.headers.get("x-payment-response")
);
console.log("Payment settled:", paymentInfo);

Installation

# For Node.js/JavaScript
npm install x402-fetch x402-axios viem
# or
yarn add x402-fetch x402-axios viem

Learn More

For complete setup guides including wallet configuration and advanced options, check out the official documentation:

Key Advantages

For Developers:
  • Simple HTTP-based payment integration
  • No complex payment gateway setups
  • Standard REST API patterns
  • Built-in payment verification
For Users:
  • Direct cryptocurrency payments
  • No account creation required
  • Global accessibility
  • Transparent, on-chain settlements
For Businesses:
  • Reduced payment processing costs
  • Instant settlement capabilities
  • Global market reach
  • Programmable payment logic

Support and Resources

Ready to integrate Web3 payments into your AI workflows? Start by making your first request to /v1/x402/ai/mystic and experience the future of programmable payments!