Authentication overview
TMDb utilizes a multi-tiered authentication approach designed to support different levels of access and user interaction. The primary method for accessing most read-only endpoints is a simple API key. This key identifies the application making the request and is essential for tracking usage against rate limits and ensuring compliance with TMDb's API terms of use. For more advanced interactions, particularly those involving user-specific data or write operations, TMDb also supports a session-based authentication flow, which is typically initiated via a web-based authorization process, leveraging an OAuth 2.0-like mechanism, specifically for its v4 API endpoints.
The choice of authentication method depends on the specific API endpoint being accessed and the nature of the operation. For general data retrieval, the API key is sufficient. When an application needs to perform actions such as rating a movie, adding to a watchlist, or managing user lists, a more robust, user-centric authentication method is required to authorize these actions securely on behalf of a TMDb user. This distinction helps maintain both the security of user data and the integrity of the TMDb platform.
Supported authentication methods
TMDb primarily supports two main authentication methods to cater to various integration needs:
- API Key (v3 API): This is the most common method for public data access. It involves passing a unique string as a query parameter with each request. The API key identifies the application making the request and is used for rate limiting and basic access control. It is suitable for read-only operations that do not require user-specific context.
- Session-based Authentication (v4 API & user-specific actions): For actions that require user authorization, such as rating movies, adding to watchlists, or managing personal lists, TMDb employs a session-based authentication flow. This process typically involves a user authorizing an application to act on their behalf, resulting in a temporary session token. This token is then used in subsequent requests to perform authenticated user-specific operations. This method is more secure for sensitive actions as it ties requests to an authenticated TMDb user rather than just an application. The v4 API specifically uses an access token for this purpose, which is generated through an OAuth 2.0 flow, requiring developer approval and user consent.
Authentication methods overview
| Method | When to Use | Security Level |
|---|---|---|
| API Key (v3) | Public read-only data access (e.g., movie details, search). | Moderate (identifies application, not user). |
| Session-based / Access Token (v4) | User-specific write operations (e.g., ratings, watchlists, account management). | High (identifies application and specific user, requires user consent). |
Getting your credentials
To begin interacting with the TMDb API, you first need to obtain the necessary credentials. The process is straightforward and involves registering an account and then requesting an API key.
- Create a TMDb Account: Navigate to the TMDb website and sign up for a new account if you don't already have one. This account will be linked to your API key and usage.
- Access the API Section: Once logged in, go to your API settings page. Here, you will find options to request a new API key.
- Request an API Key: Choose to request a new API key. You will typically be asked to specify the type of use (e.g., developer, commercial) and agree to the TMDb API terms of use. You may need to provide details about your application or project.
- Receive Your API Key: After your request is approved, your unique API key (v3 auth) will be displayed on your API settings page. This key is a long alphanumeric string. Keep it secure, as it grants access to the TMDb API on your behalf.
- (Optional) Request a v4 API Read Access Token: For applications requiring user-specific actions and leveraging the v4 API, you will also need to generate a v4 API Read Access Token. This is typically done from the same API settings page and provides a bearer token suitable for OAuth 2.0-style authentication. This token is separate from the v3 API key and is used for different sets of endpoints, as detailed in the TMDb API reference documentation.
It is crucial to understand the distinction between the v3 API key and the v4 access token. The v3 key is for general application identification and access to most public data. The v4 token is for more secure, user-authenticated interactions. Ensure you use the correct credential for the specific API endpoint you are targeting.
Authenticated request example
This section demonstrates how to make an authenticated request using the primary API key method for the TMDb v3 API. For this example, we'll fetch details for a popular movie.
Example using API Key (v3)
To authenticate with the TMDb v3 API, you typically include your api_key as a query parameter in your request URL. This is a common pattern for RESTful APIs that use simple API key authentication, as described by MDN Web Docs on HTTP Authentication.
HTTP Request Example
This example retrieves details for a specific movie (e.g., Movie ID 550 for Fight Club).
GET https://api.themoviedb.org/3/movie/550?api_key=YOUR_API_KEY
Host: api.themoviedb.org
Replace YOUR_API_KEY with the actual API key you obtained from your TMDb API settings page.
cURL Example
A cURL command provides a direct way to test this request from your terminal:
curl --request GET \
--url 'https://api.themoviedb.org/3/movie/550?api_key=YOUR_API_KEY' \
--header 'Accept: application/json'
Python Example
Using the requests library in Python:
import requests
api_key = "YOUR_API_KEY"
movie_id = 550 # Example: Fight Club
url = f"https://api.themoviedb.org/3/movie/{movie_id}"
params = {
"api_key": api_key
}
response = requests.get(url, params=params)
if response.status_code == 200:
print(response.json())
else:
print(f"Error: {response.status_code} - {response.text}")
Example using Access Token (v4)
For v4 API endpoints that require user authentication, you will use a bearer token in the Authorization header.
HTTP Request Example
GET https://api.themoviedb.org/4/account/{account_id}/lists
Host: api.themoviedb.org
Authorization: Bearer YOUR_V4_ACCESS_TOKEN
Replace YOUR_V4_ACCESS_TOKEN with the v4 access token obtained through the authorization flow, and {account_id} with the user's TMDb account ID.
cURL Example
curl --request GET \
--url 'https://api.themoviedb.org/4/account/{account_id}/lists' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer YOUR_V4_ACCESS_TOKEN'
Security best practices
Securing your TMDb API credentials is vital to prevent unauthorized access to your account and to ensure compliance with TMDb's usage policies. Adhere to these best practices:
- Keep API Keys Confidential: Your API key is your credential for TMDb. Never embed it directly into client-side code (e.g., JavaScript in a web browser, mobile app source code) where it can be easily extracted. Instead, use a backend server to make API calls, proxying requests from your client application. This protects your API key from public exposure.
- Use Environment Variables for API Keys: When developing, store your API keys in environment variables rather than hardcoding them into your application's source code. This practice is crucial for preventing accidental exposure of keys in version control systems (like Git repositories) and simplifies key rotation.
- Implement HTTPS (SSL/TLS): All communication with the TMDb API should occur over HTTPS. This encrypts data in transit, protecting your API key and other sensitive information from eavesdropping. TMDb enforces HTTPS for all API endpoints, but it's important to ensure your application also uses secure connections.
- Regularly Rotate API Keys: Periodically generate new API keys and revoke old ones. This minimizes the risk associated with a compromised key, as an attacker would only have a limited window of access. While TMDb does not enforce key rotation, it is a recommended security practice for any API integration.
- Strictly Control Access Tokens (v4 API): For v4 API access tokens, which are tied to user authorization, ensure they are stored securely and only used for the intended, authorized actions. Implement proper OAuth 2.0 flows, including token refresh mechanisms, to manage these tokens safely. Never store user access tokens indefinitely or in an insecure manner.
- Validate and Sanitize Inputs: Although authentication secures access to the API, ensure all user inputs sent to TMDb endpoints are validated and sanitized to prevent common web vulnerabilities like injection attacks.
- Monitor Usage: Regularly check your API usage statistics on your TMDb account. Unusual spikes in usage could indicate a compromised API key or an issue with your application, providing an early warning system for potential security incidents.
- Adhere to Rate Limits: While not strictly a security measure, adhering to TMDb's rate limits prevents your API key from being temporarily blocked or revoked due to overuse, which could be exploited by an attacker attempting a denial-of-service against your application.