This FastAPI CRUD API provides endpoints to perform CRUD (Create, Read, Update, Delete) operations on user records using SQLAlchemy and SQLite as the database.
Before you can set up, run, and use the API, make sure you have the following prerequisites installed:
- Python 3.x
- pip (Python package manager)
-
Clone the repository:
git clone <repository-url> cd <repository-folder>
-
Install the required Python packages:
pip install -r requirements.txt
- URL:
/api/
- Method:
POST
- Request Body: JSON
{ "name": "mark essien" }
- Response: JSON
{ "id": 1, "name": "mark essien" }
- URL:
/api/
- Method:
GET
- Query Parameters:
skip
(optional): Number of records to skip (default: 0)limit
(optional): Maximum number of records to return (default: 100)
- Response: JSON Array of Users
- URL:
/api/{user_id}/
- Method:
GET
- Response: JSON
{ "id": 1, "name": "mark essien" }
- URL:
/api/{user_id}/
- Method:
PUT
- Request Body: JSON
{ "name": "Updated Name" }
- Response: JSON
{ "id": 1, "name": "Updated Name" }
- URL:
/api/{user_id}/
- Method:
DELETE
- Response: JSON
{ "id": 1, "name": "Updated Name" }
To run the API, execute the following command in the project directory:
uvicorn main:app --reload
The API will be available at http://127.0.0.1:8000/
. You can access the Swagger documentation at http://127.0.0.1:8000/docs
and the ReDoc documentation at http://127.0.0.1:8000/redoc
.
Here are some sample requests you can make to the API:
-
for local server, replace "https://hngx-task2-qh6l.onrender.com" with "http://127.0.0.1:8000"
-
Create a User:
curl -X POST -H "Content-Type: application/json" -d '{"name": "mark essien"}' https://hngx-task2-qh6l.onrender.com/api/
-
Read All Users:
curl https://hngx-task2-qh6l.onrender.com/api/
-
Read a User by ID:
curl https://hngx-task2-qh6l.onrender.com/api/1/
-
Update a User by ID:
curl -X PUT -H "Content-Type: application/json" -d '{"name": "Updated Name"}' https://hngx-task2-qh6l.onrender.com/api/1/
-
Delete a User by ID:
curl -X DELETE https://hngx-task2-qh6l.onrender.com/api/1/
- This API uses an SQLite database, which may not be suitable for large-scale production use. Consider using a more robust database system for production deployments.
- This is a basic CRUD API and may require additional features and security measures for real-world applications.