An open API service indexing awesome lists of open source software.

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

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