A FastAPI project that implements basic CRUD (Create, Read, Update, Delete) operations for managing resources in an API. This project includes simple endpoints for handling resource data with validation, error handling, and data persistence using SQLAlchemy.
- Create: Add new resources to the database.
- Read: Retrieve a list of all resources or a single resource by its ID.
- Update: Modify existing resources in the database.
- Delete: Remove resources from the database.
- Data Validation: Ensures proper data formats using Pydantic models.
- Error Handling: Handles common HTTP errors and validation issues.
- Database Integration: Uses SQLAlchemy for ORM-based interactions with the SQLite database.
- Python 3.x
- FastAPI: Web framework for building APIs.
- Uvicorn: ASGI server for serving the FastAPI app.
- SQLAlchemy: ORM for interacting with the database.
- Pydantic: Data validation library.
- SQLite: The default database used for this project.
-
Clone the repository:
git clone https://github.com/shahramsamar/Fast_api_crud.git cd Fast_api_crud -
Install Dependencies:
If you're using
pip, run:pip install -r requirements.txt
-
Run the Application:
To run the FastAPI app using Uvicorn:
uvicorn main:app --reload
This will start the server at
http://127.0.0.1:8000.
-
Create a Resource:
- Send a
POSTrequest to/items/with the required data to create a new resource.
Example request body:
{ "name": "New Item", "description": "A description of the new item." } - Send a
-
Read Resources:
- Send a
GETrequest to/items/to retrieve all resources. - Send a
GETrequest to/items/{item_id}/to retrieve a specific resource by its ID.
- Send a
-
Update a Resource:
- Send a
PUTrequest to/items/{item_id}/with the updated data to modify an existing resource.
Example request body:
{ "name": "Updated Item", "description": "Updated description of the item." } - Send a
-
Delete a Resource:
- Send a
DELETErequest to/items/{item_id}/to delete the resource by ID.
- Send a
- Create Item:
POST /items/ - Get All Items:
GET /items/ - Get Item by ID:
GET /items/{item_id}/ - Update Item:
PUT /items/{item_id}/ - Delete Item:
DELETE /items/{item_id}/
main.py: Contains the FastAPI application, route handlers, and database setup.models.py: Defines SQLAlchemy ORM models, e.g.,Item.schemas.py: Contains Pydantic models for validation and response formatting.database.py: Configures database connection and session handling.requirements.txt: Lists the necessary libraries and dependencies for the project.
FastAPI automatically generates interactive API documentation:
- Swagger UI:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
-
Create Item (POST):
curl -X 'POST' \ 'http://127.0.0.1:8000/items/' \ -H 'Content-Type: application/json' \ -d '{ "name": "Sample Item", "description": "This is a sample item" }'
-
Get All Items (GET):
curl -X 'GET' 'http://127.0.0.1:8000/items/'
Feel free to fork the project and submit pull requests for new features, improvements, or bug fixes.
This project is open-source and available for educational purposes.