API =/= Database
Published: 2020-06-11 21:45:13

We use databases and APIs most days, if not every day, when online, but we often consider an API the same thing as a database, or use the terms interchangeably. 

When I was first learning about APIs the teachers I had didn't make any distinction between the two, outside of the general definition, and this caused a misconception that can cause a lot of confusion when trying to communicate about which part of an application is causing an issue.





# What is a Database?
A database is an organized collection of data/structured information. (2) There are many types of databased, but they are all generally stored electronically (though some hard copies can be kept depending on the need) and are usually controlled by a database management system (DBMS). Furthermore, when databases are combined with their associated applications become the _database system_, that we commonly refer to as "the database".

![Database Icon](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTyY3FvR8ZdjbkmMH8aLaBK1p2pScbce0MwtA96q26Cg4U8jdp2&usqp=CAU)

As said, there are many types of databases but what are they?:
- Relational database
- Object-oriented database
- Distributed database
- Data warehouse
- NoSQL database
- Graph database
- OLTP database

When looking at a database that an application uses it sits a ways away from the application itself. As you can see below, the database is not the application nor the API.

![Database Flow of Info to API](https://dev-to-uploads.s3.amazonaws.com/i/hdp2ptnfpegu2siudhay.png)

Consider a LAMP (Linux, Apache, MySQL, PHP) software stack where MySQL is just the database software sitting on a server. Though yes, an admin or user could log into the database software on the computer and interact with it but applications have a harder time doing this. This is where APIs come in.










# What is an API?
An API (Application Programming Interface) is as it name says; a Programming Interface for Applications.
It is a set of routines, protocols, and tools for building applications; and specifies how software components should interact. (1)

Web APIs are a software-to-software interface commonly used to build lightweight, resource-oriented services over HTTP that can leverage features of the HTTP protocol, use versioning, cache control for browsers, and more.

![API Icon](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcS5Ui5FmX4NArkP1nOAadCgNC4De7q_a27Zzumzwv_R29OcxQOV&usqp=CAU)

Now that we know what an API is it makes since that the web API sits between the database and the application, as seen below, to handle the routes and HTTP protocols.

![API Interface with Database and Users](https://dev-to-uploads.s3.amazonaws.com/i/nudna6j6qul3fj3ul7f4.png)








# Where do They Fit Together?
* Developers can communicate with databases by writing queries to access data, change tables, etc. 
* APIs enable communication between an application a developer built and a database management system. 

![API Communications vs Developer](https://dev-to-uploads.s3.amazonaws.com/i/gnj4qg3u9kyvjwpkwc9s.png)

_It is important to understand that internally an API still uses the same DB Query to get data from the database and the queried response is the same, that a developer would use if interacting with a database directly. But, the difference is in the way an API VS a developer make the DB Query requests, with each endpoint, and also in the format of the response._

![REST API Design](https://mjawaid.files.wordpress.com/2010/06/restful-api-design-620x263.jpg)











# Why Would You Want to use an API to Talk to a Database Rather Than Just Query The Database?

There are two main reasons that a developer would want to use an API to communicate with their database rather than querying the database directly: security and ease of development.

With the cloud computing and serverless trend rising, if a developer wants an application to tap into a particular database, using an API would be the ideal way to accomplish this. It doesn't matter to a API if the requesting app is a web app, a mobile app or a desktop app as long as the request is a valid request. 
An API is just an additional layer of abstraction on the DB Query, which allows the developers to not care about the kind of DB used, separately develop the frontend and the backend without worrying about breaking everything through versioning, and allows developers to not need communicate with the database directly (SQL statements are not really that fun).

![REST API Database Communication](https://www.parallels.com/blogs/ras/app/uploads/2019/07/rest-api-300x209.png)

Furthermore, providing direct access to database is not a good idea. If a malicious user has direct access to the database tables they can do pretty much anything with the data. An API allows for controls to be put into place easily to ensure that only valid users have access to the database and that only valid requests can modify the correct data. _(Note: these are not default and need to be set up but it is easier through an API)_

![End Point Interaction](https://developer.ibm.com/apiconnect/wp-content/uploads/sites/23/2017/11/Screen-Shot-2017-11-10-at-11.46.22-AM.png)






<hr>

Happy Hacking
![Happy Hacking](https://media.giphy.com/media/l3vRmVv5P01I5NDAA/giphy.gif)

### Resources:
1. https://www.webopedia.com/TERM/A/API.html
2. https://www.oracle.com/database/what-is-database.html
3. https://www.altexsoft.com/blog/engineering/what-is-api-definition-types-specifications-documentation/
4. https://www.infoworld.com/article/2905918/choosing-the-right-technology-for-building-your-service-layer-in-net.html
5. https://guides.rubyonrails.org/api_app.html
6. https://www.semantics3.com/blog/why-a-data-api-is-better-than-a-database-50ebed8eb827/
7. http://www.agiledata.org/essays/implementationStrategies.html


###### Please Note: that I am still learning and if something that I have stated is incorrect please let me know. I would love to learn more about what I may not understand fully.