A demonstration client for using Pinata's x402 service to upload files to IPFS with automatic payment processing. This project shows how to integrate with Pinata's payment-gated IPFS pinning service using the x402 protocol.
- x402 Payment Integration: Automatic payment processing for IPFS uploads
- Pinata IPFS Integration: Upload files to IPFS through Pinata's service
- CDP Wallet Integration: Use Coinbase Developer Platform wallets for payments
- Pre-signed URL Upload: Secure file upload flow with pre-signed URLs
- TypeScript: Full TypeScript implementation for type safety
- Payment Tracking: Track payment transactions and view on block explorer
- Node.js: JavaScript runtime environment
- TypeScript: Type-safe JavaScript superset
- Coinbase CDP SDK: For wallet management and payments
- x402-axios: Client library for x402 payment protocol
- Viem: Ethereum account management
- Pinata: IPFS pinning service
- Axios: HTTP client for API requests
- Node.js (v16 or later)
- npm or yarn package manager
- Coinbase Developer Platform (CDP) API credentials
- Base network testnet tokens for payments
- Access to Pinata's x402 service
# Clone the repository
git clone https://github.com/HeimLabs/coinbase-cdp-demos.git
cd 07-x402-Pinata-Client-Demo
# Install dependencies
npm installCreate a .env file in the root directory:
# Copy the example environment file
cp .env.example .envEdit the .env file and add your credentials:
# Coinbase Developer Platform API Keys
CDP_API_KEY_ID=your_cdp_api_key_id
CDP_API_KEY_SECRET=your_cdp_api_key_secret
CDP_WALLET_SECRET=your_cdp_wallet_secret
# Pinata x402 API Configuration
PINATA_X402_BASE_URL=https://402.pinata.cloud
PINATA_X402_PIN_PUBLIC_PATH=/v1/pin/public# Build the TypeScript code
npm run build
# Run the demo
npm startThe demo follows this workflow:
- Initialize CDP Client: Create a CDP client and wallet account
- Setup Payment Interceptor: Configure x402-axios with payment capabilities
- Create File Content: Generate a demo file with unique UUID
- Request Pre-signed URL: Make payment-gated request to Pinata
- Upload to IPFS: Upload file using the pre-signed URL
- Get IPFS CID: Receive Content Identifier for the uploaded file
Client Request → x402 Payment Check → Payment Processing → API Access → IPFS Upload
When you run the demo, you'll see output like:
🚀 Initializing Pinata x402 Client...
📍 Base URL: https://402.pinata.cloud
📍 Pin Path: /v1/pin/public
💳 Wallet address: 0x123...abc
📄 File details:
- Content: Hello from x402! UUID: 12345678-1234-1234-1234-123456789012
- Size: 45 bytes
📤 Requesting pre-signed URL from Pinata...
✅ Pre-signed URL obtained!
💰 Payment info: { transaction: "0xabc...def" }
🔗 Transaction: https://basescan.org/tx/0xabc...def
📁 Uploading file to Pinata...
✅ File upload completed!
🎉 SUCCESS! File pinned to IPFS
📌 IPFS CID: QmXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
🌐 View on Pinata Gateway: https://gateway.pinata.cloud/ipfs/QmXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
🌐 View on IPFS.io: https://ipfs.io/ipfs/QmXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-
CDP Client Initialization
const cdpClient = new CdpClient(); const serverAccount = await cdpClient.evm.getOrCreateAccount({ name: "MyCDPMainnetPinataPayer1", });
-
Payment Interceptor Setup
const x402Api = withPaymentInterceptor( axios.create({ baseURL: pinataBaseURL }), account );
-
File Upload Process
const presignedResponse = await x402Api.post(pinataPinPath, { fileSize: fileBuffer.length });
- Secure Wallet Management: Uses CDP SDK for secure key management
- Payment Verification: All payments are cryptographically verified
- Pre-signed URLs: Secure file upload without exposing credentials
- Environment Variables: All sensitive data stored in environment variables
- Base URL:
https://402.pinata.cloud - Pin Endpoint:
/v1/pin/public - Payment Required: USDC on Base network
- Response: Pre-signed URL for file upload
- Client makes request to protected endpoint
- x402 middleware checks for payment
- If payment required, client wallet processes payment
- Payment verification completes
- API returns pre-signed URL
- File upload proceeds to IPFS
npm run build: Build TypeScript to JavaScriptnpm start: Run the demo applicationnpm test: Run tests (not implemented)
To upload your own files, modify the file content generation:
// Replace this section in src/x402-pinata.ts
const fileContent = `Your custom content here`;
const fileBuffer = Buffer.from(fileContent);For file uploads from disk:
import fs from 'fs';
const fileBuffer = fs.readFileSync('path/to/your/file.txt');CDP_API_KEY_ID: Your CDP API key identifierCDP_API_KEY_SECRET: Your CDP API key secretCDP_WALLET_SECRET: Your CDP wallet secret for encryptionPINATA_X402_BASE_URL: Pinata's x402 service base URLPINATA_X402_PIN_PUBLIC_PATH: API endpoint path for pinning
The demo creates a wallet account named MyCDPMainnetPinataPayer1. You can customize this name in the code.
- Decentralized File Storage: Store files on IPFS with payment
- Content Monetization: Charge for file uploads and storage
- Proof of Payment: Demonstrate payment-gated IPFS services
- Web3 Integration: Integrate IPFS storage into dApps
For production use, consider:
- Error Handling: Implement comprehensive error handling
- Rate Limiting: Handle API rate limits appropriately
- File Validation: Validate file types and sizes
- Payment Monitoring: Monitor payment success/failure rates
- Backup Strategy: Consider multiple IPFS pinning services
- Payment Failures: Ensure wallet has sufficient balance
- API Errors: Check Pinata service status
- File Upload Errors: Verify file size and format
- Network Issues: Ensure stable internet connection
- Check console logs for detailed error messages
- Verify environment variables are set correctly
- Test with smaller files first
- Monitor payment transactions on block explorer
- Pinata Documentation
- x402 Protocol Documentation
- IPFS Documentation
- Coinbase Developer Platform
- Viem Documentation
- Never commit your
.envfile or expose private keys - Use testnet for development and testing
- Monitor payment transactions and wallet balances
- Implement proper access controls for production use
- Regularly rotate API keys and secrets
This project is licensed under the ISC License.
Disclaimer: This project is for demonstration purposes. For production use, additional security measures, monitoring, and testing should be implemented.
Built with ❤️ using Pinata, x402 Protocol, and Coinbase Developer Platform.