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 endpoint, I’ll show how to work
with JSON returned from the X API. User lookup is a GET method and returns
information about a user or group of users, specified by a user ID or a
username.If you aren’t familiar, R is one of the most popular languages for common Data
Science tasks like time-series analysis, modeling, visualization, and other data
analysis, and is often used in conjunction with the X API. With the user
lookup endpoint, you can use the
user object to
determine a correlation between the number of followers a person has and the
sentiment score of their bio. The user object may also be used to map a group of
accounts based on the location publicly listed in their profiles.
Before you can use the X API v2, you will need to
sign up
for a developer account.Once you have an approved developer account, you will need to first create a
Project. Projects allow you to organize your work
based on how you intend to use the X API, so you can effectively manage
your access to the API, and monitor your usage.Each Project contains an App, with which you can
generate the credentials required to use the X API. You can learn more
about how to get started with the X API, in the
getting started section of our
documentation.
For the code examples, I’m going to be showing today, you will want to create an
environment variable for your bearer token. The Bearer Token is what allows you
to authenticate to the X API and start making requests. First, replace
“your-bearer-token” with your own bearer token, which can be obtained from the
keys and tokens section of your App in the developer portal. You’ll need to run
this line of code in the console before you start writing a script.
You can use the package
httr to make HTTP
requests to the X API. If you haven’t already installed this, please
install the package in your console. You will also need to install
jsonlite to work
with our JSON object and dplyr for data
manipulation.
You can now begin writing your R script to connect to the API. At the top of the
file, call the packages httr, jsonlite, and dplyr.
Copy
Ask AI
require(httr)require(jsonlite)require(dplyr)
The first step in your code sample is to get set up to authenticate to the
X API. Grab the
Bearer Token you pulled from
your App, and pass that into your headers for authentication. In the below
example, replace $BEARER_TOKEN with your token.
Once you got your authentication set up, define the parameters of your request.
By default, you will get back the id, name, and username of each user you get
back. You can make adjustments to this payload by adding additional
fields and
expansions. For this example, you will want
the profile bio of the user which is requested using user.fields=description,
and an expansion that contains the pinned Post of the user.
Now you are ready to format your URL with the X handle, also known as
account, you are looking to get more information about. Use the readline method
to allow this sample to be reusable. After you type the handle you want to look
at, format your URL to contain the handle you define by replacing $USERNAME with
the desired X handle.
At this point, use the httr package to make a GET request to the URL you just
created, pass in our authentication credential via the header, and pass in the
parameters you defined. You can save the response as a text object in the
variable obj and print this out to view the result of the request you made.
One of my favorite ways to work with a JSON is to use a data frame, which allows
you to easily access complex nested data. To do this, use the fromJSON method of
the jsonlite package to flatten your file to allow the fields to be in the same
object. Then, pass that object into a data frame. Now you are ready to view this
data frame.
Hopefully, this tutorial can be a starting place to work with R and the X
API. As a next step, you may want to look at our R samples for recent search,
Post lookup and user lookup in our
v2 sample code. Be sure to let us know on the
forums if you run into any troubles along the
way or Post us at @XDevelopers if this
tutorial inspires you to create anything.