API vs Protocol: what is an API

Hello,
I am trying to understand what an API is, how it is different from a protocol, etc.
In the context of a client communicating with a server, the two programs talk to each other using a series of protocols stacked over each other like HTTP, TCP, etc.

Then there are API…where does that protocol or set of rules fit? programs sending information requested and consumed by other programs…

To get more practical, let’s say I write a Python script…how would I use an API? Would I write some code using functions that the API provides to connect to the remote computer?
Can we build API? Why? Can we not use the existing ones that websites make available?

Thank you!

Welcome to the community.

Let’s assume that you are referring to Swift and iOS Development as opposed to the reference to Python in your question.

API (From Apples documentation):

An API or application programming interface, is used to pass data back and forth between software apps in a formalised way. Many services offer public APIs that allow anyone to send and receive content from the service. APIs that work over the internet using http:// URLs are referred to as web APIs. On the web, you send a request to an API to get and post information.

All your favourite apps that connect to the internet are powered by APIs. For example, social media apps use APIs to let you view and publish posts from their mobile apps. When you refresh the feed, the app makes an API request to fetch all the posts. When you “like” a post, the app makes an API request to post that data.

With the ability to make API requests in Shortcuts, you can go beyond the data that’s locally available on your device and make shortcuts that interact with any web service that has a public API. You can build custom shortcuts to gather a large amount of data and display it however you want or even send data back into the API and update the web service — if it’s possible in an API, it’s possible in the Shortcuts app.

Protocol (from the Swift programming language documentation):

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

In addition to specifying requirements that conforming types must implement, you can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can take advantage of.

Hope that helps.

It might be wrong, but I like to think APIs are like “pre-coded functions” that are made available on a server, and that you get to call from your device via the Internet and often via the HTTP protocol.

A protocol is just how the communication between your phone and the server is formatted. Like, when you send a postal letter we all agree that the address should be written on the enveloppe - that’s kind of a protocol.

To take Chris Parker’s social media example, we could imagine there’s a “function” to like a post, a “function” to retrieve the latest posts to refresh your feed… So your phone kind of “sends a letter” (aka a request via HTTP protocol) to the server saying “hey I want to call the function to like this post” (PUT request of the API)

I emphasize that it might be wayyy oversimplified but yeah that’s kinda how I picture it all in my mind lol

1 Like

Thank you CalStark! I appreciate your reply. The “pre-coded functions” idea make things more clear.

To paraphrase what you said and make sure I get things right:

a) A client and a remote web server are just computer programs talking to each other using a number of protocols, like TCP, IP, HTTP. When we request a webpage from a web server, the client sends a HTTP GET request to the server which then responds with the markup resource rendered on the screen by the browser (client).

A server-side web API is a program stored on the same physical machine as the remote web server. The API program contains simple functions that a client can use to to indirectly interact with the server. There are really 3 programs: the client program (could be the app we develop, a local python script, etc.), the API program, the remote server program.
For example, in Python, there is a visualization library called seaborn which is said to be an API for another viz library called matplotlib. We can use use seaborn instead of matplotlib (using matplotlib would give us more granular control though). The seaborn functions are really “wrappers” for matplotlib functions which would be more complicated to use…The same thing happens for an web API: the request that the client would need to formulate for the server would be to complicated. The API functions make the job easier…

b) In regards to the data the API exchanges, json and XML are data formats for tabular data. What if the client was requesting from the API unstructured data like images or audio files? How would unstructured data be sent back to the client? json would not be a viable choice…

c) The scenarios where APIs are NOT involved still use URLS and HTTP, just like API calls…APIs have URLs and URLs are IP address. Is the IP address of an API the same as the IP address of the web server the API is connected to? What about the concept of port? Do APIs have ports too? I don’t think so. The port will be a port on the web server…I guess the port number must be specified somewhere in the API call…

d) When we download a pdf file from a website clicking on a like on a webpage, the browser uses HTTP to request the pdf from the web server. The response is the pdf file itself and not a markup document…Is an API involved in that process? I guess I cannot think that an API is always involved when the shared resource is not a markup document, correct?

Thank you again for any input!

Yeah I think you’re getting most of it, although there’s LOTS to unpack :sweat_smile:

I highly recommend this YouTube talk from Harvard’s David J Malan, it helped me get a high level understanding of most of this stuff in just half an hour, and it might help you too.

It’s not specifically about APIs per se but it’s definitely helpful to understand internet technologies.

First video should be enough but if you have time there’s this other one too where he also goes over ports, it’s an hour long but it goes into more details.

Hope it helps