Is there still a difference between a library and an API?
Asked Answered
C

11

63

Whenever I ask people about the difference between an API and a library, I get different opinions. Some give this kind of definition, saying that an API is a spec and a library is an implementation...

Some will tell you this type of definition, that an API is a bunch of mapped out functions, and a Library is just the distribution in compiled form.

All this makes me wonder, in a world of web code, frameworks and open-source, is there really a practical difference anymore? Could a library like jQuery or cURL crossover into the definition of an API?

Also, do frameworks cross over into this category at all? Is there part of Rails or Zend that could be more "API-like," or "libraryesque"?

Really looking forward to some enlightening thoughts :)

Cuspidor answered 9/9, 2010 at 16:45 Comment(1)
"Is there still a difference between a library and an API?" Answer: Very much so!Uveitis
D
69

My view is that when I speak of an API, it means only the parts that are exposed to the programmer. If I speak of a 'library' then I also mean everything that is working "under the hood", though part of the library nevertheless.

Dues answered 9/9, 2010 at 16:48 Comment(3)
Excellent distinction and a great point in how we think about writing reusable code.Cuspidor
Today not only libraries have APIs, i think that's why this confusion happen.Enlarger
I dismissed this answer at first because I thought APIs were only a communication interface. I didn't know the definition includes ANY interface, even a "code interface", so to speak. Knowing that, this answer is spot on.Rachaba
L
28

A library contains re-usable chunks of code (a software program).

These re-usable codes of library is linked to your program through APIs (Application Programming Interfaces). That is, this API is an interface to library through which re-usable codes are linked to your application program.
In simple term it can be said that An API is a software interface that facilitates the interaction between two software programs..

enter image description here

For example, in procedural languages like C, the library math.c contains the implementations of mathematical function, such as sqrt, exp, log etc. It contains the definition of all these functions.

These function can be referenced by using the API math.h which describes and prescribes the expected behavior.

That being said, an API is a specification (math.h explains about all the functions it provides, their arguments and data they return etc.) and a library is an implementation (math.c contains all the definitions of these functions).

Loophole answered 19/1, 2015 at 15:32 Comment(1)
After a lot of searching, this answer makes the most sense. There are a lot of opinions out there, with very few backed up by examples.Spindly
E
17

API is part of library that defines how it will interact with external code. Every library has API, API is sum of all public/exported stuff. Nowadays meaning of API is widened. we might call the way web site/service interact with code as API also. You can also tell that some device has API - the set of commands you can call.

Sometimes this terms can be mixed together. For example you have some server app (like TFS for example). It has API with it, and this API is implemented as a library. But this library is just a middle layer between you and not the one who executes your calls. But if library itself contains all action code then we can't say that this library is API.

Enlarger answered 9/9, 2010 at 16:48 Comment(1)
The best simple definition "API is part of library that defines how it will interact with external code". Thank you.Mosesmosey
D
12

I think that Library is a set of all classes and functions that can be used from our code to do our task easily. But the library can contain some of its private functions for its usage which it does not want to expose.

API is a part of library which is exposed to the user. So whatever documentation we have regarding a library, we call it an API Documentation because it contains only those classes and functions to which we have access.

Dummy answered 6/9, 2012 at 12:4 Comment(0)
D
3

we have first to define an interface ...

Interface :is the means by which 2 "things" talk to each other and exchange information. "things" could be a (1) human or (2) a running code of any sort (e.g. library ,desktop application , OS , web service ... etc).

if a human want to talks to a program he need Graphical user interface (GUI) or command line interface (CLI). both are types of interfaces that humans (but not programs) would like to use.

if however a running code (of any sort) want to talk to another running code (of any sort) it doesn't need or want a GUI or CLI ,it rather need an Application Programming Interface (API).

so to answer the original poster question : library is a type of running code and the API is the means by which this running code talk to other running codes.

Dude answered 20/11, 2015 at 17:24 Comment(0)
N
0

In Clear and concise language

Library: Collection of all classes and methods stored for re-usability

API: Part of library classes and methods which can be used by a user in his/her code.

Nadiya answered 12/10, 2013 at 13:55 Comment(0)
D
0

According to my perspective, whatever the functions are accessible to invoker , we can called as api in library file, library file having some of the functions which is private , we cannot access them ..

Doglike answered 29/12, 2014 at 6:41 Comment(0)
O
0

There are two cases when we speak or think of API

  1. Computer program using library
  2. Everything else (wider meaning)

I think, that in the first case, thinking in terms of API is confusing. It's because we always use a library. There are only libraries. API without library doesn't exist, while there's a tendency to think in such terms.

How about The Standard Template Library (STL) in C++? It's a software library.

You can have different libraries with the same API, meaning set of available classes, objects, methods, functions, procedures or whatever terms you like in some programming language. But it can be said, that we have different implementation of some "standard" library.

Some analogy may be that: SQL is a standard but can have different implementations. What you use is always some SQL engine which implements SQL. You may follow only standard set of features or use some extended, specific to that implementation.

And what "under the hood" in library is not your concern, except in terms of differences in efficiency by different implementations of such library.

Of course I'm aware, that this way of thinking is not what is a "generally binding standard". Just a lot of new terms have been created, that are not always clear, precise, intuitive, that brings some confusion. When Oracle speaks about Collections. It's not library, it's not API, it's a "Collections Framework".

Outsail answered 14/10, 2020 at 3:17 Comment(0)
C
0

Without using technical terms I would like to share my understanding regarding API and library.

The way I distinguish 'library' and 'API' is imagining a situation where I go to a book library. When I go there, I request a book which I need to a 'librarian' without knowing how a entire library is managed.

I make a simple relation between them like this.

  • Library = A book library which has a whole system and staffs to manage books.
  • API = A librarian who provides me a simple access to a book which I need.
Colombia answered 4/10, 2022 at 1:57 Comment(0)
L
0
  • Library: Think of it as a toolbox. It’s got all these pre-written codes (like functions, methods, classes, and whatnot) that you can use in your own code. You call these functions whenever you need 'em. Libraries are installed on your system, and you can use its functions, classes, interfaces (like Set, List, etc.) in your own code. These form the library’s API.
  • API (Application Programming Interface): This is like the rulebook for how software components should interact. It lays down the law on how to use a service, whether that service is provided by a library, a server, or some other component. The nitty-gritty details of that service are usually hidden from the user. While a library’s API lets you use the library’s code in your own program, other types of APIs let your program interact with code running elsewhere. For example, a web API lets your program interact with a service over the internet. The API defines what requests your program can send and what responses it can expect to receive. But the actual code that implements this functionality isn’t part of your program - it’s running on a server somewhere else.
Lashing answered 14/1 at 6:32 Comment(2)
"But the actual code that implements this functionality isn’t part of your program - it’s running on a server somewhere else". This is not always the case, and an example to this would be this: "Options API and Composition API of Vue.js". 1. Options API: This is the classic Vue.js API where options (like data, methods, computed, etc.) are provided to a Vue instance. It's straightforward and familiar for many developers. 2. Composition API: Introduced in Vue 3, this API provides a set of APIs to create and manage reactive state and lifecycle hooks within a setup function.Hudspeth
A few more notes to take into account: 1. Library Location: While libraries are often installed on your system, they can also be loaded remotely, such as from a CDN in web development. 2. API Scope: The term "API" isn't limited to external services like web APIs. It broadly refers to any defined way of interacting with a piece of software, which includes the interfaces provided by libraries.Hudspeth
A
-1

They are completely unrelated concepts.

A library is a set of resuable code.

An API is the part of a program or library that lets external agents interact with it.

Arctogaea answered 31/12, 2023 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.