Trakt.tv API is a great way to integrate movie and TV show data into your website. This tutorial will show you how to use the API to display what movie or TV show you are currently watching on your NEXT.JS website.
# Prerequisites
Before you begin, you’ll need to make sure you have a few things set up:
- A NEXT.JS website
- A Trakt.tv account
- A
Client ID
from Trakt.tv/settings/applications
To create a Trakt application and get a Client ID
, follow the steps bellow:
- Head over to Trakt.tv and sign up for an account or sign in.
- Go to Trakt.tv/settings/applications and click on the
New Application
. - Fill in the details (use
http://localhost:3000
forRedirect URI
) and click onCreate Application
. - You’ll be redirected to the application page where you’ll find your
Client ID
andClient Secret
. For this tutorial, we’ll be using theClient ID
to make API calls to Trakt.
# Making API Calls to Trakt
Now that we have our Client ID
, we can make API calls to Trakt.
First of all, inside your .env.local
file, add the Client ID
as an environment variable.
Second of all, we will make an API call to get shows and movies that we’ve watched. To do this, we’ll be using the getNowWatching
function in the lib/trakt.ts
file.
# Create an API Route
Now that we have our API call, we can create an API route to fetch the data from Trakt.
This API route will return what you’re currently watching. If you’re not watching anything, it will return an empty object.
If you’re watching a TV show, this will be the response:
If you’re watching a movie, this will be the response:
Congratulations! You’ve just created an API route that let’s you use the data we fetched from Trakt to display the movie or TV show you are currently watching.
# How to check-in to a show or a movie
Info: Trakt has released their official Android app which allows you to check-in to a show or a movie. You can download it from the Play Store
I personally use an app called cinetrakapp.com which is available for both Android and iOS. It’s a great app that allows you to check-in to the movie or TV show you’re watching. And it also helps you keep track of what you’ve watched and discover new movies and TV shows to watch.
# Conclusion
Using the Trakt.tv API in combination with the cinetrakapp.com app, you can easily display the movie or TV show the user is currently watching on your NEXT.JS website. With just a few lines of code, you can add this feature to your website and provide an enhanced user experience.