Getting started with R and v2 of the X API
This tutorial will walk through what you need to get started with the programming language R and the X API v2. Using R to connect to the user lookup.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
This tutorial will walk through what you need to get started with the programming language R and the X API v2. Using R to connect to the user lookup.
Sys.setenv(BEARER_TOKEN = "your-bearer-token")
install.packages("httr")
install.packages("jsonlite")
install.packages("dplyr")
require(httr)
require(jsonlite)
require(dplyr)
bearer_token <- Sys.getenv("$BEARER_TOKEN")
headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))
params <- list(`user.fields` = 'description', `expansions` = 'pinned_tweet_id')
handle <- readline('$USERNAME')
url_handle <- sprintf('https://api.x.com/2/users/by?usernames=%s', handle)
response <-
httr::GET(url = url_handle,
httr::add_headers(.headers = headers),
query = params)
obj <- httr::content(response, as = "text")
print(obj)
json_data <- fromJSON(obj, flatten = TRUE) %>% as.data.frame View(json_data)
final <-
sprintf(
"Handle: %s\nBio: %s\nPinned Post: %s",
json_data$data.username,
json_data$data.description,
json_data$includes.tweets.text
)
cat(final)