Building a Bookstore API with Django-Ninja-Extra

Lake Davenberg Avatar

·

If you are looking to build a fast and efficient API for your bookstore application, then look no further. In this article, we will explore how to use the Django-Ninja-Extra package along with Ninja-Schema and NinjaJWT to create a robust Bookstore API.

Step 1: Setting up the Development Environment

To get started, make sure you have Docker installed on your machine. Docker will allow us to easily build and run our API in a containerized environment. Once Docker is installed, clone the bookstoreapi repository from Github.

Step 2: Building and Running the Docker Image

In the root directory of the bookstoreapi repository, open a terminal and run the following command to build the Docker image:

bash scripts/docker_build.sh build

Once the image is built, run the following command to start the Docker container:

bash scripts/docker_build.sh up

You can now visit http://0.0.0.0:8001/api/docs to access the API documentation.

Step 3: Creating Endpoints

Now that our API is up and running, let’s start by creating some endpoints. Django-Ninja-Extra provides a powerful set of tools for building APIs, including automatic CRUD views, authentication, and schema validation.

For example, let’s create an endpoint for retrieving a list of all books in the bookstore. In the bookstoreapi/app/api/views.py file, add the following code:

from bookstoreapi.app.models import Book
from ninja import Router

router = Router()

@router.get("/books")
def get_books(request):
books = Book.objects.all()
return books

This code defines a GET endpoint /books that returns a JSON response containing all the books in the database.

Similarly, you can create endpoints for adding new books, updating existing books, and deleting books. Django-Ninja-Extra makes it easy to define these endpoints using the @router decorator.

Step 4: Securing the API with JWT Authentication

To secure our API, we can use the NinjaJWT package provided by Django-Ninja-Extra. This package allows us to easily integrate JWT authentication into our API.

First, install the django-ninja-jwt package by running the following command:

pip install ninja-jwt

Next, add the following code to the bookstoreapi/app/api/views.py file to enable JWT authentication for our endpoints:

from ninja.security import APIKeyHeader
from ninja_jwt import AuthJWT

router = Router()
auth = AuthJWT()

@router.get("/books")
@auth.jwt_required
def get_books(request):
books = Book.objects.all()
return books

Now, only authenticated users with a valid JWT token will be able to access the /books endpoint.

Conclusion

In this article, we have seen how to build a powerful Bookstore API using Django-Ninja-Extra, Ninja-Schema, and NinjaJWT. We learned how to set up the development environment, create endpoints, and secure the API with JWT authentication. With these tools, you can now easily build your own APIs and empower your bookstore application with a robust backend.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *