r/AskComputerScience • • 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.

30 Upvotes

12 comments sorted by

24

u/jxf CS Pro (20+) Jan 24 '20

An API is essentially two things:

  • A list of what kinds of requests will be accepted. (A request is either a question you can ask, a command you can issue, or an informative hint to the receiver.)

  • A list of communications channels on which requests will be accepted.

When someone says they offer a "web API", for example, they usually mean that the communications channel that will be accepted is over HTTP. The lists of requests that will be accepted is then typically documented somewhere.

Similarly, a software library for image processing may expose an API. The communications channel is whatever the calling convention of the language is; you invoke the methods directly through your program, not over HTTP. The lists of requests that will be accepted are defined by what the library exposes or not.

You make an API by deciding both of these features — what requests you want to service, and what communications channels you wish to transact on.

1

u/ConwayK9781 Feb 01 '20

Thanks for the answer (sorry for the late reply!)

After getting so many great answers, I think my primary confusion was thinking it was more than what it is. I just knew it had to be more than this because of how big of a deal they seem to be. Guess not, lol!

Thanks again for the help, I really appreciate it!

7

u/xiongchiamiov Jan 24 '20

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?

Well that depends. If they're in the same language, you can probably structure one as a library and import it into the other. If they're not, you might need to use a common underlying system (like CFFI). Or they can talk over http, grpc, custom binary protocols, etc in which case you have to set that up. Or if this is more unix cli work, you build command-line interfaces and they communicate through stdin and stdout.

1

u/ConwayK9781 Feb 01 '20

Thanks for the answer (sorry for the late reply!)

From all the great answers I've gotten, I think I was just overthinking it. They are so simple I assumed there had to be more!

Thanks so much for answering!

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.

1

u/ConwayK9781 Feb 01 '20

Thanks for the answer (sorry for the late reply!)

With how big of a deal as they are, I expected them to be more and thought I was just not understanding something. Guess they are as straight forward as one would think!

Thanks so much for the answer!

4

u/alach11 Jan 24 '20

You can do it in around 8 lines of Python using Flask. I set one up with a Raspberry Pi on a local network in around 30 minutes of tinkering.

https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask

2

u/ConwayK9781 Feb 01 '20

Thanks for the answer (sorry for the late reply!)

I love this answer! Hands on approach. I didn't even think to look up tutorials. Thanks for providing the link!

I think I was overthinking them. I expected them to be complicated. They aren't!

2

u/Bottled_Void Jan 24 '20

Your API is essentially just a header file. You compile your 'program' as a library instead of an executable.

Then program B includes the API in its compilation and loads the library at runtime (or links it statically).

You can look up the difference between dynamic and static libraries.

2

u/ConwayK9781 Feb 01 '20

Thanks for the answer (sorry for the late reply!)

When I was trying to figure it out, this is what I thought, but I just knew they had to be more complicated than this. So I assumed I was wrong. Guess not! Thanks for answering.

2

u/OldNewbProg Jan 24 '20

You've got a lot of good answers to your post. But I like adding my 2bits. The best way to understand it is to write one. I suggest either c# or javascript. If you use javascript, you want to look up how to write a basic nodejs api. If you use c# look up writing a .net core api.

Both should be relatively simple since you know how to use an API I assume you know some programming. If the tutorial doesn't look simple, look for a different tutorial ;) Here's Microsoft's own .net core API guide. Probably not the simplest but it's from the horse's mouth.

1

u/ConwayK9781 Feb 01 '20

Thanks for the answer (sorry for the late reply!)

Always room for two more bits when it comes to learning!

It looks like I was just expecting them to be more complicated than they really are.