An Introduction to APIs for the Uninitiated
APIs or Application Programming Interfaces are, like their name implies, an interface to an application that allows other systems to interact with them programmatically. In this post, we'll discuss some foundational concepts and distill the lexicon associated with APIs into tangible knowledge.
Client-Server Model
Request and Response
A typical interaction across the web can be shown using the Client-Server model. In this case, the client, such as web browser, mobile app or other program, makes requests to a server to retrieve data such as web page or data. The server then responds to the client with either the requested data or some sort of error message.
This communication occurs using a protocol, such as HTTP, which defines the format of the request and response. The following example shows a bit of what this looks like under the hood.
This is an HTTP request. It is requesting (e.g. GET
) a resource (e.g. /articles/index.html
)
from the server (e.g. jdkaplan.com
).
> GET /articles/index.html HTTP/1.1
> Host: jdkaplan.com
> User-Agent: curl/7.71.1
> Accept: */*
This is the response. It is a 200 OK
response, which means that the request
was successful. The response body is the HTML for the requested page.
What is shown here is just the metadata - the part of the response that tells
us information about the response such as the status (success or fail), content type,
and content length. The actual response body would follow this and contain
all of the data requested, in this case an HTML page.
< HTTP/1.1 200 OK
< Content-Type: text/html
< Content-Length: 27726
< Connection: keep-alive
< Date: Mon, 07 Jun 2021 20:26:07 GMT
< Last-Modified: Mon, 07 Jun 2021 00:54:26 GMT
< ETag: "a6e163063f3137d5db457cfabb097736"
< Accept-Ranges: bytes
< Server: AmazonS3
< Vary: Accept-Encoding
< X-Cache: Miss from cloudfront
< Via: 1.1 f9ae6e33f293d8a4e80f48fca6093c68.cloudfront.net (CloudFront)
...
For a deeper dive into this topic, read the Background section of my post on web development basics.
Server Rendered Applications
This is how web pages are typically served. When you type a URL into your web browser, the browser makes a request to the server. The server responds with some HTML, or Hypertext Markup Language, which tells the browser what to render on the page.
You can see a simple example example of this in my previous article on web development basics. You can also do this interactively by opening up the developer tools in your browser and exploring the network tab.
Asynchronicity
In the previous example, the client makes a request and waits for the response before doing anything else. This is called a synchronous request. In practice, this is not how things work. Instead, the client makes a request and when the response comes back, the client is notified and can handle the response. This is called an asynchronous request.
This is commonly used by synchronously requesting a web page which in turn runs some Javascript which makes asynchronous requests to the server to retrieve additional data needed to render the application.
To learn more about asynchronous Javascript, read my introductory post on the topic or take a deeper dive with my Javascript Asynchronicity Patterns article.
Historical Note
Server-rendered applications are still widely-used these days, but there has been a major paradigm shift over the last decade or so (as of this writing) towards
single-page applications, which tend to be more common. In the single-page application approach, the server returns a simple HTML page and a large Javascript bundle that dynamically renders the application.Data Formats
Web servers can send data in a number of different formats. In the examples above, we saw HTML, the language used to render web applications. When data is being exchanged between applications, it is often sent in formats more geared towards data exchange.