A lightweight FastAPI-inspired REST API framework with automatic validation, built-in security, and OpenAPI documentation.
Author: KM Fazle Rabbi
GitHub: @km-fazle
LinkedIn: km-fazle
pip install km-pyapi
git clone https://github.com/km-fazle/Python-API-Framework.git
cd Python-API-Framework
pip install -e .
- π FastAPI-based: Built on top of FastAPI for high performance
- π JWT Authentication: Secure token-based authentication
- π Automatic Validation: Request/response validation with Pydantic
- π OpenAPI Documentation: Auto-generated API docs at
/docs
- ποΈ SQLAlchemy ORM: Database integration with SQLAlchemy
- π§ͺ Comprehensive Testing: Full test suite with pytest
- π§ Easy Configuration: Environment-based configuration
- π‘οΈ Security: Password hashing, CORS, and input validation
- π¦ Production Ready: Proper packaging and deployment setup
- Install the package:
pip install km-pyapi
- Create a new project directory:
mkdir my-api-project
cd my-api-project
- Set up environment variables (optional):
# Create .env file with your configuration
echo "DATABASE_URL=sqlite:///./app.db" > .env
echo "SECRET_KEY=your-secret-key-here" >> .env
km-pyapi --reload
km-pyapi --host 0.0.0.0 --port 8000
The API will be available at:
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
Method | Endpoint | Description |
---|---|---|
POST | /api/v1/auth/register |
Register a new user |
POST | /api/v1/auth/token |
Login and get access token |
GET | /api/v1/auth/me |
Get current user info |
GET | /api/v1/auth/users |
Get all users (admin) |
Method | Endpoint | Description |
---|---|---|
GET | /api/v1/items/ |
Get all items |
POST | /api/v1/items/ |
Create new item |
GET | /api/v1/items/my-items |
Get user's items |
GET | /api/v1/items/{id} |
Get specific item |
PUT | /api/v1/items/{id} |
Update item |
DELETE | /api/v1/items/{id} |
Delete item |
Method | Endpoint | Description |
---|---|---|
GET | / |
Root endpoint |
GET | /health |
Health check |
GET | /api/v1/health |
API health check |
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"email": "john@example.com",
"password": "securepassword123"
}'
curl -X POST "http://localhost:8000/api/v1/auth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=john_doe&password=securepassword123"
curl -X POST "http://localhost:8000/api/v1/items/" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "My First Item",
"description": "This is a description of my item"
}'
curl -X GET "http://localhost:8000/api/v1/items/" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
The framework uses Pydantic settings for configuration. You can configure it through environment variables or a .env
file:
# Database
DATABASE_URL=sqlite:///./app.db
# JWT Settings
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
# API Settings
API_V1_STR=/api/v1
PROJECT_NAME=KM PyAPI Framework
# CORS
BACKEND_CORS_ORIGINS=["*"]
# Environment
ENVIRONMENT=development
DEBUG=true
If you're developing the framework:
# Clone the repository
git clone https://github.com/km-fazle/Python-API-Framework.git
cd Python-API-Framework
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=py_api_framework
py_api_framework/
βββ py_api_framework/
β βββ __init__.py # Package initialization
β βββ main.py # FastAPI application
β βββ config.py # Configuration settings
β βββ database.py # Database setup
β βββ auth.py # Authentication logic
β βββ models.py # SQLAlchemy models
β βββ schemas.py # Pydantic schemas
β βββ routers/
β βββ __init__.py # Routers package
β βββ auth.py # Authentication routes
β βββ items.py # Items routes
βββ tests/
β βββ __init__.py # Tests package
β βββ test_auth.py # Authentication tests
β βββ test_items.py # Items tests
βββ requirements.txt # Python dependencies
βββ setup.py # Package setup
βββ README.md # This file
The framework uses SQLAlchemy with SQLite by default. For production, you can switch to PostgreSQL or MySQL by updating the DATABASE_URL
in your configuration.
- User: Authentication and user management
- Item: Main business entity with ownership
- JWT Tokens: Secure authentication with configurable expiration
- Password Hashing: Bcrypt-based password hashing
- CORS Protection: Configurable CORS middleware
- Input Validation: Automatic request validation with Pydantic
- SQL Injection Protection: SQLAlchemy ORM prevents SQL injection
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install km-pyapi
EXPOSE 8000
CMD ["km-pyapi", "--host", "0.0.0.0", "--port", "8000"]
ENVIRONMENT=production
DEBUG=false
SECRET_KEY=your-super-secret-production-key
DATABASE_URL=postgresql://user:password@localhost/dbname
BACKEND_CORS_ORIGINS=["https://yourdomain.com"]
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
If you have any questions or need help, please open an issue on GitHub.
KM Fazle Rabbi - Full Stack Developer & Software Engineer
- π Website: kmfazle.dev
- πΌ LinkedIn: km-fazle
- π GitHub: @km-fazle
- Initial release
- JWT authentication
- CRUD operations for items
- Comprehensive test suite
- OpenAPI documentation
- PyPI package available as
km-pyapi