This project demonstrates how to create a simple CRUD (Create, Read, Update, Delete) API using FastAPI, a modern, fast (high-performance) web framework for building APIs with Python.
Before you begin, ensure you have the following installed on your system:
- Python 3.9.13
- pip (Python package installer)
- PostgreSQL
-
Clone the repository:
git clone https://github.com/PritamIT2023/crud-FASTAPI-poc.git cd fastapi-crud-project
-
Install the required packages:
pip install -r requirements.txt
-
Set up your database:
This project uses PostgreSQL. Make sure you have PostgreSQL installed and running. Then, create a new database for this project.
The database connection is configured using the following URL:
SQLALCHEMY_DATABASE_URL = "postgresql://postgres:admin@localhost:5432/python-fast"
This URL is structured as follows:
postgresql://
: The database system being usedpostgres
: The username for the databaseadmin
: The password for the database userlocalhost:5432
: The host and port where the database server is runningpython-fast
: The name of the database
Modify this URL in your
database.py
file if your PostgreSQL setup is different. -
Start the FastAPI server:
uvicorn main:app --reload
The API will be available at
http://localhost:8000
fastapi-crud-project/
│
├──db
│ ├── Models.py
│ ├── database.py
| ├── schema.py
├── main.py
├── requirements.txt
└── README.md
main.py
: Contains the FastAPI application and route definitions.Models.py
: Contains model structure.schema.py
: Defines the Pydantic models for request/response handling.database.py
: Handles database connections and operations.requirements.txt
: Lists all the Python dependencies for the project.
GET /product
: Retrieve all productGET /product/{product_id}
: Retrieve a specific itemPOST /product
: Create a new itemPUT /product/{product_id}
: Update an existing itemDELETE /product/{product_id}
: Delete an item
Run your application :
fastapi dev main.py
Auto-reloading when code changes are detected.
FastAPI automatically generates interactive API documentation. Once the server is running, you can access:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
Open a terminal and change to your project directory:
cd path/to/your_fastapi_project
To run all tests in your project:
pytest
This command will discover and run all files in the current directory and subdirectories that match the pattern test_*.py
or *_test.py
.
Contributions are welcome! Please feel free to submit a Pull Request.