A Definitive Guideline for Creating APIs with Django and Neo4j database, Part-0
Tags: django, beginners, python, tutorial
Published: 2023-08-24 04:34:59

Imagine, you are asked to create the "back-end" for a product or a website. Assuming this is your first project and you are a cheem doge (May he rest in peace), you may ask: 

> what do you mean by bamck-emd and what does it comtaim? 

Before starting with the execution, I want to provide some contexts in this blog post. Eventually we will build a project with graph database-based backend. That is why, this is the zeroth part of this backend development series. 

## 1. What is a Backend?

What no one told while I was starting in the industry, is what exactly is backend and why does there is also a front-end. Essentially, a software project is split into these two parts for convenience. The front-end part handles the data input and visualization part. On the other hand, back-end has the data storage and from that storage, you have to fetch the data, or do queries for data and display it through the front-end. 


![fe be](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3fuhox2udrl1ndc0cnq.png)

## 2. Database and Graph Database

Your database will store all your data. You need to pick a database that works for your project. For this project, I have picked a unique type of database - graph database. Unlike regular databases, graph database consists of nodes and edges. Each edge emulates a relationship between nodes. 


![graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sumgjenxl81l91qbzvg.png)

## 3. Draw your ERD

This step involves making an Entity-Relationship diagram for your database(s). [dbdiagram.io](https://dbdiagram.io/) is a great place to do so, where your written code for tables will instantly be turned into ERD. 

For this project, we will create an authentication system for social media users using graph database Neo4j and Django framework. Here is the graph ERD for this project:


![Graph ERD](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2s0jmpnlu3xyulqqlay.png)

## 4. API

Imagine, you have gone to a restaurant to dine. You do not go to the kitchen of the restaurant for food, do you? You are the front-end, and the kitchen is the database of food. There has to be someone who can serve you the food that you ordered. Ergo comes the APIs! API or Application Program Interface works as a liaison between your database and your front-end application. 


![API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vd80dvnz1bkjc8adw8ni.jpg)

Oops! The APIs have given you bonk instead of data.

## 5. RESTful API

RESTful API means REpresentational State Transfer API. What does that mean? These REST APIs are used to establish connections to machines and web services and do various database operations with less bandwidth. REST API architecture is built upon HTTP code response format. The results of a REST APIs can be displayed in JSON, HTML, XML formats although JSON is used the most now. There are GET, POST, PUT, PATCH and DELETE operations in RESTFUL API. 

We will build some GET and POST APIs in this project. 

## 6. Environment setup

We will start to implement things from the following posts. At first, we need to have python in our machines. It is always a best practice to install and maintain virtual environments for Python which enables you to use various Python versions in the same machine. I always use the following two links for the setup; these procedures work best in Linux and MacOs:

[Installing pyenv](https://github.com/pyenv/pyenv)
[Installing virtualenv](https://github.com/pyenv/pyenv-virtualenv)

For this project, I used python version **3.6.15** but you can use any python 3+ versions. Now install the basic packages using pip:

```
pip install Django djangorestframework

pip install neomodel django-neomodel neo4j-driver

```

Go to your settings.py file and add these along with your app name inside the INSTALLED_APPS:

```{Python}

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'yourapp.apps.MyapiConfig',
    'rest_framework',
    'django_neomodel'
]

```

In the next tutorial, we will setup connections between our frameworks and database. Let me know in the comments about what do you have in mind regarding this.