Trying to understand what APIs are

  • Thread starter Thread starter fog37
  • Start date Start date
AI Thread Summary
APIs, or application programming interfaces, serve as interfaces that allow applications to utilize the functionality of other software without exposing the underlying complexities. They are essential in web development, enabling applications to communicate with servers and other services, such as social media platforms. In programming, APIs can be found in libraries and modules, which simplify tasks for developers by providing predefined functions and methods. The conversation highlights the distinction between APIs and modules, emphasizing that APIs define the interface for interaction while modules contain the actual implementation. Understanding APIs is crucial for both application developers and those who create APIs, as they form the backbone of modern software development.
fog37
Messages
1,566
Reaction score
108
TL;DR Summary
Understand what APIs are...
Hello,

I understand that APIs are ubiquitous and stand for application programming interface. The keyword, for me, is interface.

In essence, an API is something that allows one to use the functionality of another application, software, etc. through the API, by using the API instead. So APIs are the frontend while the software they are liaison for are the backend.

In Python, modules, libraries made by other to help the programmer create GUIs, do image analysis, etc. seem to also be APIs, in a sense, because they hide all the intricacies and details of that certain library/module/etc allowing the user to make calls to specific functions in the library or module...

Is my understanding correct? APIs exist everywhere. A web developer uses APIs to build WEB applications. Those APIs are developed by other developers who also use APIs...

So anything that interfaces with something else and makes using that something else easier would be an API. When we use our smartphones to connect to Twitter or Facebook or any other website without using a computer and a browser, those apps we have on our phones are actually APIs, correct?

Thank you!
 
Last edited by a moderator:
Technology news on Phys.org
APIs are the functions/methods a software application calls upon to ask the underlying hardware to do some task. Its more of a contract between the application and the operating system the application is running on.

https://en.wikipedia.org/wiki/API

As an example, I want to read a file. The API contract says I need to open the file, read its contents, and then close the file. In python, this would look something like this:

[CODE lang="python" title="sample file reader in python"]
#!/bin/python

my_file = open("my_file.txt","r")
my_data = my_file.readlines()
my_file.close()

for my_line in my_data:
print(my_line)
[/CODE]

Notice, there is also a possible order to the calls ie I must open the file before I can read it and I must open it before I can close it. Of course, it makes sense that I ought to read it before I close it too. So there's a recipe aspect to APIs as well.

The API contract also separates programmers int two camps:
- those that write applications
- those that write APIs

Of course, experienced programmers understand both sides and often develop their code so that portions of it can become API oriented, stored in a library for future use by other programmers and programs.

Sometimes APIs are developed as frameworks where your application follows a recipe and gets embedded in the API. As an example, a C program is embedded when you write your code in the main() function:

C:
#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

If you don't start your code by writing it inside a main() then the OS can't find it when it's run.

What goes on under the covers roughly are:
- initialize the application ie initialize any dynamically linked libraries
- setup the environment
- gather the command line arguments if any
- ...
- then call the main() function ie your program

Lastly, API's are like the layers of an onion and will sometimes make you cry as you peel back the API to understand how it works. This is how programmers learn to use it more effectively.
 
Last edited:
  • Like
Likes sysprog
jedishrfu said:
As an example, a C program is embedded when you write your code in the main() function

I'm not sure the term "API" is intended to describe this. It's simply how the C language requires you to write programs. The API in the C program you give would be the printf function; it's the API that the C standard library provides for writing text to the console provided by the OS.
 
jedishrfu said:
So there's a recipe aspect to APIs as well.

Also, APIs can change over time. For example, since (IIRC) Python 2.5, Python added a new API for opening and reading from files:

Python:
with open("my_file.txt","r") as my_file:
    my_data = my_file.readlines()

for my_line in my_data:
    print(my_line)

The older API that you describe is still available, but the new one above is preferred because it guarantees that the file gets closed; you don't have to call the close method manually any more. This is considered an improvement because it eliminates possible bugs.
 
That might have been the case when C was first developed, although programming models have developed since then to incorporate this same kind of scheme to embed your code in a framework.

Callbacks are a similar scheme where your application registers functions or classes with a framework that it is running and later gets called to execute some action when needed. As an example, you register for a button press and when the user presses it, your function is called to handle the action.

https://en.wikipedia.org/wiki/Callback_(computer_programming)
 
