library(tidyverse) # All the tidy things
library(jsonlite) # Converting json data into data frames
library(magrittr) # Extracting items from list objects using piping grammar
library(httr) # Interacting with HTTP verbs
Exploring OMDb API
What is OMDb?
The Open Movie Database (OMDb) API allows users to access movie information using simple web-based requests. It can return details like a movie’s release year, plot, summary, cast, genre, ratings, etc. One may use this API to build a movie search app, analyze trends in film ratings, or retrieve summaries for film studies or entertainment projects.
How to get OMDb API Key
To use the OMDB API, you must first obtain a personal API key. You can do so by visiting the official website: https://www.omdbapi.com
Once there, select the “API Key” option from the navigation menu. You will be directed to a registration page where you will select the account type of your choice and provide your email address. After submitting, your unique API key will be sent to you via email.
Setting Up API in R
The OMDb API uses a base URL and query parameters to define what kind of data to return. Every request must include at least a title and API key. Additional parameters like media type and release year help narrow the search.
To interact with API in R, you’ll need to load few packages:
Defined below is a query for the movie The Matrix. This API request includes the title, specifies media type, and uses your API key. The API response is a JSON object, included information like the title, year, director, genre, and ratings.
<- "your_api_key_here" #placeholder only; real key used privately
api_key <- "https://www.omdbapi.com/"
base_url
#Call to API - search for movie
<- list(
parameters apikey = api_key,
t = "The Matrix",
type = "movie",
r = "json"
)
#Send API request
<- GET(url = base_url, query = parameters)
response
#Convert to text and then to data frame
<- content(response, as = "text", encoding = "UTF-8") %>%
movie_data fromJSON(flatten = TRUE)
This example demonstrates how to use OMDb APi to retrieve detailed information about a specific movie. It shows how to construct a valid request, convert the API’s response into a readable format, and begin exploring the data in R. This same structure can be used to explore other films or television series by adjusting the search parameters.