Product API A basic REST API built with Django and Django Rest Framework to perform CRUD (Create, Read, Update, Delete) operations on a collection of products.
Setup and Installation
- Clone the Repository Clone this project to your local machine.
git clone https://github.com/Nexxumm/Basic-Product-API cd Basic-Product-API
- Create a Virtual Environment It's highly recommended to use a virtual environment to manage project dependencies.
For Windows:
python -m venv .venv .venv\Scripts\activate
For macOS/Linux:
python3 -m venv .venv source .venv/bin/activate
- Install Dependencies Install all the required packages using the requirements.txt file.
pip install -r requirements.txt
- Create the .env File This project uses a .env file to manage environment variables like the SECRET_KEY.
a. Create a file named .env in the project's root directory .
b. Generate a new Django secret key by running the following command in your terminal:
python manage.py shell -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
c. Copy the generated key and add it to your .env file. It should look like this:
SECRET_KEY=your-generated-secret-key-goes-here
Running the Application
- Apply Database Migrations Run the migrations to create the database tables for your models.
python manage.py makemigrations python manage.py migrate
- Run the Development Server Start the Django development server.
python manage.py runserver
The API will now be running at http://127.0.0.1:8000/.
API Endpoints You can test the API endpoints using cURL.
- List Products or Create a New Product URL: /products/
Methods: GET, POST
GET - Retrieve all products
curl -X GET http://127.0.0.1:8000/products/
POST - Create a new product
curl -X POST -H "Content-Type: application/json" -d '{"name": "New Gadget", "description": "A shiny new gadget.", "price": "200"}' http://127.0.0.1:8000/products/
- Retrieve, Update, or Delete a Specific Product URL: /products//
Methods: GET, PUT, DELETE
GET - Retrieve a single product
curl -X GET http://127.0.0.1:8000/products/1/
PUT - Update a product
curl -X PUT -H "Content-Type: application/json" -d '{"name": "Updated Gadget", "description": "An even shinier gadget.", "price": "250"}' http://127.0.0.1:8000/products/1/
DELETE - Delete a product
curl -X DELETE http://127.0.0.1:8000/products/1/