Date: Mon, 12 Mar 2018 09:00:00 +0000
<p>Many people have a vague or incorrect idea of what the fairly common term "API" means. Heads up: it's not a type of beer! Petr lays out the basic details of an application programming interface in plain English so you'll never be confused again.</p> <p>Written by Petr Gazarov: <a href="https://twitter.com/PetrGazarov">https://twitter.com/PetrGazarov</a></p> <p>Read by Abbey Rennemeyer: <a href="https://twitter.com/abbeyrenn">https://twitter.com/abbeyrenn</a></p> <p>Original article: <a href="https://fcc.im/2FHPHer">https://fcc.im/2FHPHer</a></p> <p>Learn to code for free at: <a href="https://www.freecodecamp.org/">https://www.freecodecamp.org</a></p> <p>Intro music by Vangough: <a href="https://fcc.im/2APOG02">https://fcc.im/2APOG02</a></p> <p>Transcript: </p> <p>Before I learned software development, API sounded like a kind of beer.</p> <p>Today I use the term so often that I have in fact recently tried to order an API at a bar.</p> <p>The bartender’s response was to throw a 404: resource not found.</p> <p>I meet lots of people, both working in tech and elsewhere, who have a rather vague or incorrect idea about what this fairly common term means.</p> <p>Technically, API stands for Application Programming Interface. At some point or another, most large companies have built APIs for their customers, or for internal use.</p> <p>But how do you explain API in plain English? And is there a broader meaning than the one used in development and business? First, let’s pull back and look at how the web itself works.</p> <p>WWW and remote servers</p> <p>When I think about the Web, I imagine a large network of connected servers.</p> <p>Every page on the internet is stored somewhere on a remote server. A remote server is not so mystical after all — it’s just a part of a remotely located computer that is optimized to process requests.</p> <p>To put things in perspective, you can spin up a server on your laptop capable of serving an entire website to the Web (in fact, a local server is what engineers use to develop websites before releasing them to the public).</p> <p>When you type www.facebook.com into your browser, a request goes out to Facebook’s remote server. Once your browser receives the response, it interprets the code and displays the page.</p> <p>To the browser, also known as the client, Facebook’s server is an API. This means that every time you visit a page on the Web, you interact with some remote server’s API.</p> <p>An API isn’t the same as the remote server — rather it is the part of the server that receives requests and sends responses.</p> <p>APIs as a way to serve your customers</p> <p>You’ve probably heard of companies packaging APIs as products. For example, Weather Underground sells access to its weather data API.</p> <p>Example scenario: Your small business’s website has a form used to sign clients up for appointments. You want to give your clients the ability to automatically create a Google calendar event with the details for that appointment.</p> <p>API use: The idea is to have your website’s server talk directly to Google’s server with a request to create an event with the given details. Your server would then receive Google’s response, process it, and send back relevant information to the browser, such as a confirmation message to the user.</p> <p>Alternatively, your browser can often send an API request directly to Google’s server bypassing your server.</p> <p>How is this Google Calendar’s API different from the API of any other remote server out there?</p> <p>In technical terms, the difference is the format of the request and the response.</p> <p>To render the whole web page, your browser expects a response in HTML, which contains presentational code, while Google Calendar’s API call would just return the data — likely in a format like JSON.</p> <p>If your website’s server is making the API request, then your website’s server is the client (similar to your browser being the client when you use it to navigate to a website).</p> <p>From your users perspective, APIs allow them to complete the action without leaving your website.</p> <p>Most modern websites consume at least some third-party APIs.</p> <p>Many problems already have a third-party solution, be it in the form of a library or service. It’s often just easier and more reliable to use an existing solution.</p> <p>It’s not uncommon for development teams to break up their application into multiple servers that talk to each other via APIs. The servers that perform helper functions for the main application server are commonly referred to as microservices.</p> <p>To summarize, when a company offers an API to their customers, it just means that they’ve built a set of dedicated URLs that return pure data responses — meaning the responses won’t contain the kind of presentational overhead that you would expect in a graphical user interface like a website.</p> <p>Can you make these requests with your browser? Often, yes. Since the actual HTTP transmission happens in text, your browser will always do the best it can to display the response.</p> <p>For example, you can access GitHub’s API directly with your browser without even needing an access token. Here’s the JSON response you get when you visit a GitHub user’s API route in your browser (https://api.github.com/users/petrgazarov).</p> <p>The browser seems to have done just fine displaying a JSON response. A JSON response like this is ready for use in your code. It‘s easy to extract data from this text. Then you can do whatever you want with the data.</p> <p>A is for “Application”</p> <p>To close off, let’s throw in a couple more examples of APIs.</p> <p>“Application” can refer to many things. Here are some of them in the context of API:</p> <p>A piece of software with a distinct function.</p> <p>The whole server, the whole app, or just a small part of an app.</p> <p>Basically any piece of software that can be distinctively separated from its environment, can be an “A” in API, and will probably also have some sort of API.</p> <p>Let’s say you’re using a third-party library in your code. Once incorporated into your code, a library becomes part of your overall app. Being a distinct piece of software, the library would likely have an API which allows it to interact with the rest of your code.</p> <p>Here’s another example: In Object Oriented Design, code is organized into objects. Your application may have hundreds of objects defined that can interact with one another.</p> <p>Each object has an API — a set of public methods and properties that it uses to interact with other objects in your application.</p> <p>An object may also have inner logic that is private, meaning that it’s hidden from the outside scope (and not an API).</p> <p>From what we have covered, I hope you take away the broader meaning of API as well as the more common uses of the term today.</p> <p> </p>