what is an api feature image

What is an API and how does it work?

The weather app on your smartphone can tell you the weather for any place on the planet. How? Your phone doesn’t store all the weather data for the whole world. That would be a crazy amount of data. And even if your phone could store it all, that would be a big waste of space since you only want to know the weather for a tiny part of the planet.

What actually happens is your weather app “talks” to someone that does have all that data and just gets the bit that you need, using an Application Programming Interface, or API. Let’s break that down:

Application: A computer program.

Programming: Instructions.

Interface: A way for one thing to interact with another.

An API is a way for one computer program to send instructions to another computer program, even if it’s on a completely different computer. APIs are a huge part of software development.

APIs and you

You use APIs all the time. Microsoft Word uses your computer’s storage API to find files. Instagram uses your phone’s camera API to take pictures. The Facebook website uses Facebook’s database API to load your user profile and store your posts.

The Internet is full of APIs that let websites and apps get data from other places: stock prices, traffic updates, social media posts, sports statistics โ€“ almost anything you can think of.

The weather app on your smartphone probably talks to the API run by OpenWeather, or Dark Sky, or AccuWeather. Those in turn might talk to the API run by the National Weather Service. Mint.com uses major banks’ APIs to display all your accounts in one place. When you’re driving and say “Hey Google, take me to the gas station,” your Android smartphone turns your speech into a request to Google Maps’ API.

Some APIs charge a fee, but many are free. Why would a company just give away data? Because it gets people to use and trust the company’s products and build up potential customers.

How do APIs work?

When you go to iextrading.com/apps/stocks/AAPL to look up Apple’s stock price, IEX’s computers respond with not only the stock price, but also an image of IEX’s logo, a bunch of news headlines, graphs, fonts, colors, and a pile of code โ€” everything needed for your computer display a decent-looking webpage. It’s over 11 megabytes of data in total.

A stock ticker app on your phone doesn’t need all that stuff. It just needs the stock price. If it uses IEX’s API to look up Apple, IEX’s computers respond with only 600 bytes of data, .005% the size of the webpage.

Here’s what the API’s response might look like:

	"last_price": 219.90,
	"last_time": "2019-09-13T16:33:15.000Z",
	"bid_price": 219.89,
	"bid_size": 190,
	"ask_price": 219.85,
	"ask_size": 210,
	"open_price": null,
	"high_price": null,
	"low_price": null,
	"exchange_volume": 1776683,
	"market_volume": null,
	"updated_on": "2019-09-13T16:48:16.853Z",
	"source": "bats_delayed",
	"security": {
		"id": "sec_agjrgj",
		"ticker": "AAPL",
		"exchange_ticker": "AAPL:UW",
		"figi": "BBG000B9Y5X2",
		"composite_figi": "BBG000B9XRY4"

What is that?

At first that data might look scary. But if you take a deep breath it’s not hard to read. See if you can find Apple’s stock price in there. (Hint: it’s labeled “last_price.”)

This way of organizing data with {curly braces} and commas, called JSON, is a standard โ€” a certain way of doing things that people have agreed to use. The standard for measuring weight in the US is in pounds. There’s nothing stopping you from measuring weight in cans of kidney beans, but it would complicate your life and the life of anyone trying to communicate with you.

Most Internet-based APIs use JSON for organizing data, and another one called REST for letting people find and modify data, because it’s much easier for web developers when different APIs do things the same way.

Learn to process API data with our best Python courses, best JavaScript courses, Java courses or Kotlin courses.

APIs are a big opportunity

Making an app that uses an API is usually one of a programmer’s first projects. It’s great practice for back-end and front-end web development, and you can even make money doing it. People are willing to pay big bucks for apps that bring together and visualize different data sources in useful ways.

Written by
Robin Thomas
Join the discussion