Complete example showing how to monetize private IPFS files using Pinata's x402 protocol.
- Install dependencies:
bun install- Set environment variables:
cp .env.example .env
# Edit .env with your actual valuesOr export directly:
export PINATA_JWT="your_pinata_jwt_here"
export WALLET_ADDRESS="your_ethereum_wallet_address"
export GATEWAY_DOMAIN="your-gateway.mypinata.cloud" # Optional- Run the demo:
bun startAll configuration is set via constants at the top of x402-demo.ts:
PRICE_USD: Price in USD (default: $1.00)NETWORK: Blockchain network ("base" for production, "base-sepolia" for testing)USDC_CONTRACT: USDC contract address (auto-configured based on network)FILE_CONTENT: Content of the file to monetize
- Uploads a private file to Pinata's Private IPFS network
- Creates a payment instruction with your price and wallet
- Links the file to payment requirements
- Tests the payment flow by accessing the monetized URL
- Provides the shareable URL for requesters
The demo will output a URL like:
https://your-gateway.mypinata.cloud/x402/cid/QmYourFileHash
When requesters access this URL without payment, they'll receive a 402 response with payment requirements. After payment, they can access the file.
- Replace the gateway domain with your actual Pinata gateway
- Share the monetized URL with requesters
- Monitor your wallet for incoming payments