If you’ve thought about getting data from another source on the internet, such as Twitter you need to use Rest API. But what is a REST API and how do you use it with Twitter?
What Is A REST API
An API is an application programming interface. It is a set of rules that allow programs to talk to each other. The developer creates the API on the server and allows the client to talk to it.
REST determines how the API looks like. It stands for “Representational State Transfer”. It is a set of rules that developers follow when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.
Each URL is called a request while the data sent back to you is called a response.
The Anatomy Of A Request
It’s important to know that a request is made up of four things:
- The endpoint
- The method
- The headers
- The data (or body)
The endpoint (or route) is the url you request for. It follows this structure:
The path determines the resource you’re requesting for. You can access paths just like you can link to parts of a website.
To understand what paths are available to you, you need to look through the API documentation. For example, let’s say you want to get a list of tweets, retweets or likes through Twitter’s API. The docs tell you to use the following path to do so:
Any colons (
:) on a path denotes a variable. You should replace these values with actual values of when you send your request. In this case, you should replace
:id with the actual id of the tweet/retweet/like you’re searching for.
Twitter serves many objects as JSON, including Tweets and Users. These objects all encapsulate core attributes that describe the object. Each Tweet has an author, a message, a unique ID, a timestamp of when it was posted, and sometimes geo metadata shared by the user. Each User has a Twitter name, an ID, a number of followers, and most often an account bio.
Here’s a sample tweet:
The following JSON illustrates the structure for these objects and some of their attributes:
"created_at": "Thu Apr 06 15:24:15 +0000 2017",
"text": "1\/ Today we\u2019re sharing our vision for the future of the Twitter API platform!\nhttps:\/\/t.co\/XweGngmxlP",
"name": "Twitter Dev",
"description": "Your official source for Twitter Platform news, updates & events. Need technical help? Visit https:\/\/twittercommunity.com\/ \u2328\ufe0f #TapIntoTwitter"
"title": "Building the Future of the Twitter API Platform"
The method is the type of request you send to the server. You can choose from these five types below:
These methods provide meaning for the request you’re making. They are used to perform four possible actions:
The API lets you know what request method to use each request. For example, to get a user’s tweets, you need a
Resource URL: https://api.twitter.com/1.1/statuses/user_timeline.json
Follow, search, and get users
GET users / show is used to retrieve a single user object.
There are a few things to note when using this method.
- You must be following a protected user to be able to see their most recent status update. If you don’t follow a protected user their status will be removed.
- The order of user IDs or screen names may not match the order of users in the returned array.
- If a requested user is unknown, suspended, or deleted, then that user will not be returned in the results list.
$ curl --request GET
--header 'authorization: OAuth oauth_consumer_key="consumer-key-for-app",
oauth_version="1.0"'$ twurl /1.1/users/lookup.json?user_id=783214,6253282
HTTP Status Codes And Error Messages
HTTP status codes let you tell the status of the response quickly. The range from 100+ to 500+. In general, the numbers follow the following rules:
- 200+ means the request has succeeded.
- 300+ means the request is redirected to another URL
- 400+ means an error that originates from the client has occurred
- 500+ means an error that originates from the server has occurred
The standard Twitter API returns HTTP status codes in addition to JSON-based error codes and messages and it attempts to return appropriate HTTP status codes for every request.