Build a REST API in 30 minutes with Django REST Framework
--
Building a REST API in Django is so super easy. In this tutorial, we’ll walk through the steps to get your first API up and running.
(This post is part of a series where I teach how to deploy a React front end on a Django back end. However, nothing in this post is React specific. Whatever your API needs, read on!)
[Just want to see some source code? My pleasure: https://github.com/bennett39/drf_tutorial]
Why REST API?
Before we get to the code, it’s worth considering why you would want to build an API. If someone had explained these basic concepts to me before I started, I would have been so much better off.
A REST API is a standardized way to provide data to other applications. Those applications can then use the data however they want. Sometimes, APIs also offer a way for other applications to make changes to the data.
There are a few key options for a REST API request:
- GET — The most common option, returns some data from the API based on the endpoint you visit and any parameters you provide
- POST — Creates a new record that gets appended to the database
- PATCH — Update individual fields of an existing record
- PUT — Looks for a record at the given URI you provide. If it exists, update/overwrite the existing record. If not, create a new record
- DELETE — Deletes the record at the given URI
Typically, an API is a window into a database. The API backend handles querying the database and formatting the response. What you receive is a static response, usually in JSON format, of whatever resource you requested.
REST APIs are so commonplace in software development, it’s an essential skill for a developer to know how they work. APIs are how applications communicate with one another or even within themselves.
For example, in web development, many applications rely on REST APIs to allow the front end to talk to the back end. If you’re…