Architecture Overview
The application implements a proxy layer between the frontend and TMDB API to:- Secure API keys (never exposed to the client)
- Implement rate limiting to comply with TMDB’s limits
- Handle authentication and session management
- Provide consistent error handling
- Enable server-side caching (future enhancement)
Request Flow
- Client makes request to
/api/* - Next.js API route validates and processes the request
- Rate limiter checks if request is allowed
- Request is proxied to TMDB API with server-side credentials
- Response is returned to client
Proxy Endpoint
The catch-all proxy endpoint handles requests that don’t match specific routes.Implementation
Usage
Any TMDB API endpoint can be accessed through the proxy:Supported Methods
GET- Retrieve data from TMDBPOST- Create/update data (limited to rating endpoints)DELETE- Delete data (limited to rating endpoints)
Rate Limiting
Popcorn Vision implements token bucket rate limiting to comply with TMDB’s API limits.Configuration
Rate Limits
- 40 requests per 10 seconds (TMDB recommendation)
- Applies to all API endpoints
- Shared across all users (server-side limiting)
How It Works
- Each API route calls
limiter.removeTokens(1)before making a request - If tokens are available, the request proceeds
- If no tokens remain, a 429 error is returned
- Tokens replenish at a rate of 40 per 10 seconds
Rate Limit Response
Best Practices
- Cache responses on the client when possible
- Batch requests where applicable
- Debounce search queries to avoid rapid successive requests
- Use pagination wisely - don’t load all pages at once
- Implement loading states to prevent duplicate requests
Environment Variables
The following environment variables are required:Your TMDB API key (v3 auth)
TMDB API base URL:
https://api.themoviedb.org/3Setup
.env.local
Never commit
.env.local to version control. Keep your API key secure.Session Management
User sessions are managed using HTTP-only cookies for security.Cookies
The authenticated user’s session ID
- Max Age: 1 year
- Set by:
/api/authentication/login - Used by: All authenticated endpoints
Temporary authentication token
- Max Age: 1 hour
- Set by:
/api/authentication/token/new - Used by: Authentication flow
Automatic Session Injection
For endpoints that require authentication, the session ID is automatically injected:Error Handling
All API routes implement consistent error handling:Common Error Responses
401 Unauthorized
Authentication required or invalid session
404 Not Found
Resource does not exist
429 Too Many Requests
Rate limit exceeded
500 Internal Server Error
Server or TMDB API error
Caching Strategy
While not currently implemented, here are recommended caching strategies:Client-Side Caching
Server-Side Caching
Could be implemented using:- Redis for distributed caching
- Next.js Cache API for simple caching
- HTTP Cache Headers for browser caching
Cache Invalidation
Consider cache invalidation for:- User-specific data (favorites, watchlist, ratings)
- Trending/popular content (update hourly/daily)
- Static data (genres, languages) can be cached longer
Best Practices
Security
- Never expose API keys to the client
- Use HTTP-only cookies for sessions
- Validate and sanitize all user inputs
- Implement CORS properly
Performance
- Implement request deduplication
- Use pagination for large datasets
- Cache responses where appropriate
- Optimize images with TMDB’s size parameters
Reliability
- Handle rate limits gracefully
- Implement retry logic for transient errors
- Provide meaningful error messages
- Log errors for monitoring
TMDB API Guidelines
- Respect rate limits (40 req/10s)
- Always include attribution to TMDB
- Keep API keys secure
- Review TMDB API Terms of Use