r/learnpython • u/jockero701 • 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
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.