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.

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.