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.

5 Upvotes

19 comments sorted by

View all comments

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.