Hugging Face Spaces Deployment Guide
Overview
This backend is configured for deployment on Hugging Face Spaces using Docker.
Requirements
Environment Variables
Set these in Hugging Face Spaces Settings β Repository secrets:
Required:
DATABASE_URL- PostgreSQL connection stringJWT_SECRET- Secret key for JWT tokensPAYSTACK_SECRET_KEY- Paystack secret keyPAYSTACK_PUBLIC_KEY- Paystack public key
Optional:
PORT- Server port (default: 7860 for HF Spaces)NODE_ENV- Environment (default: production)PINATA_JWT- Pinata IPFS JWT tokenGATEWAY_URL- IPFS gateway URLNGN_PER_USD- Exchange rate fallback (default: 750)POINT_VALUE_USD- Point value in USD (default: 0.05)CURRENCY_API_KEY- For dynamic exchange ratesFIXER_API_KEY- Alternative exchange rate API
Hugging Face Spaces Configuration
1. Hardware Requirements
- CPU: 2 vCPU minimum
- RAM: 4GB minimum
- Disk: 10GB minimum
2. Dockerfile Setup
The repository includes a Dockerfile that:
- Uses Node.js 20 Alpine (lightweight)
- Multi-stage build for optimization
- Runs as non-root user for security
- Exposes port 7860 (HF Spaces default)
3. Space Settings
In your Hugging Face Space:
- Go to Settings
- Set Docker as the SDK
- Set hardware requirements
- Add environment variables (secrets)
4. Port Configuration
Hugging Face Spaces automatically maps port 7860. The Dockerfile and server are configured to use this port.
Build Process
Local Build Test
# Build the Docker image
docker build -t zurri-backend .
# Run locally
docker run -p 7860:7860 --env-file .env zurri-backend
Production Build
The Dockerfile uses multi-stage build:
- Builder stage: Installs all dependencies and builds TypeScript
- Production stage: Only copies production dependencies and built files
Database Setup
PostgreSQL on Hugging Face Spaces
Hugging Face Spaces doesn't provide managed databases. Options:
External Database (Recommended)
- Use services like Render, Supabase, or Railway
- Set
DATABASE_URLin HF Spaces secrets
Database in Docker (Not recommended for production)
- Could run PostgreSQL in same container (not ideal)
Health Check
The Dockerfile includes a health check endpoint:
GET /health
Hugging Face Spaces will automatically monitor this.
Logs
View logs in Hugging Face Spaces:
- Go to your Space
- Click Logs tab
- Monitor server output and errors
API Documentation
Once deployed, access Swagger UI at:
https://your-space.hf.space/docs
Troubleshooting
Port Issues
- Ensure
PORT=7860is set (or useSPACE_PORT) - Check Hugging Face Spaces logs for port binding errors
Database Connection
- Verify
DATABASE_URLis correctly set - Check database allows connections from HF Spaces IPs
- Test connection locally before deploying
Build Failures
- Check Dockerfile syntax
- Verify all dependencies in
package.json - Review build logs in HF Spaces
Memory Issues
- Increase RAM allocation in Space settings
- Check for memory leaks in application
- Monitor memory usage in logs
Security Considerations
- Environment Variables: Never commit secrets to git
- CORS: Configure CORS for your frontend domain
- Rate Limiting: Already configured in
app.ts - Helmet: Security headers enabled
- Non-root User: Dockerfile runs as non-root user
Monitoring
Health Endpoint
curl https://your-space.hf.space/health
API Endpoints
All endpoints are documented in Swagger:
https://your-space.hf.space/docs
Deployment Checklist
- Set all required environment variables in HF Spaces
- Configure external PostgreSQL database
- Set correct CORS origins (if needed)
- Test health endpoint after deployment
- Verify database connection
- Test payment integration (Paystack)
- Monitor logs for errors
- Update frontend API URLs to point to HF Space
Notes
- Hugging Face Spaces provides HTTPS automatically
- Webhook URLs need to be updated to HF Space URL
- Paystack webhook URL:
https://your-space.hf.space/api/wallet/webhook/paystack - Callback URL:
https://your-space.hf.space/api/wallet/callback
Support
For issues:
- Check Hugging Face Spaces logs
- Review application logs
- Verify environment variables
- Test endpoints via Swagger UI