I'm building a small project with HTML, CSS, and vanilla JavaScript. I want to pick a random movie, filter by one or more genres, and skip movies saved to a watched list. Is there a free API that would work for this?
Is there a movie API that can pick a random movie by genre?
Answers
The Movie Database (TMDB) looks like a good fit. Its discover/movie endpoint can filter movies with the with_genres parameter. Genres use IDs rather than names, so fetch the genre list and keep a mapping for your filter controls.
For multiple genres, comma-separated IDs request movies matching all of them. The results include titles and release dates, so you can extract the year when a date is available. You'll need to register for API access and a key. Check the current terms for your intended use.
Reply to this answer
Don't depend on a random endpoint. Run a discover query with your selected genres, choose a random page within the API's allowed page range, then pick a random result from that page. This gives you a random pick from the results you fetched, though it won't necessarily make every matching movie equally likely.
Keep watched TMDB IDs in a Set for quick checks. If the pick is watched, try another result or page. Put a limit on retries and show a message if you can't find an unwatched match, otherwise the app could keep requesting forever when the filters are narrow.
Reply to this answer
For a small browser-only project, localStorage is a reasonable place to start for watched IDs. IndexedDB is another option if you want more structured local data. Neither requires a separate backend, but the watched list stays in that browser's storage rather than automatically syncing across devices.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Vaskel.5n:
Also handle an empty result set before choosing a page or item. Some genre combinations might not return anything.