r/PythonLearning 5d ago

What is API?

I'm new to coding and programming languages and i frequently come across the word API. Can anyone help me understand what that is?

31 Upvotes

31 comments sorted by

25

u/atticus2132000 5d ago

API is a way for two computer systems to communicate with each other. Typically one computer is requesting information from another computer.

Where you are most likely to encounter APIs...

People are collecting data all over the world and storing that data in databases. The data could be anything you could possibly imagine--weather data, animal migratory patterns, sea levels, movie star credits, navigation information, etc. if you can think of it, likely someone out there has a database of all that information.

Let's say that you want to build an application that needs data from someone else's database (e.g. you want to make a small application that tells you what the temperature is in Austin, Texas). You would contact a company that is keeping that information (e.g. DarkSky) and create an account with them where they would give you a token and instructions on how to write a specific request to get the exact data you need. That request is usually in the form of a url (e.g. www.darksky.com/APIrequest?location=Austin;token=123). When you send that URL to the company, they will respond back with your requested query results that you can then parse and use the result to populate your program.

There are other ways to use an API. Let's suppose that you wrote a really cool program where if someone gives you the span and pitch of a roof, your program would generate a truss drawing. Now that you've created this thing, you want to allow other people to use it, so you could set up your own API receiver that could take requests from other people at their own computers that would include the variables you need for your program to run and then reply back to their requests with an image file of the drawing your program generated.

An API is a standardized method for requesting data from others or allowing other to request data from you. Since the requests and results are usually communicated through a url, you get more chances to intercept the requests and make sure they're safe without ever allowing another computer to interact directly with your database or with your program. They are sending you the parameters for a database query, but you're not actually allowing them to do the query themselves (I.e. connect to your database).

4

u/agentscientific_160 5d ago

Thanks a lot for your detailed explanation. I understand API now. Really appreciate your time and patience!

3

u/BarellTitor 5d ago

Well explained. Thank you!

7

u/FoolsSeldom 5d ago

RealPython.com has an article that will help:

APIs are used for many purposes, of which this is just one example. They have other articles on the topic.

API = Application Programming Interface. A way for programmes to talk to each other (even if they are on different systems on different sides of the planet).

1

u/agentscientific_160 5d ago

Thanks I'll look into it!

4

u/[deleted] 5d ago

[removed] — view removed comment

1

u/agentscientific_160 5d ago

Thank you for making me understand!!

1

u/Correct-Purple-9188 5d ago

Sounds awfully alot like how Gemini explained it to me before hmmmm

2

u/johlae 5d ago

The wikipedia article is quiet clear. See https://en.wikipedia.org/wiki/API.

To give you an example, Bitbucket, a Git-based source code repository hosting service owned by Atlassian, gives you a browser user interface by which you can list out repositories in a project. 20 repositories are shown at a time. At the bottom of the page there are buttons to see the next and previous groups of 20 repositories. If you want to extract the names of repositories, you have to cut and paste from the browser into notepad for quiet a while.

The API allows you to use a programming language to ask for all the repositories in a project:

```

list out all repositories in a workspace, with pagination. Newest first!

url = ( 'https://api.bitbucket.org/2.0/repositories/%s?q=project.key="%s"&pagelen=100&sort=-created_on' % (workspace, project) ) while url: url = url.replace( "//api.bitbucket.org/", "//%s:%[email protected]/" % (my_atlassian_account_email, my_api_token), ) req = requests.Request("GET", url) pre = req.prepare() resp = ses.send(pre) data = json.loads(resp.text) repositories = data["values"] for repository in repositories: full_name = repository["full_name"] name = full_name.replace(workspace + "/", "") print(name) url = data["next"] if "next" in data.keys() else None ```

This piece of code lists out all the repositories. Run it once, direct the output to a file, and you're all set with minimal cut and pasting.

2

u/johlae 5d ago

An example:

In the browser:

