r/AskProgramming • u/cellwall-999 • Mar 26 '20
APIs Explained ...
I dunno but I don't understand APIs fluently. Does anyone know of an informative article or perhaps a website or just personally explain to me frankly wtf APIs are an why we need them ...
Edit: Thanks everyone for your empathetic contributions. Guess what; I have a better understanding of APIs now than I did before.
7
u/SeerUD Mar 26 '20
Your application needs data, programmatically, so you fetch that data from some remote source via the remote source's exposed interface. API - application programming interface. That's basically the what.
Interface here is the bit that's probably most confusing; the interface is really just something you know the shape of that you can "call".
"API" really is that vague by the way. APIs aren't always web services, or remote services of any kind. Libraries that you consume when building applications have an API. It's just something you consume programmatically in your application to do something. What most people talk about when they say API though is "web service" or something similar (e.g. a REST API, maybe a gRPC service, that sort of thing).
Why do you need them? Well, can your application do literally everything on it's own? Probably not. And there might be someone that's already built what you need. Maybe it's someone on your own team. So you would use an API to centralise that logic / data and use it in many places.
An example; you want to get weather information for your home town. There's almost definitely someone who already has that data, and has publicly exposed it via an API. You can use their API to fetch the data and use it in your application.
2
2
u/SnideBumbling Mar 26 '20
Well, can your application do literally everything on it's own? Probably not.
Depends on how bad the NIH syndrome is.
15
u/frostbyte650 Mar 26 '20 edited Mar 26 '20
API stands for “Application Programming Interface”
Technically, it’s just an abstraction layer of functions that perform programmic tasks.
Most of the time you hear about APIs it’s in relation to web development where there’s a front-end / back-end model. It’s referring to the back-end.
So take reddit for example. Reddit has an API that acts as the controller of data. So you have the front end (either the app or reddit.com) & when you first open the app or webpage, your device sends a request to Reddit’s API saying “hey, can I get the 20 hottest posts from r/askprogramming” and the reddit API will be like “sure, here you go!” And it sends your device the 20 hottest posts in the form of raw data containing text & your device will display that data however the front-end was designed to. Same goes for posting data, when I hit “send” on this post, my phone will send a request to Reddit’s API and be like “hey! This user replied to this post with this text” and the API will be like “thanks! I’ll add this text to the database” so when your device asks the API for all the replies to your post, it’ll send you my text that it stored in the database and your device will display it as a reply.
Generally, any time you need to deal with users modifying data you’ll need to create an API that does the actual managing of that data.
That’s what people generally mean when they refer to an “API” but it can actually refer to any abstracted programming layer.
Like, your computer itself also has an API that your applications use to manipulate states. So for example when you download something from a website, generally the way it works is your browser will request the file from the web server & then it will tell your computer’s API “hey, here’s this file I got, I need you to save it” and your computer will be like “No problem!” And then it’ll take the file and encode the data to your hard drive & thanks to this abstraction model, the browser developers only need to code one “save function” and your computer will write it to the drive using whatever method it needs to and the browser developer doesn’t need to care whether the computer has a SSD or HDD because the computer’s API handles that instead.
It’s a very powerful model & if you’ve ever written a program (or even used just a computer) you’ve likely used APIs without even knowing it & that’s kind of their point.
Edit: I just looked through your post history & it seems like you’re trying to learn java, this book is a great resource that starts with the fundamentals of Java & programming concepts like this then gets into how to apply them efficiently as algorithms that manipulate data structures (which you’ll soon find is exactly what all programming is)
1
u/cellwall-999 Mar 26 '20
Thanks a lot. You're right. I just started learning Java and I'm doing pretty good with the basics n also using codingbat.com to cement my programming skills.
3
Mar 26 '20 edited Feb 06 '25
F reddit
1
u/cellwall-999 Mar 26 '20
👊
2
Mar 26 '20
Oh yeah, if you want to play with & test REST APIs, check this app out:
We use it constantly at work.
2
u/tobysmith568 Mar 26 '20
From the context in your description, I assume you're asking about web APIs.
Simply, web APIs allow your program to get information which it does not otherwise have.
So let's say that you're making an application to display the current weather - how would you get the information to display in your app? You would use a weather web API.
An example could be this one:
Notice that the URL there contains a few variables, firstly it's asking for version 2.5 of the API. Secondly q = London, UK - so this request is asking about London. Finally, appid = an ID.
Web APIs usually require some type of ID to be passed in some manner, this is to protect them. Think of your ID as your username and password rolled into one. Don't worry, what I typed there isn't my ID - it's just a sample one given by openweathermap.org
Finally, if you go to that URL yourself, you can see the data. It doesn't look like a normal website, it only returns data in a specific plaintext format - in this case, JSON.
Now, in the weather app that you're making you can visit that specific, custom URL, download the JSON, and then use the data within it.
Web APIs are not only for getting data like we've spoken about here, you can also send data to a server, request a server to delete data, and more - it just depends on how the web API is written.
1
2
u/McMasilmof Mar 26 '20
APIs are not 'one thing' that work the same everywhere. Any program can have an API, that just means that there is an easy way to interact with it.
Most APIs are web-based (and the most common is probably REST - but thats just a design pattern)
Foe example if you want to write a twitter bot, you can write a bot that opens a browser, types some credentials and presses the login butron etc. With the twitter API you can do these things without opening a browser, but you will just send a loginrequest or tweet to some URL.
1
2
u/Dparse Mar 26 '20 edited Mar 27 '20
OK something I'm not really seeing other people say is that there is another, more general meaning of the term API. Like everyone says it stands for Application Programming Interface, but 'application' here does not necessarily mean two different programs, perhaps operating on different servers.
API can also refer to the interface that a class or module makes available for other parts of the code in the project to use. You might say "The API of my user class allows editing the user's name, but not their address." In this sense the API is the line between something's internals - like the methods that a class uses privately to handle its business - and absolutely everything else, which is external. By tightly controlling your API and what it provides you can have perfect control over how the internals are operated and modified.
As a concrete example, imagine a class User with public fields for name and address, like my example above. That might look like this in pseudocode:
class User {
public string name;
public string address;
}
User harry = new User(name: "Harry", address: "123 Main Street")However since these strings are public, any code that is using Harry could very easily change Harry's address. Now maybe that's what you want to do if Harry is in a UpdateUserAddress method, but other methods that shouldn't be touching Harry's address could touch it, and they shouldn't be allowed.
Let's imagine you want to model the logic "Addresses must be in the United States" and you have a method called CheckAddressInUnitedStates that will verify exactly that. With User's API the way it is, even the places that should update addresses will be required to remember to call your Check method, or invalid addresses might sneak in.
Imagine if instead User looked like this.
class User {
public string name;
private string address;
public function getAddress() // Omitted
public function setAddress(newAddress) {
if CheckAddressInUnitedStates(newAddress) {
address = newAddress
}
}
}Now, the address is private, and other parts of the code cannot read or write it. We have instead provided an API to address, and we can enforce our logic within that API. Now, if someone tries to set Harry's address to something that is outside the US, the if condition will be false and the newAddress will not be saved.
I've omitted getAddress because its implementation starts to beg the question of whether or not you should have an Address class, which is a broader discussion I don't want to get in to.
1
2
u/TheActualMc47 Mar 26 '20
The "I" gives it away. API stands for "application programming interface", imagine that you have a service or an application running somewhere: for example, you have an online multi-player game running on a server. Of course, you can login to the game and see your items and your stats. For the sake of example, this is a PC game with no mobile support. You also have certain items that you can craft, but that takes a long time, and you don't want to always open your computer to check. Luckily, the game has a public API: for example, you can send a request to a certain URL, with certain options, and you get how much time is left on your items. With that, you build a mobile app to check for your items through your phone.
Tl;dr: it's an interface to use services from another application from a third party, which you can interact with "programmatically"
2
u/socratesTwo Mar 26 '20
The phrase I came up with to help explain it to a coworker is "APIs are sanity checkpoints." Imagine a standard military checkpoint; there's no way around it, and there are lots of men with machine guns. When you pull up to a check point, you interact with the guards they way they want you to interact with them (see previous point about the machine guns). If they want to search your vehicle, it gets searched. If they want you to drive it backwards, you drive it backwards.
So the idea of an API is to codify the policies that you want the guards to enforce on everything going into or out of the cordoned off area.
1
u/scandii Mar 26 '20
I feel people are giving you really long word salads of responses worthy of a wiki article.
an API is simply a program located behind an address somewhere that takes data. say you send the data "hello there" to an API located at myapi.com/myapi, it takes the data and if programmed to do so returns the data "general Kenobi".
that's it.
there's of course a bazillion layers of complexity on top of this but all we're doing is sending data (called payload in API lingo) and getting responses.
the reason we do this is so that we can ask other software about things like getting weather data from a weather service.
1
u/nutrecht Mar 26 '20
an API is simply a program located behind an address somewhere that takes data.
An API doesn't have to be a web API. While it's common nowadays, it's definitely not the only use of the work.
1
u/scandii Mar 26 '20
unlike popular belief address is not equal to web.
1
u/nutrecht Mar 26 '20
An API can also be a DLL for example. There doesn't have to be any 'address'.
1
0
Mar 26 '20
[deleted]
1
u/cellwall-999 Mar 26 '20
Phew! This was hard to take in but really helpful. Thank you for your time and assistance. 👊
1
u/cakeofzerg Mar 26 '20
I forget what API stands for doesn't matter but it's like a guy in the corner U can ask a question and sometimes U get an answer back RESTFULLY
1
1
u/AdmiralAdama99 Mar 26 '20
API's are just a language (often a bunch of functions) that different computer programs use to talk to each other. Examples:
chess gui talking to chess AI
application talking to operating system
php website talking to aws (amazon web services) to use their services (like send an email that wont go to spam, or use a queue)
-1
128
u/Jrobah Mar 26 '20
API - Application Programming Interface. It is a way of allowing you to access the data from another application or a way of applications to communicate with each other.
Take the example of a restaurant, since it belongs to someone else and you are not allowed to go to the Kitchen therefore the menu is the API. Allows you to make an order for food. Food is like data which you request for. The order you make is called an API call. When you make an order, the waiter will give you a response eg the food is available or not. Same happens with APIs, when you make an api call, you will either get a successful response and get your data or get an error and no data.
Now you can refer to the more technical explanations given in the thread