This is a simple Flask-based Notes API that allows users to create, read, update, and delete (CRUD) notes. It uses PostgreSQL as the database and provides RESTful API endpoints to interact with the notes data.
- Features
- Technologies Used
- Installation
- Running the Application
- API Endpoints
- Database Setup
- Using Docker
- Testing the API with Postman
- Using DataGrip
- Create a new note with a name and description.
- Retrieve all notes or a specific note by ID.
- Update an existing note.
- Delete a note.
- Python: The main programming language used.
- Flask: Micro web framework to create the REST API.
- PostgreSQL: Relational database used to store the notes.
- psycopg2: PostgreSQL adapter for Python to connect and interact with the database.
- Docker: Used for containerizing the application and the database.
- DataGrip: SQL IDE for managing and visualizing the database.
- Postman: API testing tool to interact with the API endpoints.
- Python 3.x installed.
- PostgreSQL installed and running locally or via Docker.
pip
package manager for Python.- Docker (optional, for containerization).
- Postman (optional, for testing API).
- DataGrip (optional, for managing the database).
-
Clone the repository:
git clone https://github.com/avyya-singh/Flask-Notes-API.git cd Flask-Notes-API
-
Create a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up PostgreSQL:
- Option 1: Install PostgreSQL locally and create a database named
postgres
. - Option 2: Use Docker to run PostgreSQL (instructions below).
- Option 1: Install PostgreSQL locally and create a database named
-
Initialize the database:
python init_db.py
To start the Flask server:
python app.py
The application will run on http://localhost:5000
.
- Retrieves a list of all notes.
- Response:
{ "notes": [ { "id": 1, "name": "Note1", "description": "Description1" }, { "id": 2, "name": "Note2", "description": "Description2" } ] }
- Retrieves a specific note by ID.
- Response:
{ "note": { "id": 1, "name": "Note1", "description": "Description1" } }
- Creates a new note.
- Request Body:
{ "name": "New Note", "description": "New description" }
- Response:
{ "message": "Note created successfully", "note_id": 3 }
- Updates an existing note by ID.
- Request Body:
{ "name": "Updated Note", "description": "Updated description" }
- Response:
{ "message": "Note updated successfully", "note_id": 1 }
- Deletes a specific note by ID.
- Response:
{ "message": "Note deleted successfully" }
- The PostgreSQL table is automatically created if it doesn't already exist when running the app.
- You can manually insert data into the
table1
using theinit_db.py
script.
The table schema for table1
is:
CREATE TABLE IF NOT EXISTS table1 (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
description VARCHAR(1000)
);
To simplify setting up the PostgreSQL database, you can use Docker to run PostgreSQL in a container.
-
Pull the PostgreSQL image from Docker Hub:
docker pull postgres
-
Run the PostgreSQL container:
docker run --name postgres-container -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
-
You can now connect your Flask app to this PostgreSQL instance by updating the database credentials in
repository/NoteRepository.py
andinit_db.py
to:- Host:
localhost
- Port:
5432
- Database:
postgres
- Username:
postgres
- Password:
mysecretpassword
- Host:
You can use Postman to test the API endpoints.
- Install Postman if you don't have it: Download Postman.
- Create a new request and specify the API method (GET, POST, PUT, DELETE).
- For POST/PUT requests, provide the required JSON body in the Body tab of Postman.
- Test the API by sending requests to
http://localhost:5000/api/v1/notes
.
Examples:
- GET all notes:
GET http://localhost:5000/api/v1/notes
- Create a note:
POST http://localhost:5000/api/v1/notes
with a body like:{ "name": "New Note", "description": "Note description" }
You can manage and inspect your PostgreSQL database using DataGrip:
- Open DataGrip and create a new PostgreSQL data source.
- Provide the connection details (e.g.,
localhost
, port5432
, userpostgres
, passwordmysecretpassword
). - Once connected, you can view and manage the
table1
table, execute queries, and inspect the data in your database.
├── app.py # Flask application entry point
├── init_db.py # Initializes the PostgreSQL database
├── models
│ └── Note.py # Note model
├── repository
│ └── NoteRepository.py # Repository layer for database interactions
├── routes
│ └── NoteRoutes.py # Flask routes for handling HTTP requests
├── servicess
│ └── Service.py # Business logic for handling notes
├── requirements.txt # Dependencies