DEV Community

gyi2521
gyi2521

Posted on

API == server?

Recently, I ran into a YouTube video explaining API. The video used 'API is like a server in a restaurant' as an analogy, so I wanted to share that analogy here on my blog.

Have you ever wondered exactly how we get all the information that we ask for on internet? How we make a reservation or buy things with just a few clicks? The behind scene hero is API. It stands for Application Programming Interface.

API is the messenger that takes requests from us and tells a system what we want, and then returns the responses back to us, just like a server in a restaurant. We, the customers, order food from a menu to a server, and the server takes the order to the kitchen and then brings back what we ordered to the table.

If we apply this to a code, it will be like this:

    //api call - ordering food
     $.get('/api/order')
       .then(function (food) {
           `put on the table`;
     });


    //api - server taking order
    app.get('/api/order', function (req, res) {
        `make food`(kitchen)
        return food;
    });

Have you ever booked your flight using an online travel service? It's similar to ordering food in a sit down restaurant. We enter our desired criteria for our travel (like food order) on the travel service website, and then the travel service website calls available airlines' API (like a server) to search for the flight information that matches our criteria from their system (like kitchen). Once the matching flight information are found, API returns the information (like delivering food) back to the online travel service via response. The travel service website displays the flight information for us to compare and choose the best option.

So the next time you think of an API, just think of it as your server running back and forth between applications, databases, and devices to deliver data and fulfill your request.

By the way, I was once an API... back in college days... and it helped me pay my tuition...:-)

Thanks for reading!

Top comments (4)

Collapse
 
vjnvisakh profile image
Visakh Vijayan

Cool and fun

Collapse
 
tux0r profile image
tux0r

I was once an API

How could anyone access you? Was there an open port?

Collapse
 
gyi2521 profile image
gyi2521

Ha Ha, anyone came to the restaurant i worked at had an access... ;-)

Collapse
 
tux0r profile image
tux0r

Sounds suspicious, but not quite uninteresting. ;-p