Well, yes, ish. It’s just a more general thing than that, it’s a very broad thing, it’s not specific.
So I’ll have a look at what you’re trying to do in a bit, but just in an attempt to clarify (sorry for long post again):
When you write code that is to be used by other developers (or you): a programming language, or a framework, or a library or even a single class or function. Or, say, a web interface, or the code for some hardware device. Just basically anything that needs to be used programmatically.
When that is done, you want to hide how it works. To use another analogy, a TV. The internal workings of it aren’t exposed, you don’t have to fiddle around with the circuits to just turn it on or off. You just press a button. Click on. Click off.
An API is just that – the programmatic controls that are exposed so you can do {the things} with whatever you’re using.
This is what I mean by it being much more general and non-specific than I think you understood it to be. It’s just the {things} made available by {thing} that you can use to interact programmatically with {thing}.
So in a web context, “API” is often used to refer specifically to the controls provided by some web service to let you, as a programmer, get access to some resources on that service’s system.
This is the same as I’ve described above. What I think obfuscates understanding is how it’s talked about – colloquially, you “access an API”, as if this is a special kind of system. But this is what I alluded to in a previous post: you use an API. That’s semantics, but I think it’s important. It’s not a mysterious thing.
An API can be defined in an almost infinite number of different ways. There are naturally common patterns, architectures, protocols etc. that developers use because if everyone just did their own thing, it would be almost impossible, as a developer, to get anything done.
So, example, anyway:
Say you have an application that has some stuff stored in a database, say types of domestic cat. You have some function that takes a string (a key), and when you run the function it looks up whatever is under that key in the database. And it returns that data as a string – under the hood, it tells the dB system to make a query, gets the result, converts it to JSON.
function getCatInfo (catType) {
const dbResponse = myCatDatabase.query(catType);
// Bleep bloop bloop, do some stuff
return JSON.stringify(dbResponse);
}
And that’s ok, works on your computer. You’ve got the database and you’ve written the code and you can look up cats and stuff. You can just run the file with the function – something like node ./look-up-cats.js persian
But you want to put this on the web and let everyone look up cats.
So you buy a domain name, cats.info
, and you find somewhere to host an app. Then you look up how to make your cat app available on the web. And you figure out that, install Express or whatever in your app, and you start it up. And what you end up with is something that listens for HTTP requests and responds with something else. You type a URL into your browser (https://cats.info/persian
), the URL points to the domain that your app is hosted on, your app is running and reads that request, it takes the incoming URL, splits the string, grabs the persian
bit, gives it to your getCatInfo
function and gets the result, sends that back as a text, says the type is JSON, you get that back in the browser.
So the API the functional bit of the app is a function called getCatInfo
. The API for the made up database library I used in the function includes a function called query
. The web framework has an API – if it’s Express, if you go to the Express docs, there’s instructions on the functions you use to work with Express. Node has an API, it has a standard library it ships with, loads of functions for doing various things. The browser that made the request has an API – maybe the request was made with JS, and used one of the API functions fetch
. Maybe it wasn’t, maybe it just used an HTML form element, which has an API. And the API for the overall web service, that’s not functions, that’s basically just a list of URLs and what you get by typing in those URLs – https://cats.info/someCat
will give you some data about someCat
, that’s the API