Skip to main content

REST API

REST API (also known as RESTful API) is also supported via django-rest-framework

Developers can quickly expose API to the frontend apps or mobile apps.

Consistent Signup, Login Behavior

The Signup, Login workflow are the same as the standard Django app. (django-allauth)

How to use the API

  1. Please check tests code under django_app/tests/unittest/users/api
  2. Or check the API Doc, you can find entry URL of the API doc in django_app/urls.py

Token

By default, we use rest_framework.authtoken as our Token solution

Simple Example

# make sure the django dev server is running
$ python manage.py runserver
# run code below in the Django shell

import requests
from django.urls import reverse

# login to get the token
login_url = reverse("rest_login")
resp = requests.post(
f"http://localhost:8000{login_url}",
json={"username": "CHANGE-ME", "password": "CHANGE-ME"},
)
resp.raise_for_status()

# if login succeed, get token from the response
data = resp.json()
token = data["key"]

# add token in the request header
detail_url = reverse("rest_user_details")
resp = requests.get(
f"http://localhost:8000{detail_url}",
headers={"Authorization": f"Token {token}"},
)
resp.raise_for_status()

# you should see username, user_pk of the current login user
print(resp.json())

JWT Reference

You can also switch to JWT if you like

  1. JSON Web Token (JWT) Support
  2. Blacklist app
  3. Revoke JWT Refresh Token