Demystifying APIs

Have you ever heard the term “API” and wondered what it is?

What is API?

Technically, API stands for Application Programming Interface. It is a set of programming codes that facilitates data transmission between software systems. 

It is not a software program, or even some type of beer but technical specifications that describe how to exchange data from the server. The keyword is “Interface.” Simply put, API is an interface to a software application. 

API

Source: https://miro.medium.com

Example 1:

Go to https://api.github.com/users/phpcontrols/repos, and it should give you all of our public Github repositories in JSON (link) format. The URL is the public API endpoint provided by GitHub to get a list of repositories.

Without the API, one would probably have to email Github and request a spreadsheet of all this data; or scrape them using a crawler. Neither is particularly fun to do. 

Example 2:

Another example is login integrations. Let’s say your developer is attempting to input a login system with Google to your application, soon find out that OAuth can do just that, so now you can integrate that into your app. Your developer studies its API documentation and develops code to incorporate the Google Login into his/her working app. Without OAuth API provided by Google, login integration would not be possible.

Web API

While other APIs exist, such as remote procedure call (RPC), let’s focus on a specific kind of APIs — Web APIs. It’s the most popular thanks to the Internet. These APIs mainly deliver requests from web applications and responses from servers via the Hypertext Transfer Protocol (HTTP).

Developers can use web APIs to extend the functionality of their web apps. For instance, the Twitter API comes with tools for adding users’ tweets to a website. Google Maps API enables the addition of a map with an organization’s location.

RESTful API

When discussing APIs, we must mention types of data exchange standards used for APIs.

  • Remote Procedure Call (RPC)
  • Service Object Access Protocol (SOAP)
  • Representational State Transfer (REST)
  • GraphQL

Web developers will be most likely work with REST (stands for Representational State Transfer) or RESTful API. While REST can be used over nearly any protocol, it usually takes advantage of HTTP when used for Web APIs. (source: https://www.mulesoft.com/resources/api/what-is-rest-api-design)

RESTful systems support messaging in different formats, such as plain text, HTML, YAML, XML, and JSON. For a large part of web development, you will be working with data in JSON format. There is a separate post  – JSON in Plan English – you should read. 

CRUD

REST has gained massive adaptation since its invention around the year 2000 due to its simplicity – simple to create and easy to consume CRUD.

When I mention CRUD operations, I meant that we Create a resource, Read a resource, Update a resource and Delete a resource. To do these actions, RESTful APIs use HTTP requests (AKA methods or verbs) to work with resources: GET, PUT, POST, and DELETE. 

CRUD

Source: https://miro.medium.com

To create a RESTful API, one only needs to expose the Endpoint URL – URL with a domain, port, path, and/or querystring.

e.g.

GET a list of all users

https://mydomain/user/

GET user by id 123

https://example.com/user/123?format=json

For CREATE, UPDATE, DELETE, it’s similar with endpoints but with additional body data. 

HTTP method CRUD Action
GET read returns requested data
POST create creates a new record
PUT or PATCH update updates an existing record
DELETE delete deletes an existing record

Source: https://www.sitepoint.com/developers-rest-api/

GraphQL

Finally, there is a new kid in town that is gradually replacing RESTful API. It’s called GraphQL, initially created by Facebook in 2012.

Different from REST, GraphQL is a query language. GraphQL does data aggregation from multiple sources so data can be returned in a single query rather than numerous calls.

GraphQL will be another post for another time.

Recommended read:
https://www.sitepoint.com/developers-rest-api/