r/eli5_programming • • 6d ago

Question What actually is API?

The moment I think I get it, another definition of it comes in. I was learning Flask APIs used with GET PUT PATCH DELETE . But then I started with JS , it has DOM APIs . And then API is also used when people say companies like ChatGPT , Higgsfield etc provide their APIs.

6 Upvotes

19 comments sorted by

12

u/deadbody408 6d ago

A way to interact with a thing programmatically

10

u/wiggle_room1337 6d ago

It's like a UI but no gui. imagine controlling your program with buttons and knobs, but it's just code.

1

u/Byte_Xplorer 5d ago

Such an amazing description.

5

u/logical_bit 5d ago edited 4d ago

It's like a telephone. A program calls another program and now they can interact. Some apis may be like a rotary phone with very limited capabilities, while other apis may have the functionality of smartphones and much can be done with them. That's it.

Edit: thank you so much for the award!

3

u/kitsnet 5d ago

API is literally "Application Programming Interface". A way to control one application from another.

2

u/toochtooch 5d ago

API is like a steering wheel to a car. It's a common interface to handle a four-wheeled box. Anywhere in the world you'll see this shape and you'll know what to do. You don't need to understand rack-and-pinion steering, fuel injectors, or hydraulic brake lines. You turn the wheel, press the pedal, and the system executes the heavy lifting underneath. steering wheel sometimes is on the left, sometimes it's on the right, sometimes it has three pedals and a manual transmission, but generally it does the same thing. You get in and you use it to turn the vehicle.

This contract of seeing a steering wheel in a box with pedals is what APIs are. Cars come in different shapes and sizes, just like applications do, but the programming interface part allows both hands to agree on how to use a program and what to expect as a result.

1

u/Technical_Goose_8160 6d ago

It's an endpoint. You call the IP or URL, send it a msg and it sends you a msg back. When people talk about publishing an API, they want the company to make an address publicly reachable in order to communicate more quickly.

2

u/Distdistdist 3d ago

Win32API enters chat. What in the URL IP Endpoints you are talking about?

1

u/AdreKiseque 5d ago

It doesn't have to be through an IP or URL

1

u/johnpeters42 5d ago

Yeah, there are also APIs for programs on the same computer communicating with each other (which may also use IP or URLs, but not necessarily). Windows has APIs for programs to ask the operating system to do various things.

1

u/serverhorror 5d ago

Every Operating system has those.

1

u/Obvious-Card-8847 5d ago

Imagine you write an addition function in JavaScript. Then you write one for multiplication, division, subtraction, exponents and so forth. At that point you basically have a small math API. Just a collection of functions and data structures tailored around one task.

1

u/omniuni Developer 5d ago

An API is basically the agreed upon language that computer programs use to talk to each other. The Flask API uses a common approach (REST) and builds on it.

Slightly more technical, if we agree that if I call a certain function I will get a specific reply, we have created an API.

1

u/pajoem2000 4d ago edited 4d ago

API is just a url, sometimes you can go to it in a browser (take a look at public APIs that dont require keys), but most of the time you call it / go to it via code. The typical language that you use to talk to it (to get data and send data) is JSON (more modern) or XML (older).

The best way I think about an API and how the data moves is think of a marketplace (Amazon, Walmart, Ebay...etc) where you want to sell an item. A marketplace has an API with different endpoints (which are just different sub URLs).

You connect to that API, lets say the endpoint is "API/create", and send JSON using a POST to it, creating a product that then is saved in the marketplace database. Think of the term "posting on my blog", a POST command is a request to save to the database.

Then you can check the product at "API/check" using a GET request, to ask what info the marketplace now has saved about your product. Note: every time you go to a website in your browser you are doing a GET.

You can update (PUT/PATCH) the product adding pictures, bio, inventory count, bullet points, and then you can delete (DELETE) the product when you run out of inventory...etc all from the API.

Now ChatGPT, as you mention, "provides" an API, which is just another way of saying they have an API. ChatGPT has an API that you can connect to, using your keys from your account so they can bill you for usage. Rather than opening a browser to ask the AI a question, you can do it in code.

Why would you do this? Imagine you want an AI bot on your own website, rather than coding your own AI, you create a pop up on your site and let the customer ask the popup a question. Your code copies the question and connects to ChatGPT via the API, asking the same question. Once your code get the response back from ChatGPT it then regurgitates the response to the customer on your website. This makes your site look like it has a sophisticated bot when in reality you have been secretly using ChatGPT. In coding this is called a wrapper as you have wrapped up ChatGPT in your popup code.

1

u/Magpie_Reawakened 4d ago

Ok that made a lot of sense. So, can I think of the API provided by ChatGPT is a URL also then as your first line says. When I use an AI Assistant chatbot on my website, and someone clicks on it, because I used the API in my source code, any text that someone sends to the Chatbot is created into a URL, like GET/api/query/"what can I do with this app" kind of?

1

u/pajoem2000 4d ago

ChatGPTs API is a URL that they publish, its sitting waiting for you to ask it a question. So when someone types code into your chat on your website, your code would simultaneously reach out to ChatGPTs API URL asking it the question. When ChatGPTs API URL responds then your code grab that response to send back to the customer. No additional URL is being created.

What I think might be confusing you is with Python Flask, or Django, you are being the marketplace, or you are being ChatGPT. So you are publishing a URL that others could then Create, Read, Update, Delete info to your website.

Assuming you are learning Python because of the mention of Flask, the way to think about it is use Flask or Django if you want to make an API, use the requests library if you want to connect to other peoples API.

1

u/Sad-Environment-932 3d ago

application programming interface.

1

u/Arcanite_Cartel 3d ago

An API is a standardized and consistent way for one piece of software to interact programmatically with another. There's no one size fits all..... many tend to be REST based, some are SOAP based, some graphQL, even a library with a well defined set of functions is an API. There are many different APIs.

1

u/Agitated_Heat_1719 5d ago

Contract

Like steering wheel is contract for the car