What's API. In English,Please.

Author :@Petr Gazarov
Agu13. 2016


Artwork by Phillip Blackowl. Spanish translation. Portuguese translation

Before I learned software development,API sounded like a kind of bear.

Today I use the term so often that I have in fact recently tried to order an API at a bar.

The Bartender's response was to throw a 404:resource not found.

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.

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.

But how 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.

WWW and remote servers

When I think about the Web,I imagine a large network of connected servers.

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.

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).

When you type www.facebook.com into your browser,a request goes out to Facebook's remote server.Once your computer receives response,it interprets the code and displays the page.

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.

An API isn't the same as the remote server---rather it is the part of the server that receives requests and sends responses.

API as a way to server your customers

You've probably heard of companies packaging APIs as products.For example,Weather Underground sells access to its weather data API.

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.

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.

Alternatively,your browser can often send an API request directly to Google's server bypassing your server.

How is this Google Calendar's API different form the API of any other remote server out there?

In technical terms,the difference is the format of the request and the response.

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.

If your website's server is making the API request,the your website's server is the client(similar to your browser being the client when you use it to navigate to a website).

From your users perspective,APIs allow them to complete the action without leaving your website.

Most modern websites consume at least some third-party APIs.

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.

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 microserives.

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.

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.

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.

{
  "login": "petrgazarov",
  "id": 5581195,
  "avatar_url": "https://avatars.githubusercontent.com/u/5581195?v=3",
  "gravatar_id": "",
  "url": "https://api.github.com/users/petrgazarov",
  "html_url": "https://github.com/petrgazarov",
  "followers_url": "https://api.github.com/users/petrgazarov/followers",
  "following_url": "https://api.github.com/users/petrgazarov/following{/other_user}",
  "gists_url": "https://api.github.com/users/petrgazarov/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/petrgazarov/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/petrgazarov/subscriptions",
  "organizations_url": "https://api.github.com/users/petrgazarov/orgs",
  "repos_url": "https://api.github.com/users/petrgazarov/repos",
  "events_url": "https://api.github.com/users/petrgazarov/events{/privacy}",
  "received_events_url": "https://api.github.com/users/petrgazarov/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Petr Gazarov",
  "company": "PolicyGenius",
  "blog": "http://petrgazarov.com/",
  "location": "NYC",
  "email": "petrgazarov@gmail.com",
  "hireable": null,
  "bio": null,
  "public_repos": 23,
  "public_gists": 0,
  "followers": 7,
  "following": 14,
  "created_at": "2013-10-01T00:33:23Z",
  "updated_at": "2016-08-02T05:44:01Z"
}

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.

A is for "Application"

To close off,let's throw in a couple more example of APIs.

"Application"can refer to many things.Here are some of them in the context of API:

  1. A piece of software with a distinct function.
  2. The whole server,the whole app, or just a small part of an app.

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.

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.

Here's another example:In Object Oriented Design,code is organized into objects.Your application may have hundreds of objects define that can interact with one another.

Each objects has an API-a set of public methods and properties that it uses to interact with other objects in your application.

An object may also have inner logic that is private,meaning that it's hidden from the outside scope( and not an API).

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.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容