Weather Demo
The Weather demo shows how to protect a practical API endpoint that returns dynamic data. It demonstrates protecting an endpoint with query parameters while integrating with a third-party API (Open-Meteo).Overview
This demo creates an Express server with a weather API endpoint that requires payment to access. The endpoint fetches real weather data for any city from the Open-Meteo API (no API key required) and returns temperature information. Key Features:- Protects dynamic endpoint with query parameters
- Integrates with external weather API (Open-Meteo)
- Price: $0.001 USDC on
base-sepolia - Returns current temperature for requested city
- Demonstrates real-world API monetization pattern
Setup
Installation
Environment Variables
.env
PAY_TO- Your EVM wallet address to receive paymentsPORT- Server port (default: 3100)TARGET_URL- Full URL including query parameters for testingPRIVATE_KEY- Private key for payment header generation
Server Implementation
The server uses x402 middleware and integrates with Open-Meteo API:src/server.ts
Usage
Access the Endpoint
Open the weather endpoint with a city query parameter:Request Without Payment (JSON)
Request With Payment
Request With Paywall HTML
Generating Payment Headers
Configure Credentials
Generate Header
Use With curl
- Fetches
/weatherendpoint to read payment requirements - Signs
exactEVM payment with specified amount - Encodes payment data as Base64 X-PAYMENT header
- Works by default for
base-sepolianetwork
Query Parameters
City Parameter
Thecity query parameter accepts any city name:
Error Handling
If city is not found:Testing
Pretty-Print Response
Test Multiple Cities
Test Error Cases
Weather API Integration
This demo uses Open-Meteo, a free weather API:Geocoding API
Converts city names to coordinates:Weather API
Fetches current temperature:Why Open-Meteo?
- No API key required
- Free for development and testing
- Reliable global coverage
- Simple REST API
- Current weather + forecasts available
Payment Flow
Network Configuration
Base Sepolia Testnet:- Network:
base-sepolia - USDC Contract:
0x036CbD53842c5426634e7929541eC2318f3dCF7e - Price: $0.001 USDC (1000 base units)
Get Testnet Tokens
Base Sepolia ETH: Base Sepolia USDC:- https://faucet.circle.com/ (select Base Sepolia)
Use Cases
This pattern is perfect for:API Monetization
- Charge per API call
- Micro-transactions for data access
- Usage-based pricing
Data Services
- Weather data
- Financial market data
- Geographic information
- Sports scores and statistics
Content APIs
- Article access
- Image generation
- Translation services
- Data enrichment
Key Differences from Ping Demo
| Feature | Ping Demo | Weather Demo |
|---|---|---|
| Endpoint | Static /ping | Dynamic /weather?city=... |
| Response | Fixed JSON | Dynamic API data |
| External API | None | Open-Meteo integration |
| Error Handling | Basic | City not found, API errors |
| Complexity | Minimal | Real-world pattern |
Dependencies
package.json
Next Steps
Ping Crossmint
Add smart wallet integration with React UI
Ping Demo
Start with the basic implementation
Solana Demo
Implement paywalls on Solana
x402 Express
Explore the middleware documentation
Troubleshooting
Open-Meteo API Errors
Error: City not found Solution: Verify city name spelling. Try major cities first. Use URL encoding for spaces.Port Already in Use
Error:Error: listen EADDRINUSE ::1:3100
Solution:
Payment Verification Fails
Error: Still getting 402 with payment header Solutions:- Generate fresh payment header (nonces are single-use)
- Verify TARGET_URL matches exact endpoint and query params
- Check payer wallet has USDC balance on base-sepolia
- Ensure network parameter is “base-sepolia” in both client and server