https://github.com/thilinajayamal/flask-crud-operations
Simple REST API Using Flask
https://github.com/thilinajayamal/flask-crud-operations
crud-operation flask python rest-api
Last synced: 24 days ago
JSON representation
Simple REST API Using Flask
- Host: GitHub
- URL: https://github.com/thilinajayamal/flask-crud-operations
- Owner: ThilinaJayamal
- Created: 2025-09-07T05:57:47.000Z (30 days ago)
- Default Branch: main
- Last Pushed: 2025-09-07T06:44:57.000Z (30 days ago)
- Last Synced: 2025-09-07T08:34:58.447Z (30 days ago)
- Topics: crud-operation, flask, python, rest-api
- Language: Python
- Homepage: https://github.com/ThilinaJayamal/flask-CRUD-operations
- Size: 6.84 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ Simple REST API Using Flask
A simple, RESTful API built with **Flask**, **Flask-RESTful**, and **SQLAlchemy** to manage users. Easily create, read, update, and delete users in a SQLite database.
## ๐ Features
* โ Create new users with name and email
* ๐ Retrieve all users or a single user by ID
* โ๏ธ Update user information
* ๐๏ธ Delete users
* ๐พ Persist data with a SQLite database
* โก Input validation with Flask-RESTful reqparse
* ๐ฆ JSON responses using marshal_with## ๐ ๏ธ Tech Stack
* **Backend**: Python, Flask, Flask-RESTful
* **Database & ORM**: SQLite, SQLAlchemy
* **API Design**: RESTful## ๐งช Getting Started (Development)
### Prerequisites
* Python 3.7 or higher
* pip (Python package installer)### Installation
1. **Clone the repository:**
```bash
git clone https://github.com/ThilinaJayamal/flask-CRUD-operations.git
cd flask-CRUD-operations
```2. **Create a virtual environment (optional but recommended):**
```bash
# For Linux/Mac
python3 -m venv .venv
source .venv/bin/activate# For Windows (Command Prompt)
py -m venv .venv
.venv\Scripts\activate# For Windows (Git Bash)
py -m venv .venv
source .venv/Scripts/activate
```3. **Install dependencies:**
```bash
pip install -r requirements.txt
```4. **Initialize the database:**
```bash
python create_db.py
```5. **Run the application:**
```bash
python api.py
```6. **Access the API:**
Open your browser or API client to access the API at:
```
http://127.0.0.1:5000/
```## ๐๏ธ API Endpoints
### Get all users
```http
GET /api/users/
```**Response:**
```json
[
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
]
```### Get user by ID
```http
GET /api/users/
```**Response:**
```json
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
```### Create a new user
```http
POST /api/users/
```**Request Body:**
```json
{
"name": "John Doe",
"email": "john@example.com"
}
```**Response:**
```json
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
```### Update user
```http
PATCH /api/users/
```**Request Body:**
```json
{
"name": "Jane Doe",
"email": "jane@example.com"
}
```**Response:**
```json
{
"id": 1,
"name": "Jane Doe",
"email": "jane@example.com"
}
```### Delete user
```http
DELETE /api/users/
```**Response:**
```json
{
"message": "User deleted successfully"
}
```## ๐๏ธ Database Schema
### UserModel
| Field | Type | Constraints |
|-------|---------|-------------------|
| id | Integer | Primary Key |
| name | String | Not Null |
| email | String | Unique, Not Null |## ๐ Project Structure
```
flask-CRUD-operations/
โโโ api.py # Main Flask application
โโโ create_db.py # Database initialization script
โโโ requirements.txt # Python dependencies
โโโ README.md # Project documentation
โโโ instance/
โโโ database.db # SQLite database file (created after running)
```## ๐ฆ Requirements
```
Flask==3.0.0
Flask-RESTful==1.0.0
Flask-SQLAlchemy==3.0.0
```## ๐งช Testing the API
You can test the API endpoints using various tools:
### Using curl
```bash
# Get all users
curl -X GET http://127.0.0.1:5000/api/users/# Create a new user
curl -X POST http://127.0.0.1:5000/api/users/ \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'# Get user by ID
curl -X GET http://127.0.0.1:5000/api/users/1# Update user
curl -X PATCH http://127.0.0.1:5000/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "Jane Doe", "email": "jane@example.com"}'# Delete user
curl -X DELETE http://127.0.0.1:5000/api/users/1
```### Using Postman
1. Import the collection or manually create requests
2. Set the base URL to `http://127.0.0.1:5000`
3. Add the appropriate headers and request bodies as shown above## ๐ง Configuration
The application uses the following default configuration:
* **Database**: SQLite (`instance/database.db`)
* **Host**: 127.0.0.1
* **Port**: 5000
* **Debug Mode**: Enabled (for development)To modify these settings, update the configuration in `api.py`.
## ๐ค Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request## ๐ License
This project is licensed under the MIT License. See the `LICENSE` file for details.
## ๐ง Contact
**Thilina Jayamal** - [GitHub](https://github.com/ThilinaJayamal)
Project Link: [https://github.com/ThilinaJayamal/flask-CRUD-operations](https://github.com/ThilinaJayamal/flask-CRUD-operations)
## ๐ Acknowledgments
* Flask documentation
* Flask-RESTful documentation
* SQLAlchemy documentation