What is a web Application Programming Interface?
Many common sites provide APIs. Why are they needed, and what function do they provide?
How does one access a web API?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityImagine you want to write a program that counted the number of friends you have on facebook whose names start with the letter B.
When you, as a person, interact with facebook, you go to "facebook.com" in your web browser and click on links and pictures. To perform this friend-counting task, you might go to your "friends" page, navigate to the "B" section, and then count the number of names you see.
But how do you make your program do the same thing? Technically, there are ways to make your own computer code "click" on things, or "navigate" accross different pages, but this is a large amount of work for what would ideally be a pretty simple problem.
An API is almost like a website designed specifically for interaction by programs, not humans. To see your list of friends as a human, on a pretty web page, you click the link which takes you to www.facebook.com/Chris/friends
. Facebook sends you back that webpage. But if you were to visit (fake example URL) www.facebook.com/API/Chris/friends
, you get back a much different result. Instead of a webpage with images, links, chat boxes, and lots of other display- and interaction-related extras, you get back a very plain, but very well-structured, block of text. Something like this:
[
{
"name": "Adam Smith",
"id": "176399948"
},
{
"name": "Bill Gates",
"id": "8462993451"
}
]
This text describes the friend list in a manner that is easily understood and internalized by a program. It's a special notation for storing data (this one is called JSON, but there are many others), and most programming languages have functions to convert it into variables accessible from your program.
So in your program, to print a single friend's name you might say:
api_result = get_webpage("www.facebook.com/API/Chris/friends")
friend_list = convert_json_to_data(api_result)
first_friend = friend_list[0]
print("One of my friends is named ", first_friend{'name'})
As you can see with friend_list[0]
, the content of the webpage is now a real array (or list, or whatever) of data, accessible and traversable through our programming environment.
So, in summary, and "Application Programming Interface" is... an interface allowing you to access an application (like facebook, microsoft word, etc.) using a programming language. A more clear name might be "Programming Langauge Interface to an Application". "API" refers to that whole other "site" designed for programs to access.
APIs often don't just let you see data, but alter it as well. The process is very slightly different usually, but it basically comes down to visiting a different address, like this one (to add a freind for Chris): www.facebook.com/API/Chris/add_friend?new_friend_id=34564573
, or alternately, the same URL using a different method (like an HTTP POST to www.facebook.com/API/Chris/friends
instead of a GET)
Technically that description I gave you only describes so-called "web APIs", which are APIs available on the internet through different URLs, and which usually correspond to websites. But the general idea applies to any API. It's just a well-structured and predictable way to interact with someone else's computer program using your own computer program. API is a really general term. In fact, it isn't even limit to refering to interaction with a stand-alone "application", but really just... any programming interface. So the "print" function above is part of an "API" which allows writing of text to some screen or file.