Your First Project¶
Let's build a complete REST API from scratch using Projex!
What We'll Build¶
A User Management API with: - User CRUD operations - JWT authentication - PostgreSQL database - Docker support - Tests
Step 1: Create Project¶
projex create user-api \
--template fastapi \
--db postgresql \
--auth jwt \
--style full \
--author "Your Name"
What happens:
- Creates user-api/ directory
- Generates complete project structure
- Sets up virtual environment
- Initializes git repository
- Installs dependencies
Step 2: Explore Structure¶
You'll see:
user-api/
├── app/
│ ├── main.py # Application entry
│ ├── core/ # Config & database
│ ├── api/ # API routes
│ ├── models/ # Database models
│ └── schemas/ # Pydantic schemas
├── tests/ # Test files
├── .env.example # Environment template
├── Dockerfile # Docker config
├── docker-compose.yml # Docker services
├── requirements.txt # Dependencies
└── README.md # Project docs
Step 3: Configure Environment¶
# Copy environment template
cp .env.example .env
# Edit with your values
nano .env # or use any editor
Example .env:
DATABASE_URL=postgresql://user:pass@localhost:5432/userdb
SECRET_KEY=your-secret-key-here
DEBUG=True
ENVIRONMENT=development
Step 4: Start Database¶
Using Docker (Recommended)¶
Or Install PostgreSQL Locally¶
Step 5: Run Migrations¶
# Activate virtual environment
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# Run migrations
alembic upgrade head
Step 6: Start Server¶
Output:
Step 7: Test API¶
Visit API Docs¶
Open browser: http://localhost:8000/docs
You'll see Swagger UI with all endpoints!
Test Health Endpoint¶
Response:
Register User¶
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePass123",
"name": "John Doe"
}'
Login¶
curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePass123"
}'
Response:
Step 8: Add Custom Endpoint¶
Let's add a user profile endpoint!
This generates:
- app/api/v1/endpoints/profile.py
- Route handlers (GET, POST, PUT, DELETE)
- Tests
Step 9: Add Model¶
This creates:
- app/models/post.py - Database model
- app/schemas/post.py - Pydantic schemas
- Migration file
Step 10: Run Tests¶
# Install dev dependencies
pip install -r requirements-dev.txt
# Run tests
pytest
# With coverage
pytest --cov=app tests/
Output:
=============== test session starts ===============
collected 12 items
tests/test_main.py ...... [ 50%]
tests/api/test_users.py ...... [100%]
=============== 12 passed in 2.45s ===============
Step 11: Deploy with Docker¶
Step 12: Add CI/CD¶
This creates .github/workflows/ci.yml with:
- Automated tests
- Linting
- Docker build
- Deployment ready
Next Steps¶
Enhance Your API¶
# Add more models
projex add model Comment --fields text:str,post_id:int
# Add services
projex add service email --async
# Add middleware
projex add middleware cors
Add Documentation¶
Environment Management¶
Code Quality¶
Common Issues¶
Database Connection Error¶
Import Errors¶
Port Already in Use¶
What You Learned¶
✅ Created a production-ready FastAPI project
✅ Set up PostgreSQL database
✅ Configured authentication
✅ Added custom endpoints and models
✅ Ran tests
✅ Deployed with Docker
✅ Set up CI/CD