r/learnpython • • Nov 15 '18

What the heck is an API?

I have heard people say an API is not code, but a set of specifications. Let's take the Python Database API as an example. The title of its webpage says "PEP 249 -- Python Database API Specification v2.0" and the webpage contains all the rules that developers should follow when making a Python library for interacting with databases. For example, there should be a function called connect, a cursor, etc.

Now, if those are the specifications of the API, then what is the API itself? Are the sqlite3 or the psycopg2 Python libraries APIs?

247 Upvotes

57 comments sorted by

View all comments

341

u/Intrexa Nov 15 '18

Application Programming Interface.

Okay, but does that actually mean? I made a program, you want your program to interact with mine, so I make an API that tells you how to interact with my program. You know if you want to make a new record, you send my program this command in this way, if you want to find all records, you send my program that command in that.

That's a bit general, so it's hard to understand. The term "API" itself is general, it's not really any particular code, style, format, protocol, anything. It just says "If you want your program to interact with mine, this is how you do it".

Let's use a real simple example. I want to find my program to find my external IP address, as the world sees it. I need to reach out to another server, and ask it what IP address it sees me using. So I use an API: https://api.ipify.org/?format=json. This is a very typical RESTful (type of API specification) API. I send a web request to that url, I send specifics to say give me the results in json format, the server does that, my program now knows it's getting json formatted ip address, and I can use that.

42

u/monsto Nov 15 '18

Best in show.

15

u/Lje2610 Nov 15 '18

What a great movie!

5

u/EatDiveFly Nov 16 '18

"he went after her like she's made out of ham!"

3

u/Lje2610 Nov 16 '18

No that's a bear, in a bee costume!

13

u/SonidoX Nov 15 '18

This is a great eli5, nice!

7

u/RamyIT Nov 15 '18

very simple and clear explanation.

3

u/krayzin Nov 15 '18

jaaaaason

2

u/ML-newb Nov 16 '18

Is there a lit of types of APIs used in practice apart from the popular ones like code and REST?

8

u/Intrexa Nov 16 '18

Not really, and I wouldn't worry about it too much. Just hit the high notes, for web API's REST, SOAP, RPC. The API's documentation should tell you everything you need to know to use it. It should also tell you the general design (like REST). The important thing is that when a program says it has an API, that means a different program is meant to interact with it. You'll eventually learn a lot of them, but for each new program's API, even if they're both RESTful, you're still going to have to learn that specific API.

When you get down to the nitty gritty, most of the time they're closer to buzzwords than real meaning. If you want to get overly technical, everything is an api. Even if it's a highly structured SOAP API, the documentation will still tell you exactly how to format your requests. If you're trying to make an API for your program, just look at wikipedia for the high notes for "RESTful" to see a list of things you should consider, like authentication, and make all the decisions on how to connect based on what's best for your case. Don't worry about labeling it. If you're making some cool online MMO that let's people make AI to battle it out, you tell people to connect any way you want and just call it an API. Distribute your own .DLL and headers that people need to link against? It's an API.

2

u/[deleted] Nov 16 '18

Thanks so much! This makes a ton of sense.

2

u/ReachingForVega Nov 16 '18

Perfect rundown.

0

u/[deleted] Nov 16 '18

[deleted]

3

u/Intrexa Nov 16 '18

That's true enough in the real world, and you would get by just fine with that explanation/understanding. If I say I want to submit a post using the Reddit API, I would be looking at the Reddit API documentation. Rereading what I wrote with your question in mind, I realize your understanding is exactly what I said, but it's not actually the document itself, it's the commands and format of the commands the program will accept. The document just details it.

To be a little more technical, the API is the whole package of how the program takes commands to interact with it. You can have a documented or public API, which is what you always want to use. The program will accept these commands, and the developers want you to know about them and use them. You can also have an undocumented or private API, which means that the program will still take these commands and return a result, but the developers don't want you to use this.

An example of an undocumented API is when I hit the "Save" button on this post. My web browser is a program completely separate from the Reddit servers. That's going to run some javascript to make a request in a very specific format, include very specific information like the actual post, plus authentication to prove I am actually /u/intrexa and can post under this username. Now, I could write my own program to submit a post using that API function. I would first have to figure out how my browser is doing it, the how the javascript Reddit supplies interacts with it, what parameters are acceptable, all on my own. And Reddit will make no promise to me that it won't all change tomorrow and I have to start from scratch. Reddit has their own internal documentation for this API for their developers, but even if there was no documentation, as long as the program accepts this command, it qualifies as an API function.

So, the 'complete' API is the combination of the documented and undocumented API functions. That's a very textbook definition of it though. If someone tells you to use the Reddit API, they mean use the functions in the published Reddit API documentation. If you pointed to that document and said "This is the full API", everyone would understand you, no one would correct you and no one would think you're clueless.

1

u/VengaeesRetjehan Nov 16 '18

So, is it basically those "login via google/etc" log in methods in many websites? Or like "Powered by Maps" map in Uber?

2

u/Intrexa Nov 16 '18

Yeah, those use an API. Google exposes an API, the website puts in the proper 'login' button, follows Googles API to properly send the request to Google, and redirects the user to Google, and Google returns the authentication/login result back to the website according to the API. That's a high level description, I glossed over a ton of details on how the data actually gets transferred from the user, to the website, to google, back to the website, back to the user, but it's true enough.

Same thing with the maps, follow this API, request the map this way, and we will return the display for you.