A Node.js Express server demonstrating server-side integration with the Humanity Protocol API, featuring Swagger documentation and proper error handling.
- RESTful API Endpoints: Proxy endpoints for Humanity Protocol API
- Swagger UI Documentation: Interactive API testing interface
- Environment Configuration: Secure API key management
- Error Handling: Comprehensive error messages and logging
- CORS Support: Enabled for cross-origin requests
- Request Logging: Timestamped console output for debugging
- Node.js 16.x or higher
- npm or yarn
- Humanity Protocol API key
cd server-example
npm installCreate a .env file from the example:
cp .env.example .envEdit .env and configure your settings:
HP_API_KEY=your_actual_api_key_here
HP_API_URL=https://api.humanity.org
PORT=3002Important Configuration Notes:
- Replace
your_actual_api_key_herewith your actual Humanity Protocol API key - For testnet, use
HP_API_URL=https://testnet-api.humanity.org - Default port is 3002 to avoid conflicts with client example
Development mode with auto-reload:
npm run devProduction mode:
npm startThe server will start on http://localhost:3002
Once the server is running, access the interactive Swagger documentation at:
http://localhost:3002/api-docs
The Swagger UI provides:
- Interactive API testing interface
- Complete request/response documentation
- Schema definitions and examples
- Pre-populated example wallet:
0xdead00000000000000000000000000000000beef
GET /verify/:walletAddress
Verifies if a wallet address belongs to a human.
Parameters:
walletAddress(path parameter): The wallet address to verify (EIP-55 compliant)
Example Request:
curl -X GET "http://localhost:3002/verify/0xdead00000000000000000000000000000000beef" \
-H "accept: application/json"Success Response (200):
{
"wallet_address": "0xdead00000000000000000000000000000000beef",
"is_human": true,
"user_id": "user_123456",
"timestamp": "2024-01-01T12:00:00.000Z",
"server": "humanity-protocol-server-example"
}Error Response (500):
{
"error": "Server configuration error",
"message": "API key not configured. Please check your .env file."
}GET /health
Returns the server status and configuration.
Example Request:
curl -X GET "http://localhost:3002/health" \
-H "accept: application/json"Response (200):
{
"status": "ok",
"timestamp": "2024-01-01T12:00:00.000Z",
"apiUrl": "https://api.humanity.org"
}server-example/
├── index.js # Main server file with routes and Swagger docs
├── package.json # Dependencies and scripts
├── .env.example # Environment variables template
└── README.md # This file
The server provides detailed error messages for common issues:
-
Missing Environment Variables
- Clear error messages indicating which variable is missing
- Instructions to check
.envfile
-
API Errors
- Forwards Humanity Protocol API errors with proper status codes
- Adds server timestamp and identifier
-
Network Errors
- Catches and logs connection failures
- Returns user-friendly error messages
The server logs all operations with timestamps:
[2024-01-01T12:00:00.000Z] Verifying wallet: 0xdead00000000000000000000000000000000beef
[2024-01-01T12:00:00.100Z] Making request to: https://api.humanity.org/v1/human/verify?wallet_address=0xdead00000000000000000000000000000000beef
[2024-01-01T12:00:00.500Z] Response: { wallet_address: '0xdead...', is_human: true, user_id: 'user_123' }
-
API Key Protection
- Never commit
.envfile to version control - Use environment variables for all sensitive data
- Consider using secrets management in production
- Never commit
-
Rate Limiting
- Implement rate limiting for production use
- Monitor API usage to prevent abuse
-
Input Validation
- The example trusts wallet address format validation to the Humanity Protocol API
- Consider adding EIP-55 checksum validation for production
-
Check if port 3002 is already in use:
lsof -i :3002
-
Change port in
.envfile if needed
- Verify
.envfile exists (not just.env.example) - Check API key format and validity
- Ensure no extra spaces or quotes in
.envfile
This indicates missing environment variables. Check:
.envfile exists and is properly formatted- Server was restarted after creating
.env - No typos in variable names
- Check network connectivity
- Verify API URL is correct (mainnet vs testnet)
- Check if API key has proper permissions
-
Adding New Endpoints
- Add route handler in
index.js - Add Swagger documentation above the handler
- Update schema definitions if needed
- Add route handler in
-
Testing
- Use Swagger UI for quick testing
- Monitor console logs for debugging
- Test with various wallet addresses
-
Production Deployment
- Use process manager like PM2
- Set up proper logging (consider Winston)
- Implement monitoring and alerts
| Variable | Description | Default | Required |
|---|---|---|---|
HP_API_KEY |
Your Humanity Protocol API key | - | Yes |
HP_API_URL |
Humanity Protocol API endpoint | https://api.humanity.org |
Yes |
PORT |
Server port | 3002 |
No |