DEV Community

Cover image for APIs are magic
Sibusiso Dlamini
Sibusiso Dlamini

Posted on

APIs are magic

I have no idea why but it took me forever to grasp the concept of what an API was. I heard this term every now and then in a YouTube video, a DEV blog but I never really understood what this acronym actually meant. Having actually built a project and used it I believe I finally caught on to the concept.

As much as I feel like I understand how they work I can't give a concise definition for it. Hence the name of the article. I can't put it in my own words without writing paragraphs and I don't think an API deserves a definition that long for such a simple concept. So I'd rather attempt to explain APIs through how it is used.

A client(user) can make a request to the API. This request can be either to retrieve (get) information or change (post, insert) information and the API will carry out the request. This data is usually built in the form of JSON. This format can be manipulated to show valuable information for example.

I built this small python script that fetches pictures form a Pixabay API. The plan was to get or fetch random images from an API that I would use to change my wallpaper on a daily basis. The problem was setting the wallpaper using code. I know I said in an earlier post that I would complete this project but let me explain...

I am not one to be scared of messing around with unfamiliar technology but changing the wallpaper using code required editing the windows registry. Now from what I've learned about the windows registry is that it is basically this sort of dictionary that keeps track of all your settings on your computer. I say dictionary because the data is stored in a normal file folder structure where each file at the end of a folder has a sort of key value pair structure.

Anyway... After browsing the web for a while I kept getting the same message that went something along the lines of "Make sure you backup your registry". They always seemed to caution the user on editing the registry. So I weighed my options. Go mess with the registry with code which 9 out of 10 times has bugs in it and could possibly wreak havoc on my PC ORRRRRRR just give up. I went with the latter.

I don't encourage giving up except for in the cases that I believe it is merited, like when I do it 😉 . There is a saying that goes. Winners know when to quit. And if this is true, I feel justified in quitting. You see the code to change the registry is one line of code like so

os.system('a command that you would enter into the command line')
Enter fullscreen mode Exit fullscreen mode

The point being that I could've tried it but the risk was greater than the reward.

Conclusion. I've felt like I've learned a lot from using an API since it helped improve my understanding of how the internet works in general. I'm pretty sure that I have a lot left to learn about APIs but just knowing that this thing exists is oddly satisfying. If you are not content and you still need a formal concise definition of an API just use the heading of the blog post. If anyone wants to check out the little program I wrote you can find it in this github repository. Happy coding 😄

Latest comments (0)