fog37 said:
When we use our smartphones to connect to Twitter or Facebook or any other website without using a computer and a browser, those apps we have on our phones are actually APIs, correct?
No, the APIs are typically implemented on server side. An app in your phone can be a client that uses an API published by an server.
 
fog37 said:
When we use our smartphones to connect to Twitter or Facebook or any other website without using a computer and a browser, those apps we have on our phones are actually APIs, correct?

I would say the apps use APIs, not that they are APIs. Twitter and Facebook have APIs that the apps use; the apps also use APIs provided by your smartphone's OS for things like displaying data on your phone's screen and accepting input from you.

This is also true if you use Twitter or Facebook on a regular computer with a browser; the browser (or rather the Javascript code loaded by the browser) is using APIs to communicate with Twitter's or Facebook's servers, just as the smartphone app does (in fact they will largely be the same APIs), and is using other APIs provided by your computer's OS to display data on your computer's screen and accept input from the computer's keyboard and mouse.
 
  • Like
Likes sysprog and lomidrevo
Thank you everyone!

I see how apps on smartphones use APIs to connect to internet servers and download/upload information.

Back to Python, for example, I would say that the module Tkinter for creating GUIs represents an API in the sense that it was developed by somebody to allow the user to create GUIs through classes, methods, etc. without having to deal with the granular details under the hood.

I guess I could say the same for all Python modules since the simplify the user's life and hide the complexities underneath. For example, the request library is for transferring information from the web. The library scikit-learn for machine learning modules would also seem to be an API as it offers the user the ability to create ML models without having to built those models from scratch!
 
I wouldn't say that the python modules themselves are APIs. Instead, it makes more sense to say that each such module is implemented to provide some functionality (or program), and the API consists only of those classes/methods/functions which are exposed to the user, so he/she can call them to make use of module's in-built program.
 
  • Like
Likes fog37 and sysprog
  • #10
fog37 said:
Thank you everyone!

I see how apps on smartphones use APIs to connect to internet servers and download/upload information.

Back to Python, for example, I would say that the module Tkinter for creating GUIs represents an API in the sense that it was developed by somebody to allow the user to create GUIs through classes, methods, etc. without having to deal with the granular details under the hood.

I guess I could say the same for all Python modules since the simplify the user's life and hide the complexities underneath. For example, the request library is for transferring information from the web. The library scikit-learn for machine learning modules would also seem to be an API as it offers the user the ability to create ML models without having to built those models from scratch!
You need to be more careful about the terms that you use. Use the correct choice of "programmer" versus "user". Also, use the correct choice of "API" versus "module". Otherwise, the thread conversation may get hopelessly confusing.
 
Last edited:
  • Like
Likes fog37
  • #11
The term API most commonly means the specification of software procedures or functions that a company or product offers the public, whether free or for money. They are black box to the user/ caller, who cannot see the source code, so they both provide a standard interface for that product and hide it's internal workings from the user for reasons of safety or ownership.

Langauge libraries are like APIs but sometimes you can get there source code, and on OOP, you can extend them.
 
  • #12
jedishrfu said:
The API contract also separates programmers int two camps:
- those that write applications
- those that write APIs
The API is an interface. It is entirely possible to write the API before any of the application code or library code has been written.
An API can be expressed in code or in the documentation - or both. In C++ code, it is expected to be encoded in a header (*.h) file.

jedishrfu said:
Sometimes APIs are developed as frameworks where your application follows a recipe and gets embedded in the API. As an example, a C program is embedded when you write your code in the main() function:
Function main() is called from the "Runtime Environment". Also, I would avoid describing this as an embedded application since an "embedded application" is the term used when the computer is a dedicated component of a larger machine.
 
  • Like
Likes FactChecker
  • #13
harborsparrow said:
The term API most commonly means the specification of software procedures or functions that a company or product offers the public, whether free or for money. They are black box to the user/ caller, who cannot see the source code, so they both provide a standard interface for that product and hide it's internal workings from the user for reasons of safety or ownership.

Langauge libraries are like APIs but sometimes you can get there source code, and on OOP, you can extend them.
Another important reason to define the interface without specifying the implementation code is to allow high-level programmers to use tools without worrying about the implementation. That way, they can write code that will work on different machines where the library is implemented differently on each machine / operating system.
 
  • Like
Likes fog37, jtbell, PeroK and 1 other person
Back
Top