A Node.js API server built with Hono that provides anonymous authentication and AI image processing through Replicate's image restoration models.
- Anonymous Authentication: JWT-based auth without requiring personal information
- Rate Limiting: Per-user daily and hourly limits to prevent abuse
- AI Image Processing: Restore and enhance images using Replicate's models
- Development Tools: Test endpoint for quick iteration
- Health Monitoring: Built-in health checks and user statistics
Client Request β Authentication β Rate Limiting β Replicate Processing β Response
- Authentication: Users authenticate with device fingerprint to get JWT token
- Rate Limiting: Each user has daily/hourly processing limits
- Image Processing: Images are sent directly to Replicate for AI processing
- Response: Processed image URL returned to client
Health check endpoint showing server status and metrics.
Response:
{
"status": "healthy",
"timestamp": "2024-01-15T10:30:00.000Z",
"users": 42,
"uptime": 3600,
"memory": { "rss": 67108864, "heapTotal": 20971520, "heapUsed": 18874368 },
"version": "1.0.0"
}Get comprehensive public statistics about API usage across multiple time periods. No authentication required.
Response:
{
"timestamp": "2024-01-15T10:30:00.000Z",
"periods": {
"24h": {
"totalRequests": 156,
"uniqueUsers": 23,
"averageRequestsPerUser": 6.78
},
"7d": {
"totalRequests": 1247,
"uniqueUsers": 89,
"averageRequestsPerUser": 14.01
},
"30d": {
"totalRequests": 4567,
"uniqueUsers": 234,
"averageRequestsPerUser": 19.52
}
},
"histogram": {
"type": "daily",
"period": "7d",
"data": [
{
"date": "2024-01-09",
"requests": 45,
"users": 12
},
{
"date": "2024-01-10",
"requests": 67,
"users": 18
},
{
"date": "2024-01-11",
"requests": 89,
"users": 23
},
{
"date": "2024-01-12",
"requests": 123,
"users": 31
},
{
"date": "2024-01-13",
"requests": 156,
"users": 28
},
{
"date": "2024-01-14",
"requests": 134,
"users": 25
},
{
"date": "2024-01-15",
"requests": 156,
"users": 23
}
]
},
"totals": {
"totalUsers": 234
}
}Notes:
- Multiple time periods: 24h, 7d, and 30d statistics
- Daily histogram: Breakdown of requests and users by day for the last 7 days
- Extensible structure: Easy to add new time periods or histogram types
- Real-time data: All statistics are calculated in real-time
- No authentication required: Public endpoint for monitoring API usage
Create an anonymous user session and get JWT token.
Input:
{
"device_info": "unique-device-fingerprint-string",
"app_version": "1.0.0"
}Output:
{
"success": true,
"token": "eyJhbGciOiJIUzI1NiIs...",
"userId": "device-fingerprint-string",
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"limits": {
"daily": 20,
"remaining": 20,
"resetAt": "2024-01-16T00:00:00.000Z"
}
}Notes:
device_infois used as both device fingerprint and user ID- JWT token expires in 24 hours
- Each device gets 20 requests per day
Process an image using AI restoration models.
Headers:
Authorization: Bearer <jwt-token>
Content-Type: application/json
Input:
multipart/form-data
image: <image-file>Output:
{
"success": true,
"result": "https://replicate.delivery/xezq/abc123.png",
"requestId": "550e8400-e29b-41d4-a716-446655440000",
"processingTime": 6234,
"limits": {
"daily": 20,
"remaining": 19,
"resetAt": "2024-01-16T00:00:00.000Z"
}
}Notes:
- Images must be under 10MB
- Processing typically takes 5-7 seconds
- Result is a URL to the processed image
- Rate limits are enforced per user
Get current user's usage statistics and limits.
Headers:
Authorization: Bearer <jwt-token>
Output:
{
"success": true,
"stats": {
"userId": "device12...",
"sessionId": "550e840...",
"memberSince": "2024-01-15T10:00:00.000Z",
"totalRequests": 5,
"averageProcessingTime": 6234,
"sessionsCount": 3,
"lastSeen": "2024-01-15T10:30:00.000Z",
"limits": {
"daily": 20,
"used": 5,
"remaining": 15,
"resetAt": "2024-01-16T00:00:00.000Z"
}
}
}Test image processing without authentication using a local image file.
Requirements:
NODE_ENV=developmenthenri-cartier-bresson.jpgfile in project root
Output:
{
"success": true,
"result": "https://replicate.delivery/xezq/abc123.png",
"processingTime": 6234,
"savedTo": "/path/to/henri-cartier-bresson-restored-2024-01-15T10-30-00-000Z.jpg",
"originalSize": "42KB",
"resultSize": "156KB"
}Notes:
- Automatically saves processed image locally
- No authentication required
- Perfect for testing and development
Create a .env file with:
# Required
JWT_SECRET=your-super-secret-jwt-key-make-it-very-long-and-random
REPLICATE_API_TOKEN=r8_your_replicate_token_here
# Optional
PORT=3500
NODE_ENV=development- Daily Limit: 20 requests per user per day
- Hourly Limit: 5 requests per user per hour (25% of daily)
- Global Limit: 100 requests per IP per 15 minutes
- Resets: Daily limits reset at midnight UTC
-
Install dependencies:
npm install
-
Set up environment:
cp .env.example .env # Edit .env with your API tokens -
Start development server:
npm run dev
-
Test the API:
# Health check curl http://localhost:3500/health # Get auth token curl -X POST http://localhost:3500/api/auth/anonymous \ -H "Content-Type: application/json" \ -d '{"device_info":"my-unique-device-id","app_version":"1.0.0"}'
- Authenticate: POST to
/api/auth/anonymouswith device info - Get Token: Receive JWT token for subsequent requests
- Process Image: POST to
/api/replicate/colorisewith image data - Monitor Usage: GET
/api/statsto check remaining limits - Handle Result: Download processed image from returned URL
All endpoints return consistent error formats:
{
"error": "Description of what went wrong",
"details": "Additional technical details (development only)"
}Common HTTP status codes:
400: Bad request (missing fields, invalid image)401: Unauthorized (invalid/expired token)404: Endpoint not found408: Request timeout429: Rate limit exceeded500: Internal server error
- Connect your repository to Railway
- Set environment variables:
JWT_SECRET: Generate a secure random stringREPLICATE_API_TOKEN: Your Replicate API token
- Deploy: Railway will automatically build and deploy
This is a standard Node application that can be deployed to any platform that supports Node.js.
This project is licensed under the MIT License - see the LICENSE file for details.