r/AskComputerScience • u/ConwayK9781 • Jan 24 '20
How does one actually make an API?
Hi everyone!
To clarify, I know what an API is. I use APIs. And maybe this is a super obvious question (because I can't find an answer on Google for this, only what an API is), but I for the life of me can't figure out how one would actually make an API.
For example. I have a program called Program_A. Program_A is running. I have another program, Program_B, that I want to be able to use a method in Program_A. What would I do to Program_A that would make calling it's function an API that Program_B could use?
If anyone could even give me a high level explanation that would be great, I don't really need an exact technical example (although I'm not opposed to that either). I'm just looking to understand how it works generally, because I really can't figure it out.
8
u/asdff01 Jan 24 '20 edited Jan 24 '20
In the case of a web app making a call to a backend application, the front end will make an asynchronous http call to the backend application. The backend application (my experience is with ASP.NET) is running on a web server which routes the request to the program, which is set to perform an action (method) upon receiving this request at a certain URL.
The backend may then need to request that a database return the result of an operation that it describes. The backend will then make an asynchronous call (or not, as it will likely be able to handle, or is multithreaded such that it will be equipped to handle, additional processes during this time) to the database. Upon this process completing, it will respond to the request from the front end with an HTTP response with the requested information.
The front end will receive this information, then most likely will proceed to process this information in a callback function or set of commands attached to the asynchronous call. In JavaScript, this is a promise, and in other languages asynchronous operations are handled with async/await.