(as there are less than 20 repositories no buttons to go to the previous or next set of 20 are shown, also note that everything is shown, we're only interested in the names of the repositories)

API:

johlae@laptop /cygdrive/c/git/bitbucket
$ ./bitbucketListAllRepositoriesInAProject.py johlae GIT
gitdevelopercoursecobol
gitdevelopercourse
gitdevelopercoursetestsfork
gitdevelopercoursetests

1

u/agentscientific_160 5d ago

Thanks a lot, reading the wikipedia page and seeing your example really helped. Appreciate your patience

2

u/PureWasian 5d ago

A very high-level way to think of it is as a contract between two parties/programs/etc. where one is requesting something from the other.

In a very similar way to how functions work, where you have a caller that sends input args and a callee that does stuff and returns back data.

2

u/THERT4 5d ago

It's a thing that let you use other's services to put it simply! Such as import [lib] is importing a APIs

2

u/SCD_minecraft 5d ago

Let's say you and your friend both speak different languages

You speak let's say Japanese and he speaks German

If you would go and ask him in Japanese, he wouldn't understand

But both of you know English, so even that it isn't native language for neither of you, you both can use it to communicate

English here represents API; common format for 2 languages/services/server and client to communicate

Server may be written in C (German) but it can expose JSON API (English) so your python app (Japanese) can understand it and communicate

1

u/SCD_minecraft 5d ago

Ofc as you need to learn English, your app has to learn JSON; fortunately python provides built-in library json for that so hard part is out of the way

1

u/agentscientific_160 5d ago

Thanks a lot brother. This explanation was awesome!!

2

u/FreeGazaToday 5d ago

did you try to google first?

1

u/agentscientific_160 5d ago

Yes but didn't quite understand

2

u/FreeGazaToday 5d ago

try gemini...just have to come up with a good prompt....i use that all the time

1

u/agentscientific_160 5d ago

Aight sure!!

2

u/Living_Fig_6386 5d ago

API = Application Programming Interface

It is the documented way that one piece of software can interact with another piece of software to exchange data and invoke actions. It can also refer to the documentation itself.

In Python, when you "import" a library, you are using an API. You are loading up a piece of software (the library) so that you can access the functions in it. There are also web APIs, where you can format a message to a website and have it do something or return something to you (say, send a stock ticker symbol and get back the current price of the stock). Operating systems have APIs that specify how you get the operating system or services it offers to do something for you. There are all kinds, and different ways of interacting with them (which the documentation should cover).

2

u/armahillo 5d ago

API means “application programming interface”

If you understand the concept of an “object”, an API is the “surface” of that object that other programs can interface with.

A car does a bunch of stuff (literally) under the hood, but it offers a steering wheel, pedals, and levers to interact with jts function. This is like an API.

_Typically_ , when people say API they are referring to “objects” thar are other services accessed over a network, usually via HTTP. But this isnt the ONLY meaning of API that you may run into.

2

u/arivictor 5d ago edited 5d ago

Application Programming Interface

It's used commonly to refer to how you communicate with a server and request and send information to it. Reddit has a server, it gives you this frontend, then this front end makes calls to the reddit API whenever you do something (Sign in, fetch comments, load new posts). The server expects these requests in a specific format with specific information, thats the API.

GET https://example.com/
POST https://example.com/new/comment data: {"text": "hola!"} -> sends back an ID: 123
PATCH https://example.com/new/comment/123 data: {"text": "hello!"}

However, ANYTHING that enforces a contract and rules to work with it, is technically an API. The above is an API to a server/service, the below is an API for code:

class SuperCoolService:
    def __init__(self):
        pass

    def do_stuff(self):
        pass

    def do_even_cooler_stuff(self, password: str):
        pass

How you interact with this class can also be referred to as the API. You could say, explain the API to make cooler stuff happen, and I'd say, well it needs a password and the password must be a string.

Commonly - Instructions for sending requests to servers over HTTP/S

Broadly/Technically - Instructions for talking to or using a service of any kind (SDK, module, server)

2

u/rachville 5d ago

A simple way to think about it: an API is like a waiter in a restaurant. You tell the waiter what you want, they deliver your request to the kitchen, and bring back the result. You don’t need to know how the kitchen works to get your meal.

2

u/FewPotato2413 4d ago

For the most high level view of what an API is

Think of your built in function or maybe even self defined function

Lets call it mystery(), this is basically your API….

1

u/StatementFew5973 2d ago

Application programming interface.

1

u/Junior_Honey_1406 5d ago

If you are new just, i think you should focus on fundamentals api and stuff will come later on don't worry about it if you are curious just search on YouTube to find what is the stuff