Trying to understand what APIs are

  • Thread starter fog37
  • Start date
In summary, APIs exist everywhere and are the functions or methods a software application calls upon to ask the underlying hardware to do some task. They are a contract between the application and the operating system the application is running on.
  • #1
fog37
1,568
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
  • #2
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:

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)

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
  • #3
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.
 
  • #4
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.
 
  • #5
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)
 
  • #6
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.
 
  • #7
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
  • #8
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!
 
  • #9
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

Related to Trying to understand what APIs are

1. What is an API?

An API (Application Programming Interface) is a set of protocols, tools, and definitions for building software and applications. It allows different programs, systems, and applications to communicate with each other and share data.

2. Why are APIs important?

APIs are important because they enable developers to build complex and innovative applications by accessing data and functionalities from other programs and systems. This allows for faster and more efficient development, as well as the integration of different technologies and platforms.

3. How do APIs work?

APIs work by sending a request for data or functionality from one system to another. The request is usually formatted in a specific way, such as using REST (Representational State Transfer) principles. The receiving system then processes the request and sends a response back, usually in the form of data in a specific format, such as JSON or XML.

4. What is the difference between public and private APIs?

Public APIs are accessible to anyone and can be used by developers to build applications. They are usually provided by companies or organizations to allow others to access their data or services. Private APIs, on the other hand, are only accessible within a specific organization or for a specific group of users. They are often used for internal communication between different systems and applications.

5. Are there different types of APIs?

Yes, there are different types of APIs, such as REST APIs, SOAP APIs, GraphQL APIs, and more. Each type has its own set of protocols and standards for communication and data transfer. The type of API used will depend on the needs and requirements of the application being built.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Computing and Technology
Replies
8
Views
458
Replies
2
Views
915
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top