A payment-enabled proxy for HuggingFace Inference API using X402 micropayments. Pay per request with USDC on Base blockchain.
| Environment | API | Playground |
|---|---|---|
| Testnet (Base Sepolia) | x402-huggingface-testnet.operations-4bf.workers.dev | x402-huggingface-playground-testnet.pages.dev |
| Mainnet (Base) | x402-huggingface-mainnet.operations-4bf.workers.dev | x402-huggingface-playground-mainnet.pages.dev |
- HuggingFace Compatible: Same API as HuggingFace (
POST /models/{org}/{model}) - X402 Micropayments: Pay-per-request with USDC on Base
- Per-Model Pricing: Different prices for different models
- 10 Supported Tasks: Classification, detection, segmentation, embeddings, image generation, speech-to-text, and more
- Playground UI: React frontend with wallet integration
| Task | Example Model | Price |
|---|---|---|
| Text Classification | distilbert/distilbert-base-uncased-finetuned-sst-2-english |
$0.0015 |
| Image Classification | google/vit-base-patch16-224 |
$0.003 |
| Audio Classification | MIT/ast-finetuned-audioset-10-10-0.4593 |
$0.003 |
| Object Detection | facebook/detr-resnet-50 |
$0.0075 |
| Image Segmentation | facebook/mask2former-swin-large-coco-panoptic |
$0.015 |
| Embeddings | sentence-transformers/all-MiniLM-L6-v2 |
$0.0003 |
| Text to Image | black-forest-labs/FLUX.1-dev |
$0.03 |
| Image to Image | lllyasviel/control_v11p_sd15_canny |
$0.03 |
| Speech to Text | openai/whisper-large-v3 |
$0.015 |
| Fill Mask | google-bert/bert-base-uncased |
$0.00075 |
Models not in the pricing list use the default price of $0.01.
npm installcp .dev.vars.example .dev.varsEdit .dev.vars:
HF_TOKEN=your_huggingface_token
Update wrangler.toml with your payment address:
PAYMENT_ADDRESS = "0xYourWalletAddress"npm run devAPI available at http://localhost:8787
# Set HuggingFace token secret
wrangler secret put HF_TOKEN
# Deploy to testnet (Base Sepolia)
npm run deploy:testnet
# Deploy to mainnet (Base)
npm run deploy:mainnetPOST /models/{org}/{model}
Matches HuggingFace Inference API exactly.
curl -X POST http://localhost:8787/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english \
-H "Content-Type: application/json" \
-d '{"inputs": "I love this product!"}'First request returns 402 Payment Required with payment details. After signing payment with your wallet, include the signature:
curl -X POST http://localhost:8787/models/distilbert/distilbert-base-uncased-finetuned-sst-2-english \
-H "Content-Type: application/json" \
-H "PAYMENT-SIGNATURE: <base64-encoded-payment>" \
-d '{"inputs": "I love this product!"}'curl -X POST http://localhost:8787/models/google/vit-base-patch16-224 \
-H "PAYMENT-SIGNATURE: <base64-encoded-payment>" \
--data-binary @image.jpgGET /- API informationGET /pricing- Current pricing policy
Pricing is configured in src/pricing/policy.ts. Add models to the endpoints object:
endpoints: {
"org/model-name": {
basePrice: "3000", // $0.003 (1 USDC = 1,000,000 units)
description: "Model description",
},
}Models not listed use defaultPrice ($0.01).
React frontend for testing the API with wallet integration.
cd playground
npm install
cp .env.example .env.local
# Edit .env.local
npm run devVITE_API_URL=http://localhost:8787
VITE_NETWORK=base-sepolia
VITE_WALLETCONNECT_PROJECT_ID= # Optional - works without it for browser extension wallets
├── src/
│ ├── index.ts # Main entry point
│ ├── x402/
│ │ ├── middleware.ts # X402 payment middleware
│ │ └── types.ts # Environment types
│ ├── pricing/
│ │ ├── engine.ts # Pricing calculation
│ │ ├── policy.ts # Per-model prices
│ │ └── types.ts # Pricing types
│ └── routes/
│ ├── models.ts # HF-compatible /models route
│ └── pricing.ts # Pricing endpoints
├── playground/ # React frontend
├── wrangler.toml # Cloudflare Workers config
└── package.json
| Variable | Description |
|---|---|
PAYMENT_NETWORK |
eip155:8453 (Base) or eip155:84532 (Sepolia) |
PAYMENT_ADDRESS |
Your wallet address for receiving payments |
DEFAULT_PRICE |
Default price in USDC atomic units |
| Secret | Description |
|---|---|
HF_TOKEN |
HuggingFace API token |
CDP_API_KEY_ID |
Optional: Coinbase CDP key ID |
CDP_API_KEY_SECRET |
Optional: Coinbase CDP key secret |
- Client sends request without payment
- Server returns
402 Payment Requiredwith price and payment details - Client signs EIP-712 payment authorization with wallet
- Client retries with
PAYMENT-SIGNATUREheader - Server verifies and processes request
- Server settles payment on-chain
- Response includes
X-Payment-Transactionheader with tx hash
MIT