Learn REST APIs
1st Edition
Adonis Gaitatzis
BackupBrain Publishing, 2019
ISBN: 978-1-9993817-6-9
backupbrain.co
Learn REST APIs
by Tony Gaitatzis
Copyright 2019 All Rights Reserved
All rights reserved. This book or any portion thereof may not be reproduced or used in any manner whatsoever without the express written permission of the publisher except for the use of brief quotations in a book review. For permission requests, write to the publisher, addressed Learn REST APIs at the address below.
To Nick and Chrisoula, for all the fresh tea
Introduction
The Internet runs on APIs. Social media apps, hotel, and travel sites, Netflix, and more use APIs to load, save, and analyze data over the Internet.
APIs or "Application Programming Interfaces" are computer programs that talk to other computer programs. They provide an interface for programmed applications to communicate, and they are becoming big business.
Representational State Transfer or REST APIs are a type of API hosted by a Web server, typically selling or analyzing data for customers via the web. These data analysis tools are built so that they can be used by 3rd party products such as mobile apps and other web sites. They enable companies to create new revenue streams at near-zero cost by monetizing their existing services. To a business, a REST API is like a white-label product or service they can offer to clients.
Some companies that offer APIs include:
- Google Maps
- SalesForce
- Amazon
- Twitter, and
- Yahoo! Finance.
Many other companies use APIs internally to streamline development on multiple platforms such as iPhone, Android, and Desktop. For example, Facebook and LinkedIn use APIs so that their engineers have access to the same data regardless of what platform they are programming for. That way, deploying upgrades is much easier because the majority of changes are centralized in the API instead of numerous platforms.
APIs also allow for a snappier, more interactive user experience. Instead of loading everything upfront, an app or website can download important content on startup, then passively download only necessary content from APIs as a user interacts with the user interface, such as by scrolling and clicking on buttons and links.
For example, when scrolling through news headlines on a blog or news site that seems to have never-ending content. The news site queries an API for ten or twenty headlines when the site loads. As the user scrolls down, the site queries the API again for another ten or twenty headlines, inserting them at the end of the page and allowing the user to continue scrolling. Each time the user nears the bottom of the page, the site queries the API again and inserts more content, making the website content seem endless.
REST APIs allow for faster development cycles, faster and more interactive software products, and better separation of content and presentation on any platform.
When correctly implemented, REST APIs are highly standardized, so much so that most modern software can read and write data to REST
APIs out of the box, allowing developers to focus on how to best use the data rather than how to connect, authenticate, and download it. When correctly implemented and documented, it can create a practically free passive revenue stream by monetizing a company's existing work.
Chapter 1
Introduction to REST APIs
What is a REST API?
REST APIs are a standard way of providing access to data from a web server to clients such as mobile apps, web sites, or Internet of Things devices. They can allow clients to create, remove, update, or delete data, or to provide some type of data analysis.
Typically, people do not interact with REST APIs directly. Instead, people interact with client software on their phone, browser, or another device, which in turn requests and modifies data from one or more REST APIs in the background.
REST API is typically programmed with a web scripting language such as Python, Ruby on Rails, or PHP, which interacts with a database or other APIs. The REST API is accessible by URL, just like a website. Except instead of requesting a website with a web browser, another software client is requesting data. The scripting language is configured to run through a web server such as Apache or NGINX so that requests can be processed and responses can be delivered back to the client via the Internet. When a client submits a request via a URL, the server determines what action to take and how to process and present any data involved. It responds with a status code, some formatting headers, and structured data.
The client retrieves this data and converts it into something useful to the program or its users. The server treats each request as a new, unique request, meaning it does not store session variables or maintain any state information about the client. A REST API may have many clients and a client talk to many REST APIs. The software that powers a REST API may also be a client for other REST APIs.
The data stored on a REST server is called a resource . This can be any type of data, from time-sensitive data such as stock quotes, real-world data such as contact information, documents, images, audio, or anything else. In some cases, the actual resource data is stored in a database on the server, but with REST this resource is typically represented in JSON, XML, or some other common format that is compatible with HTTP. JSON is the most popular.
The same resource can be represented in either JSON or XML, for example details of the book "War and Peace" by Leo Tolstoy represented in JSON:
{
"title": "War and Peace",
"author": "Leo Tolstoy",
"published_year": "1869",
"num_pages": "1225"
}
Example 2.1: Details of the book "War and Peace" represented in JSON
The same book resource represented in XML:
Leo Tolstoy
1869
1225
Example 2.2: Details of the book "War and Peace" represented in XML
More information about the JSON data format can be found at http://json.org .
Using a common representation and the REST protocol, a client can access or modify a resource using a REST interface. Many REST APIs exist to perform a variety of tasks, from payment processing, marketing, finance, travel and logistics, sports, weather, and more.
A few interesting APIs include:
- Stripe (stripe.com ): allows developers to integrate credit card processing into their website so they can create an online store.
- From Data With Love (fromdatawithlove.com ): predicts demographic information based on peoples name and job title.
- SkyScanner (skyscanner.com ) provides flight prices and route tracking information.
- OpenWeatherMap (openweathermap.org ) provides historical and current, and forecasted weather data.
- Yahoo! Finance (yahoo.com ) provides stock quotes.
- DataDemograph (datademograph.com ) provides demographic information for US addresses.
- Yelp (yelp.com ) provides photos, menus, reviews, and hours of operations for restaurants.
- Rebrandly (rebrandly.com ) creates custom tiny URLs.
Who Uses REST APIs?
Although REST is typically human-readable, it is designed to be used by other software known as a Client. REST APIs are made so that other software developers can add functionality to their software and apps. REST services are client-agnostic, meaning that any type of client software can interface with them. A web browser, a smartphone, a smartwatch, or any other software can interface with a REST server. The REST server does not know or care what type of client connects.