FastAPI + DocumentDB E-Commerce Demo
A modern, full-stack e-commerce application built with FastAPI and open-source DocumentDB (MongoDB-compatible), orchestrated with Docker containers.
๐งช New to this project? Start here for step-by-step instructions!
๐ฏ What You'll Learn
- Setting up a FastAPI backend and connecting it to DocumentDB using Docker Compose
- How open-source DocumentDB enables rapid iteration and seamless migration to cloud environments
- Using the DocumentDB for VS Code extension to explore, query, and manage your database visually
๐๏ธ Architecture
- Backend: FastAPI (Python 3.11+)
- Database: DocumentDB (MongoDB-compatible, PostgreSQL-based)
- ODM: Beanie (async MongoDB ODM built on Pydantic)
- Containerization: Docker & Docker Compose
- Developer Tools: DocumentDB for VS Code extension, MongoDB Playground
๐ Prerequisites
- Docker Desktop installed and running
- Python 3.11+ (for local development)
- Git
- VS Code with DocumentDB extension (recommended)
๐ Quick Start
1. Clone the Repository
git clone https://github.com/documentdb/fast-api-sample.git
cd fast-api-sample2. Set up your DocumentDB instance
# Pull the latest DocumentDB Docker image docker pull ghcr.io/documentdb/documentdb/documentdb-local:latest # Tag the image for convenience docker tag ghcr.io/documentdb/documentdb/documentdb-local:latest documentdb # Run the container with your chosen username and password docker run -dt -p 10260:10260 --name documentdb-container documentdb --username <YOUR_USERNAME> --password <YOUR_PASSWORD> docker image rm -f ghcr.io/documentdb/documentdb/documentdb-local:latest
- Click the DocumentDB icon in the VS Code sidebar
- Click "Add New Connection"
- On the navigation bar, click on "Connection String"
- Paste your connection string
mongodb://<YOUR_USERNAME>:<YOUR_PASSWORD>@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true&authMechanism=SCRAM-SHA-256
- Click on the drop-down next to your local connection and select "Create Database..."
- Enter database name and confirm (suggested: ecommerce)
- Click on the drop-down next to your created database and select "Create Collection..."
- Create your collections,
productsandcustomersand confirm.
3. Import data to DocumentDB
- For each collection, click the
Importbutton on the top-right corner. - Import the
/scripts/sample_customers.jsonfile in thecustomerscollection and/scripts/sample_products.jsonfile in theproductscollection.
4. Set Up Environment Variables
In the .env file, add your connection string, database name, and DocumentDB credentials.
Afterwards, install the requirements needed for the data to migrate to the frontend.
cd backend
pip install -r requirements.txt5. Start the Application
This will:
- Build the FastAPI backend container
- Pull and start the DocumentDB container
- Set up networking between services
- Initialize the database connection
6. Access the Application
- Final Product: http://localhost
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
๐ API Endpoints
Products
GET /api/v1/products- List all products (with pagination)GET /api/v1/products/{id}- Get a specific productPOST /api/v1/products- Create a new productPUT /api/v1/products/{id}- Update a productDELETE /api/v1/products/{id}- Delete a product
Orders
GET /api/v1/orders- List all ordersGET /api/v1/orders/{id}- Get a specific orderPOST /api/v1/orders- Create a new orderPUT /api/v1/orders/{id}/status- Update order status
Customers
GET /api/v1/customers- List all customersGET /api/v1/customers/{id}- Get a specific customerPOST /api/v1/customers- Create a new customer
๐งช Running Tests
# Run all tests docker-compose exec backend pytest # Run with coverage docker-compose exec backend pytest --cov=app --cov-report=html # Run specific test file docker-compose exec backend pytest tests/test_products.py
๐ ๏ธ Development Workflow
Using the DocumentDB for VS Code Extension (Recommended)
The DocumentDB for VS Code extension provides a powerful, integrated way to explore and manage your database directly within VS Code.
Quick Setup
-
Install the Extension:
- Open VS Code Extensions (
Ctrl+Shift+X/Cmd+Shift+X) - Search for "DocumentDB for VS Code"
- Install the extension by Microsoft (
ms-azuretools.vscode-documentdb)
- Open VS Code Extensions (
-
Connect to Your Local Database:
- Click the DocumentDB icon in the Activity Bar
- Click "Add New Connection" โ "Connection String"
- Paste:
mongodb://docdb_user:docdb_password@localhost:27017/?tls=true&tlsAllowInvalidCertificates=true&authMechanism=SCRAM-SHA-256 - Name it "E-commerce Local Dev" and connect
-
Explore Your Data:
- Expand your connection to see databases and collections
- View documents in Table, Tree, or JSON view
- Run queries with IntelliSense support
- Import/Export data as JSON
๐ Detailed Guide: See DocumentDB Extension Guide for comprehensive documentation.
Alternative: Command Line Access
Connect to DocumentDB using mongosh:
mongosh localhost:27017 -u docdb_user -p docdb_password \ --authenticationMechanism SCRAM-SHA-256 \ --tls --tlsAllowInvalidCertificates
Local Development (without Docker)
- Install dependencies:
cd backend
pip install -r requirements.txt- Start DocumentDB separately:
docker run -dt -p 27017:27017 \ --name documentdb-local \ ghcr.io/microsoft/documentdb/documentdb-local:latest \ --username docdb_user --password docdb_password
- Run the FastAPI app:
cd backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000View Logs
# All services docker-compose logs -f # Specific service docker-compose logs -f backend docker-compose logs -f documentdb
Rebuild After Code Changes
docker-compose down docker-compose up --build
๐ Project Structure
.
โโโ backend/
โ โโโ app/
โ โ โโโ __init__.py
โ โ โโโ main.py # FastAPI application entry point
โ โ โโโ core/
โ โ โ โโโ config.py # Configuration settings
โ โ โ โโโ database.py # Database connection setup
โ โ โโโ models/ # Beanie document models
โ โ โ โโโ product.py
โ โ โ โโโ order.py
โ โ โ โโโ customer.py
โ โ โโโ routers/ # API route handlers
โ โ โ โโโ products.py
โ โ โ โโโ orders.py
โ โ โ โโโ customers.py
โ โ โโโ schemas/ # Pydantic request/response schemas
โ โ โโโ product.py
โ โ โโโ order.py
โ โ โโโ customer.py
โ โโโ tests/ # Test files
โ โ โโโ conftest.py
โ โ โโโ test_products.py
โ โ โโโ test_orders.py
โ โ โโโ test_customers.py
โ โโโ requirements.txt
โ โโโ Dockerfile
โโโ docker-compose.yml
โโโ .env.example
โโโ README.md
๐ Key Features Demonstrated
DocumentDB Capabilities
- โ BSON document storage
- โ MongoDB wire protocol compatibility
- โ Complex aggregation pipelines
- โ Indexing strategies
- โ Full-text search (optional)
- โ Vector search for AI workloads (optional)
FastAPI Best Practices
- โ Async/await patterns
- โ Pydantic validation
- โ Automatic OpenAPI documentation
- โ Dependency injection
- โ Error handling
- โ CORS configuration
Docker Best Practices
- โ Multi-stage builds
- โ Environment variable management
- โ Volume mounting for development
- โ Health checks
- โ Network isolation
๐ License
This project is licensed under the MIT License.
๐ Resources
Official Documentation
DocumentDB for VS Code Extension
- Extension Marketplace: Install DocumentDB for VS Code
- GitHub Repository: microsoft/vscode-documentdb
- Extension Documentation: Official Docs
- Quickstart Guide: VS Code Extension Quick Start
- Blog Post: Meet the DocumentDB Extension
- Local Guide: docs/DOCUMENTDB_EXTENSION_GUIDE.md