What is an API?
API stands for Application Programming Interface, let’s break that down:
Interface – Every device we use has some kind of interface. Your TV has a remote with buttons on it, while a light switch has an even more straightforward interface- on or off. We use these interfaces to get the device to do what we want.
Application Programming – The Application Programming bit is easier to understand. An API is how applications talk to each other. All software that you can interact with through code has some form of an API.
An application programming interface is a set of rules that define how computers, applications, or machines can talk to each other. You can think of it this way: the typical user interface is intended for use by a human being, while APIs are intended for use by an application or computer.
How do web APIs work?
You may not know this but your web browser has plenty of APIs built into it that we can use! These are called Web APIs.
Some APIs let your app view weather forecasts, show the latest sports scores, respond to video game controllers, and lots more!
These are APIs that sit between your code and make requests to data sources or functionality on a server that you’d like to access.
The API generally does two things:
- It sets rules for interacting with it
The “rules” are the API saying “Structure your request like this, and I’ll send you data structured like this.” Badly structured requests result in errors.
- It handles data transfer between the server and the code making the request. The API is a program that acts as a middleman between the web app and the server and database
Once it receives a valid request, it will run. Depending on what you ask for, it might return an image, some data, or just let you know that it successfully received your request.
Ok so just to recap. APIs consists of 3 basic steps:
- The initial request. Normally via a URL
- The API handles the rules for interacting with the server/database
- The results that return. Normally as JSON
Working with JSON
Most HTTP APIs will take and receive data in the JSON (JavaScript Object Notation) format. It makes learning how to read and write using this format a pretty essential skill when working with APIs. JSON keeps its data in key-value pairs
This is an example of a JSON output:
{ “name”: “John”, “age”: 30, ”car”: null }
It defines an object with 3 properties, name, age, and car.
Some API concepts
Endpoints – Simply put, an endpoint is one end of a communication channel. When an API interacts with another system, the touchpoints of this communication are considered endpoints. For APIs, an endpoint can include a URL of a server or service. Each endpoint is the location from which APIs can access the resources they need to carry out their function.
APIs work using ‘requests’ and ‘responses.’ When an API requests information from a web application or web server, it will receive a response. The place that APIs send requests and where the resource lives are called endpoints.
Authentication – Some APIs require you to sign up for an account or obtain a unique key to access their information. It might be to block anonymous traffic, prevent abuse of the service, or because they want to charge a fee for the data.
With authentication, you pass the API a secret key that identifies a specific user or application request. The server can then determine if you’re able to access the data or not.
HTTP Verbs – With each HTTP request created, there is always an ‘HTTP Verb’ that goes along with it. The most common are POST, GET, PUT, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively.
Troubleshooting
Eventually, you’ll run into some trouble with APIs, and you’ll need to debug a problem in your code. If that happens, here are some helpful tips.
Check the documentation – There should be documentation to tell you how to structure your request. Make sure you’re following the syntax described there. Compare their examples to what you have in your request to see what’s different.
Check the network tab – If you’re making your API requests in the browser, one of the best API troubleshooting tools is the Network tab. open DevTools. Click the Network tab at the top. Now the Network tab will listen to every single request that the website makes.
Check the status code – You’ve seen “404 File Not Found” on a website when you clicked a dead link or typed something incorrectly. That 404 is a specific code that the server gives to your browser as feedback on its request. It’s one of many HTTP status codes that help us understand how our requests are being received. The responses are grouped into hundreds:
- 1xx informational response – the request was received, continuing process
- 2xx successful – the request was successfully received, understood and accepted
- 3xx redirect – further action needs to be taken in order to complete the request
- 4xx client error – the request contains bad syntax or cannot be fulfilled
Conclusion
While some APIs can be incredibly complex, the underlying notions remain the same, hopefully, this helps to take some of the mystery away. We touched on some key concepts and explained what makes APIs such powerful tools in web development.
As always practice is important and there are many free resources and tutorials out there to help you continue your API adventures.
Good luck talking with robots!