X402 Web3 Payments
Pay for AI Image Generation with Web3
Seamlessly integrate cryptocurrency payments into your AI workflows using Coinbase's x402 protocol
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.
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
participant Client
participant API
participant Facilitator
participant Blockchain
Client->>API: POST /v1/x402/ai/mystic (initial request)
API-->>Client: HTTP 402 Payment Required + requirements
Client->>API: POST with X-PAYMENT header
API->>Facilitator: Verify payment
Facilitator->>Blockchain: Check on-chain
Blockchain-->>Facilitator: Payment confirmed
Facilitator-->>API: Valid payment receipt
API-->>Client: HTTP 200 + content + X-PAYMENT-RESPONSEIntegration Benefits
Getting Started
Implementation Examples
Using the x402 SDKs, payment handling is completely automatic. Just set up your wallet and make requests:
javascript
// 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.magnific.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);javascript
// Using x402-axios (Node.js)
import axios from 'axios';
import { withPaymentInterceptor } from 'x402-axios';
import { privateKeyToAccount } from 'viem/accounts';
// Set up your wallet
const account = privateKeyToAccount('0xYourPrivateKey'); // Use env variable
// Create axios instance with automatic payment handling
const api = withPaymentInterceptor(axios.create({ baseURL: 'https://api.magnific.com' }), account);
// Make request - payment is automatic!
const response = await api.post('/v1/x402/ai/mystic', {
prompt: 'A futuristic city at sunset',
model: 'realism',
});
console.log('Generated image:', response.data);python
# Using x402 with requests (Python)
import requests
from eth_account import Account
from x402.clients.requests import x402_http_adapter
# Set up your wallet
account = Account.from_key("your_private_key") # Use env variable
# Create session with automatic payment handling
session = requests.Session()
adapter = x402_http_adapter(account)
session.mount("https://", adapter)
# Make request - payment is automatic!
response = session.post(
"https://api.magnific.com/v1/x402/ai/mystic",
json={
"prompt": "A futuristic city at sunset",
"model": "realism"
}
)
print("Generated image:", response.json())
# Optional: Get payment details from header
payment_info = response.headers.get("X-PAYMENT-RESPONSE")
print("Payment settled:", payment_info)python
# Using x402 with HTTPX (async Python)
import httpx
from eth_account import Account
from x402.clients.httpx import x402_payment_hooks
# Set up your wallet
account = Account.from_key("your_private_key") # Use env variable
# Create async client with automatic payment handling
async with httpx.AsyncClient() as client:
# Add x402 payment hooks
client.event_hooks = x402_payment_hooks(account)
# Make request - payment is automatic!
response = await client.post(
"https://api.magnific.com/v1/x402/ai/mystic",
json={
"prompt": "A futuristic city at sunset",
"model": "realism"
}
)
result = await response.json()
print("Generated image:", result)Installation
bash
# For Node.js/JavaScript
npm install x402-fetch x402-axios viem
# or
yarn add x402-fetch x402-axios viembash
# For Python
pip install x402 eth-account httpxLearn 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!