RESTful APIs

Shaun Coghlan

A breakdown of the difference between RESTful APIs and APIs.

hero background

What are RESTful APIs? 

Despite what you might think, these APIs are not sleeping on the job! Unlike regular APIs that set rules and tools for software applications to communicate, RESTful APIs follow the principles of REST (Representational State Transfer). These principles serve as an architectural style for designing networked applications. 

What is the difference between APIs and RESTful APIs? 

To begin, we need to understand that "API" is a general term that can refer to any set of rules established between software applications, regardless of the architectural style. In contrast, RESTful APIs are a more specific type of API that follows the REST architecture. This means that everything from protocols to state and data formats is more specific with RESTful APIs. In short, while all RESTful APIs are APIs, not all APIs are RESTful. 

Where would I use a RESTful API? 

Thanks to the flexibility of RESTful APIs, they can be used in any situation that requires you to connect with software to share data. In web and mobile applications, they may be used to fetch user data from social media platforms, while in location-based services, you would use a mapping service API like Google Maps. 

Examples 

Below are some examples of how you might use a RESTful API to retrieve user data from a social media platform such as Facebook. Note that to interact with Facebook's API, you need to use the Facebook Graph API. You'll also need to have an access token, which you can obtain by authenticating through Facebook's OAuth process, which you can learn more about here. (https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/)  
 
Step-by-Step Example 

1. Get Access Token 

First, you'll need to authenticate and get an access token from Facebook. This is usually done through the OAuth 2.0 authentication process. For simplicity, let's assume you already have an access token. 

2. Make a Request to the Graph API 

To fetch user data, you'll make a GET request to the Graph API endpoint. Here’s how you might do this in "Python" using the requests library: 

import requests

access_token = 'YOUR_ACCESS_TOKEN'
url = 'https://graph.facebook.com/v12.0/me'
params = {
    'fields': 'id,name,email',
    'access_token': access_token
}

response = requests.get(url, params=params)
user_data = response.json()

print(user_data)