DEV Community

Cover image for Understanding REST APIs: A Beginner-Friendly Guide
Sarah Nyambura Kiiru
Sarah Nyambura Kiiru

Posted on

Understanding REST APIs: A Beginner-Friendly Guide

When building modern web applications, the Frontend (like React) and the Backend (like Django) need a way to talk to each other. This is where APIs (Application Programming Interfaces) come in.

One of the most common types is the REST API (Representational State Transfer). Let’s dive in


What is a REST API?

A REST API is an architectural style for designing APIs that allows communication between client and server using standard HTTP methods.

It works on resources (like users, jobs, or posts) and represents them using URLs (endpoints).

For example:

GET /api/users/      → fetch all users
GET /api/users/1/    → fetch a specific user with ID=1
POST /api/users/     → create a new user
Enter fullscreen mode Exit fullscreen mode

HTTP Methods vs REST API Actions

REST APIs rely on HTTP methods to define what action should be performed.

Here’s the mapping between HTTP methods and their REST meanings:

HTTP Method REST API Action Example Description
GET Read GET /api/jobs/ Retrieve data (read-only).
POST Create POST /api/jobs/ Add new data.
PUT Update (replace) PUT /api/jobs/1/ Update all fields of a resource.
PATCH Update (partial) PATCH /api/jobs/1/ Update specific fields.
DELETE Delete DELETE /api/jobs/1/ Remove a resource.

Key Difference:

  • HTTP method = the protocol action (GET, POST, etc).
  • REST API action = how that HTTP method is applied to resources in your system.

Commonly Used HTTP Status Codes

When your client (React frontend) talks to your server (Django backend), the server responds with a status code that tells you what happened.

Here are some of the most common ones:

Status Code Meaning When it’s used
200 OK Success Request was successful.
201 Created Success Resource was created (e.g., after POST).
204 No Content Success Resource deleted successfully.
400 Bad Request Client Error Request was malformed (wrong data).
401 Unauthorized Client Error Authentication required or failed.
403 Forbidden Client Error You don’t have permission to access this resource.
404 Not Found Client Error Resource doesn’t exist.
500 Internal Server Error Server Error Something went wrong on the server.

A Quick Example with Django REST Framework (DRF)

In Django, creating a REST API often involves:

  1. Models – define your data.
  2. Serializers – convert models to JSON.
  3. Views – handle requests (GET, POST, etc).
  4. URLs – map endpoints.

Example:

# models.py
from django.db import models

class Interview(models.Model):
    candidate_name = models.CharField(max_length=100)
    date = models.DateField()
    score = models.IntegerField()
Enter fullscreen mode Exit fullscreen mode
# serializers.py
from rest_framework import serializers
from .models import Interview

class InterviewSerializer(serializers.ModelSerializer):
    class Meta:
        model = Interview
        fields = '__all__'
Enter fullscreen mode Exit fullscreen mode
# views.py
from rest_framework import viewsets
from .models import Interview
from .serializers import InterviewSerializer

class InterviewViewSet(viewsets.ModelViewSet):
    queryset = Interview.objects.all()
    serializer_class = InterviewSerializer
Enter fullscreen mode Exit fullscreen mode
# urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import InterviewViewSet

router = DefaultRouter()
router.register(r'interviews', InterviewViewSet)

urlpatterns = [
    path('api/', include(router.urls)),
]
Enter fullscreen mode Exit fullscreen mode

Now your API endpoints look like:

GET /api/interviews/     → list all interviews
POST /api/interviews/    → create an interview
GET /api/interviews/1/   → fetch specific interview
PUT /api/interviews/1/   → update interview
DELETE /api/interviews/1/ → delete interview
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • REST APIs allow frontend and backend to communicate using HTTP.
  • Each HTTP method maps to a REST action (CRUD: Create, Read, Update, Delete).
  • Status codes help understand what happened with the request.
  • Django REST Framework makes building REST APIs simpler by providing serializers, viewsets, and routers.

With this foundation, you can confidently connect your React frontend with your Django backend using REST APIs. 🎉


💡 Pro tip: Keep this as a reference whenever you feel stuck in API development!

Top comments (1)

Collapse
 
suvrajeet profile image
Suvrajeet Banerjee

Awesome & Insightful! 🏆😃