Creating Python APIs - The Django REST framework : Basics[1/n]
Photo from Dreamstime.com
What is an API?
I'm not going to be talking about the technical definition here, you can find that on Wikipedia.
What I'm going to talk about however is what APIs are in real life, where they are used and why they are important? For example, when you're developing MAC or iOS apps, there is an API that connects with the OS, we're not discussing that.
We are going to talk about Web APIs.
So why are APIs important?
Let's say you want to scrape Twitter and display the trending topics on your website. This will work but will be inefficient because it depends on the website and its items not changing. So if twitter decides to move the trending tab's position then your scraper will break.
The correct way to deal with this is to use the Twitter API. For example, if you're trying to get tweet timelines then the request would be something like -
and the response would be something like this -
"created_at": "Wed Oct 10 20:19:24 +0000 2018", "id": 1050118621198921728,`
"text": "To make room for more expression, we will now count all emojis as equal—including those with gender and skin t… https://t.co/MkGjXf9aXm",
"user_mentions": , ...
There are other ways to display the data as well, like CSVs and XML format files but Json is so widely used it is the standard now.
The Json file basically says that this is how the data will be received. The 'h' in 'hashtags' won't change from 'h' to 'H'. It would remain constant. The Json file is the machine code and will be in the background. You will see the Twitter timeline and not the code behind it.
Where are APIs used?
- Lets say you have a website and you want to make an app, API is the way to do it.
- If you want to display the weather data on your website then you can use the API of a broadcasting site and use it in your website to display the weather. So that adds additional functionality to your website.
- You can use the Google Maps API to embed a map in your website.
and many more.
Hope you enjoyed this post! If you happen to like it, feel free to share. You can also follow me on Twitter on my coding journey.
This is the 1st article of creating Python APIs series, stay tuned for the remaining blog posts. We will at make an API and implement it in one of the apps we made using Django!