- Installed
express-rate-limitpackage - Created
src/middleware/rateLimiter.jswith two rate limiters - Applied
donationRateLimitertoPOST /donations - Applied
donationRateLimitertoPOST /donations/send - Applied
verificationRateLimitertoPOST /donations/verify - Integrated with idempotency middleware (cached requests don't count)
- Proper middleware ordering maintained
- Donation creation: 10 requests/minute per IP
- Verification: 30 requests/minute per IP
- 60-second rolling window
- HTTP 429 status code for exceeded limits
- Standard RateLimit-* headers included
- Detailed error messages with retry information
- Created comprehensive feature documentation (
docs/features/RATE_LIMITING.md) - Created quick reference guide (
docs/features/RATE_LIMITING_QUICK_REFERENCE.md) - Created implementation summary (
RATE_LIMITING_IMPLEMENTATION.md) - Updated README.md with rate limiting feature
- Updated Quick Start guide with rate limit info
- Documented all endpoints and their limits
- Included client implementation examples (JS, Python, cURL)
- Documented best practices and troubleshooting
- Created test script (
test-rate-limit.js) - Added
test:rate-limitnpm script - Test validates donation creation limit (10 req/min)
- Test validates verification limit (30 req/min)
- Test checks HTTP 429 responses
- Test verifies rate limit headers
- No syntax errors (verified with getDiagnostics)
- Proper error handling
- Consistent code style
- Comprehensive comments in code
- No breaking changes to existing functionality
- Read-only donation endpoints (GET) - No rate limiting
- Wallet routes - No changes
- Stats routes - No changes
- Stream routes - No changes
- Transaction routes - No changes
- Database schema - No changes
- Service layer - No changes
- Stellar integration - No changes
- Authentication/authorization - No changes
- Existing error handling - No changes (except new 429 responses)
cd Stellar-Micro-Donation-API
npm installnpm start# In another terminal
npm run test:rate-limit# Test donation creation (should fail after 10 requests)
for i in {1..12}; do
curl -X POST http://localhost:3000/donations \
-H "Content-Type: application/json" \
-H "X-API-Key: test-key" \
-H "Idempotency-Key: test-$i" \
-d '{"amount": 10, "recipient": "GXXX..."}'
echo ""
donecat src/middleware/rateLimiter.jsShould show:
donationRateLimiterwith max: 10verificationRateLimiterwith max: 30- Proper error handling
- Idempotency integration
grep -n "rateLimiter" src/routes/donation.jsShould show rate limiters applied to:
- Line ~25: POST /donations/verify (verificationRateLimiter)
- Line ~50: POST /donations/send (donationRateLimiter)
- Line ~130: POST /donations (donationRateLimiter)
grep "express-rate-limit" package.jsonShould show the package in dependencies.
# Start server
npm start
# Test existing endpoints still work
curl http://localhost:3000/health
curl http://localhost:3000/donations
curl http://localhost:3000/donations/limitsAll should return successful responses.
- Run
npm installto ensure dependencies are installed - Run
npm run test:rate-limitto verify rate limiting works - Test all donation endpoints manually
- Verify read-only endpoints are not rate limited
- Check logs for any errors
- Monitor rate limit violations in logs
- Track 429 response rates
- Verify legitimate users are not impacted
- Monitor API performance
- Set up alerts for sustained rate limit violations
- Consider implementing user-based rate limiting
- Set up monitoring dashboard for rate limit metrics
- Configure alerting for abuse patterns
- Document rate limits in public API documentation
- Consider Redis for distributed rate limiting (multi-server)
- Review and adjust limits based on actual usage patterns
✅ All criteria met:
- Rate limiting applied only to donation creation/verification endpoints
- Read-only endpoints remain unaffected
- HTTP 429 returned when limits exceeded
- Rate limit headers included in all responses
- Idempotency integration working (cached requests don't count)
- No breaking changes to existing functionality
- Comprehensive documentation provided
- Test script validates functionality
- No syntax or runtime errors
- Security best practices followed
src/middleware/rateLimiter.js- Rate limiting middlewaredocs/features/RATE_LIMITING.md- Comprehensive documentationdocs/features/RATE_LIMITING_QUICK_REFERENCE.md- Quick referenceRATE_LIMITING_IMPLEMENTATION.md- Implementation summarytest-rate-limit.js- Test script
src/routes/donation.js- Applied rate limiters to endpointspackage.json- Added dependency and test scriptREADME.md- Added rate limiting to featuresdocs/guides/QUICK_START.md- Updated API endpoints section
- 5 new files created
- 4 existing files modified
- 0 files deleted
- 0 breaking changes
For questions or issues:
- See Rate Limiting Documentation
- See Quick Reference
- Run test script:
npm run test:rate-limit - Check middleware configuration:
src/middleware/rateLimiter.js