The FakeStore API is a RESTful API for managing products, orders, carts, and more. This documentation provides instructions on how to clone and run the FakeStore API on your local machine.
Before you begin, make sure you have the following installed on your computer:
- Python 3.x
- pip (Python package manager)
- MySQL (optional)
- Docker (optional)
- Open your terminal or command prompt.
- Navigate to the directory where you want to clone the FakeStore API repository.
- Run the following command to clone the repository:
git clone "https://github.com/urs-santoshh/fake_store_api.git"
We recommend using a virtual environment to isolate the project's dependencies. If you choose not to use a virtual environment, you can skip this step.
- Navigate to the project directory:
cd fake_store_api
- Create a virtual environment (Python 3.x) and activate it:
python -m venv .venv source venv/bin/activate # On Windows, use "./venv/scripts/activate"
- Install the project dependencies using pip:
pip install -r requirements.txt
Create a .env
file in the project root directory and configure the following environment variables:
ENVIRONMENT = {your environment PRODUCTION|DEVELOPMENT}
MY_DOMAIN=http://127.0.0.1:8000
SECRET_KEY = {your django secret key here}
SITE_ID = {your django site it}
Choose one of the following database configuration options:
If you prefer to use MySQL in a Docker container, ensure you have Docker installed on your machine. Then, follow these steps:
-
Update the .env file with your MySQL database credentials:
MYSQL_DATABASE = {your-database-name} MYSQL_HOST = {your-database-host} MYSQL_PASSWORD = {your-password} MYSQL_PORT = {Mysql default port} MYSQL_ROOT_PASSWORD = {your-root-password} MYSQL_USER = {your-username}
-
Start the MySQL container using Docker Compose:
docker-compose up -d
This command launches a MySQL container configured to work with the FakeStore API.
If you prefer to use the default SQLite database included with the project, follow these steps:
-
Open the base.py file in the fakestore app settings directory.
-
Uncomment the default SQLite database settings by removing the comment (#) in front of the following lines and comment the MySql database settings:
# DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': BASE_DIR / 'db.sqlite3', # } # } DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': os.environ.get('MYSQL_DATABASE'), 'USER': os.environ.get('MYSQL_USER'), 'PASSWORD': os.environ.get('MYSQL_PASSWORD'), 'HOST': os.environ.get('MYSQL_HOST'), # Use the service name defined in your Docker Compose file in production 'PORT': os.environ.get('MYSQL_PORT'), # Default MySQL port } }
Run the following commands to apply database migrations:
python manage.py makemigrations
python manage.py migrate
Run the following command to create superuser for admin privileges:
python manage.py createsuperuser
Start the development server to run the FakeStore API locally:
python manage.py runserver
The API should now be running at http://127.0.0.1:8000/
.
You can access the API documentation by visiting the following URL in your web browser:
http://127.0.0.1:8000/api/
This interactive documentation provides details on available API endpoints and allows you to test them.
You can now start using the FakeStore APIs to manage various aspects of the application. Refer to the API documentation for specific endpoints and usage instructions:
- Products API Documentation
- Orders API Documentation
- Cart API Documentation
- User API Documentation
- Search API Documentation
Each API documentation provides details on available endpoints and how to use them.
The Fake Store API is powered by Django REST framework, and it offers various configuration options to control the behavior of the API. Below, we describe the key configuration settings used in this project:
By default, the API uses PageNumberPagination
to paginate large lists of data. The PAGE_SIZE
setting is configured to limit the number of items per page to 10. This means that when querying a list of resources, such as products or orders, only 10 items will be shown per page. Users can navigate through the pages by using pagination links.
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
}
To enable filtering of data in the API, we use the DjangoFilterBackend
provided by the django_filters
package. This allows users to filter lists of resources based on specific criteria, such as filtering products by category or filtering orders by status.
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
),
}
Throttling is applied to control the rate at which API requests can be made. It helps prevent abuse and ensure fair usage of the API. We have defined two throttle classes: AnonRateThrottle
and UserRateThrottle
. Anonymous users are limited to 100 requests per day, while authenticated users are allowed up to 1000 requests per day.
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
For detailed information on the models used in the FakeStore API, please refer to the Model Documentation.
If you'd like to contribute to the development of the FakeStore API, please follow the guidelines in the CONTRIBUTING.md file.
This project is licensed under the MIT License - see the LICENSE.md file for details.