What is an API

what is an API

You’ll have seen the API acronyms more than once and twice when you read software articles. But not everyone knows what an API really is. That’s why we make it clear in this article. The first thing to say is that it stands for Application Programming Interface. And many times it generates confusion even among those who know something about programming.

For example, Arduino has its own programming API, offering a number of functions that you can use in Arduino IDE or other environments to program the microcontroller to allow you to create your projects. But… could you tell me the difference between a programming library and an API? are there differences between a framework and an API? is an API the same as an ABI? There are many doubts that perhaps continue to generate confusion and that right now we are going to make clear.

I don’t know if you know that low level languages, such as assembly or ASM, depend directly on the hardware architecture, while high level languages are abstracted from the hardware to facilitate the programmer’s task, but they depend on the operating system (syscalls) or certain APIs, etc. So that all this does not sound Chinese to you, let’s see what it is about…

What is an API?

An API is a tool that provides developers with a set of functions, subroutines and procedures or methods for the POO that can be used through existing libraries. Among what an API offers, there are functions to create simple apps, functions related to AI, with the generation of graphics, management of hardware resources, etc.

For example, you will surely remember well known APIs such as the one offered by Linux through the glibc library, or the graphics ones like OpenGL and Vulkan, or also others like OpenCL for heterogeneous computing, OpenXR for virtual and augmented reality, etc. Other operating systems and software also include their own APIs so that other programmers can create addons, plugins or modules for that system, etc.

Example with Arduino

If you have an Arduino board and you frequently use Arduino IDE, or any other Arduino development environment, you will know that when you create a code you make use of multiple options to command your microcontroller to do a series of actions. For example, pinMode() is a typical function to configure an Arduino pin, right?

When you type pinMode(9, INPUT), to give an example, you are indicating that pin 9 of the Arduino board should work as an input, that is, the microcontroller will be waiting for the information coming through that pin to read it and perform an action. But have you ever wondered how it is able to understand that command?

Well, Arduino has a developmental API that is available to us. You can add third party libraries to this API as we have seen in many examples in this blog. Like SparkFun’s libraries for certain sensors, etc. You can then enter the functions in the Arduino IDE and it will translate the code properly to load it into the microcontroller’s memory for processing.

If we didn’t have such an API you couldn’t create these programs for Arduino in such a simple way, since we should try to make them in assembly code for the ATMega328P microcontroller, i.e. in ASM for the AVR architecture. And that would be much more complicated, since you would have to use directly the instructions available by this ISA. If so, not only would you have to know the repertoire of this ISA properly, but also other aspects such as the number of records, etc. In other words, you should have low level knowledge of the hardware you are working for.

For example, the ASM code that you should generate for an LED to titrate in a loop would be

<br data-mce-bogus="1">

.ORG 0x0000
; the next instruction has to be written to
address 0x0000
rjmp START
The reset vector: jump to "main"
START:
ldi r16, low(RAMEND) ; set up the stack
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16
ldi r16, 0xFF
; load register 16 with 0xFF (all bits 1)
out DDRB, r16
write the value in r16 (0xFF) to Data
Direction Register B
LOOP:
sbi PortB, 5
switch off the LED
rcall delay_05
cbi PortB, 5
; wait for half a second
switch it on
rcall delay_05 ; wait for half a second
rjmp LOOP ; jump to loop
DELAY_05:
; the subroutine:
ldi r16, 31
load r16 with 31
OUTER_LOOP:
; outer loop label
ldi r24, low(1021) ; load registers r24:r25 with 1021, our new
init value
ldi r25, high(1021) ; the loop label
DELAY_LOOP:
; "add immediate to word": r24:r25 are
incredmented
adiw r24, 1
if no overflow ("branch if not equal"), go
back to "delay_loop"
brne DELAY_LOOP
dec r16
decrement r16
brne OUTER_LOOP
ret
; and loop if outer loop not finished
return from subroutine

While thanks to the API the facilities are total, by writing at high level the following equivalent code (much shorter and more intuitive):

<br data-mce-bogus="1">

int ledPin = 13; // LED on pin 13
   void setup(){
   pinMode(ledPin, OUTPUT); // p1n 13 will be a digital output
}
void loop(){
   digitalWrite(ledPin, HIGH); // Turn on the LED
   delay(1000); // 1 second pause
   digitalWrite(ledPin, LOW); // Turn off the LED
   delay(1000); // 1 second pause

Differences with an ABI

API vs Linux ABI

ABI is a less known term, it is a ‘string’, or Application Binary Interface. In this case it is an interface between modules of a program, usually between a library or a machine language operating system for the architecture in which you are: SPARC, AMD64, ARM, PPC, RISC-V, etc.

The ABI determines how to call the functions, the binary format that can understand the machine you are compiling for or the calls to the system, how exceptions are handled, how data is passed, etc.

Differences with a Framework

A framework is more a set of tools at your disposal to help the development of a given project. The framework usually sets some coding standards, provides useful components, etc. For example, JUnit is a framework for Java, or Symfony/Cake for PHP, etc.

Differences with an SDK and NDK

SDK is a Software Development Kit. It goes beyond what a framework or an API is. An example could be Android Studio or iOS xCode, etc. For example, in the first of these, in addition to the Android API itself, an IDE or integrated development environment for programming and compiling, libraries, tools, etc. is also included.

On the other hand, NDK (Native Development Kit) is an add-on. For example, Android NDK allows developers to reuse C/C++ code by introducing it to applications through JNI (Java Native Interface) .

Differences with a library

Glibc diagram Wikipedia

Finally, the library is a collection of reusable source code that makes programmers’ lives easier. For example, inside the stdio.h library in C there is a printf function to print out a text on the screen. For that to be possible you need source code that makes the operating system do that task. But since this is something very recurrent that is used frequently, simply invoking that library you can make use of printf without writing all the code from scratch. That is, in other words, you can see them as prefabricated blocks.

A library and an API can be easily confused, in fact, libraries are wrapped inside an API. Example glibc .

I hope that after this you have a clearer idea of what is an API, ABI, a framework, an SDK and a library, besides being able to differentiate between them from now on.

Leave a Comment

*

*