CodingNic

Persist the Wishlist and Load Saved Movies

Build the Movie Data API Route

Persist the Wishlist and Load Saved Movies 10 min read

Build the Movie Data API Route

Build the Movie Data API Route

The browser wishlist stores ids, not full movie objects. Create the server-side route that can turn one saved id back into a movie summary without exposing the TMDB key to the browser.

Step 1 — Create the route

Create app/api/movies/[id]/route.ts. The route parameter gives you the movie id.

Step 2 — Import the server data function

Use getMovieDetail from lib/tmdb.ts. The browser should never need to know about the TMDB API key or URL construction.

Step 3 — Fetch on the server

Read the id from the route params, request the movie detail, and return only the JSON the wishlist page needs.

typescript
return Response.json(movie);

Handle request failures with an appropriate error response rather than returning a successful response with invalid data.

Checkpoint

With the development server running, visit /api/movies/<known-id> in the browser. The route should return JSON for a real movie id and keep the TMDB request server-side.