Skip to contents

Ping the timeline endpoint. The X API only allows fetching up to the most recent 3,200 posts.

Usage

x_get_timeline(
  username,
  max_results = 10,
  end_time = NULL,
  start_time = NULL,
  until_id = NULL,
  since_id = NULL,
  pagination_token = NULL,
  exclude = NULL,
  sleep_time = 0,
  bearer_token = Sys.getenv("X_BEARER_TOKEN"),
  post_fields = c("created_at", "text", "public_metrics", "geo", "attachments",
    "context_annotations", "entities", "lang", "referenced_tweets", "reply_settings",
    "conversation_id", "in_reply_to_user_id", "author_id", "edit_history_tweet_ids",
    "id"),
  user_fields = c("created_at", "description", "protected", "entities", "location",
    "profile_image_url", "public_metrics", "verified", "verified_type"),
  media_fields = c("duration_ms", "height", "width", "preview_image_url", "type", "url",
    "public_metrics", "variants", "media_key"),
  poll_fields = c("end_datetime", "duration_minutes", "options", "voting_status", "id"),
  place_fields = c("contained_within", "country", "country_code", "full_name", "geo",
    "id", "name", "place_type"),
  expansions = c("author_id", "entities.mentions.username",
    "referenced_tweets.id.author_id", "referenced_tweets.id", "in_reply_to_user_id",
    "attachments.media_keys", "attachments.poll_ids")
)

Arguments

username

character; the name of the account on X without the "@" symbol.

max_results

numeric; a number between 5 and 100 indicating the number of posts returned per API call.

end_time

The latest date-time from which you want to get posts. Provide the value in ISO 8601 format (i.e., YYYY-MM-DDTHH:mm:ssZ). The iso_8601() function will convert a string, date, or date-time object to the required format (e.g., iso_8601("2024-10-10")).

start_time

The earliest date-time from which you want to get posts.

until_id

A post ID to limit the results to posts older than the specified ID.

since_id

A post ID to limit the results to posts more recent than the specified ID.

pagination_token

A string used to navigate backward through result pages. The X API provides this token when more results are available. Typically, you won’t need to set this manually as the function handles it, but you can supply a pagination_token from a previous response to continue retrieving results beyond the last page, if desired.

exclude

A comma-separated list of the types of posts to exclude from the response (e.g., "retweets", "replies", or "retweets,replies"). #' @param sleep_time A numeric value specifying the number of seconds to wait between API calls. This helps avoid hitting rate limits imposed by the X API. You can adjust this value based on your tier's rate limits, which are detailed on the X API documentation website.

bearer_token

A string containing the bearer token for authenticating with the X API. By default, this argument retrieves the token from the environment variable X_BEARER_TOKEN (via Sys.getenv("X_BEARER_TOKEN")). Adding your bearer token to your .Renviron file ensures it is securely stored and accessible without needing to manually input it for each session.

post_fields

character, vector; a list of fields to return for each post c("created_at", "text", "public_metrics", "geo", "attachments","context_annotations", "entities", "lang", "referenced_tweets", "reply_settings", "conversation_id", "in_reply_to_user_id", "author_id", "edit_history_tweet_ids", "id")

user_fields

character, vector; a list of fields to return for each user c("created_at", "description", "protected", "entities", "location", "profile_image_url", "public_metrics", "verified", "verified_type")

Value

A list containing the four elements that make up the API response

Examples

if (FALSE) { # \dontrun{
tl <- x_get_timeline("Tesla")
} # }