A platform for e-commerce stores to offer carbon footprint offset options to their customers during checkout. The system consists of three main components: a FastAPI backend, an offset estimation as a micro service and a JavaScript widget for store integration.
offset-cf/
βββ api/ # FastAPI Backend Service
β βββ app/ # Application code
β β βββ __init__.py
β β βββ main.py # FastAPI app entry point
β β βββ api.py # API routes and endpoints
β β βββ models.py # SQLAlchemy & Pydantic models
β β βββ database.py # Database connection & session
β β βββ constants.py # Default configurations
β β βββ utils.py # Utility functions
β β βββ requirements.txt # Python dependencies
β βββ db/ # Database setup scripts
β β βββ setup_db.sql # Main database schema
β β βββ seed_db.sql # Sample data
β βββ Dockerfile # Container configuration
β βββ deploy.sh # AWS ECR deployment script
βββ estimator/ # AWS Lambda Function
β βββ lambda.py # Carbon offset estimation logic
βββ widget/ # Shopify store widget
βββ widget_v1_0_0.js # Main widget script
βββ config_v1_0_0.json # Default widget configuration
Purpose: Central API service that manages merchant configurations, tracks customer opt-ins, and provides reporting capabilities.
Key Features:
- Widget Configuration API: Retrieves store-specific widget settings
- Opt-in Tracking: Records customer carbon offset preferences
- Monthly Reporting: Provides analytics on opt-ins and estimated offsets
- Merchant Management: Supports multiple store configurations
API Endpoints:
GET /v1/widget-config- Get widget configuration by store or merchant IDPOST /v1/opt-ins- Record customer opt-in eventsGET /v1/merchant/{store}/monthly-summary- Get monthly analyticsGET /health- Health check endpoint
Database Schema:
merchants- Store information and settingswidget_configs- Widget appearance and behavior configurationopt_ins- Customer offset preferences and order data
Purpose: Serverless function that calculates carbon offset amounts based on cart subtotals.
Features:
- Configurable offset rate (default: 2% of cart value)
- Multi-currency support
Input: { "subtotal": 100.00, "currency": "USD" }
Output: { "estimated_offset": 2.000, "rate": 0.02, "currency": "USD", "estimator_version": "v0.1.0" }
Purpose: Drop-in script that integrates with e-commerce stores to offer carbon offset options.
Features:
- Automatic cart detection and integration
- Configurable placement and styling
- Real-time offset estimation
- Opt-in tracking and analytics
- Shopify-compatible design patterns
Integration: Include script with data attributes for configuration:
<script
src="widget_v1_0_0.js"
data-api-base="https://api.offsetcf.com"
data-estimate-url="https://estimate.offsetcf.com"
data-api-version="v1">
</script>- Docker and Docker Compose
- Python 3.11+
- PostgreSQL (or use Docker)
-
Clone and navigate to the project:
cd offset-cf/api -
Create environment file:
cp .env.example .env.local # if available, or create manually -
Set up environment variables:
# AWS Configuration (for deployment) AWS_PROFILE=default AWS_REGION=us-east-1 REPO_NAME=offset-cf-api IMAGE_TAG=0.1 # Database Configuration (for database connection) DB_URL=postgresql://postgres:postgres@host.docker.internal:5432/offsetcf
-
Start the database:
psql -h localhost -U postgres -d offsetcf
-
Initialize the database:
# Wait for database to be ready, then run: psql -h localhost -U postgres -d offsetcf -f db/setup_db.sql -
Run the FastAPI service:
# Using Docker docker build -t offset-cf-api:local . docker run -p 8000:8000 --env-file .env.local --name offset-cf-local offset-cf-api:local # Or locally with Python cd app pip install -r requirements.txt uvicorn main:app --reload --host 0.0.0.0 --port 8000
-
Test the API:
curl http://localhost:8000/docs # Swagger UI
The project includes a deployment script for AWS ECR:
cd api
./deploy.sh -r us-east-1 -n offset-cf-api -t 1.0 -p your-aws-profileScript Features:
- Automatic ECR repository creation
- AWS profile support
- Environment variable loading from
.env.local - Image tagging and pushing
Required AWS Permissions:
- ECR: Create repository, push images
-
Package the Lambda function:
cd estimate zip -r lambda.zip lambda.py -
Deploy via AWS CLI:
aws lambda create-function \ --function-name offset-cf-estimator \ --runtime python3.9 \ --handler lambda.lambda_handler \ --zip-file fileb://lambda.zip \ --role arn:aws:iam::ACCOUNT:role/lambda-execution-role
-
Set environment variables:
aws lambda update-function-configuration \ --function-name offset-cf-estimator \ --environment Variables='{RATE=0.02,DEFAULT_CURRENCY=USD,ESTIMATOR_VERSION=v0.0.1}'
GET /v1/widget-config?store=acme.myshopify.comResponse:
{
"placement": "#cart-footer",
"verbiage": "to offset my carbon footprint",
"theme": {"primary_color": "#4CAF50"},
"insert_position": "before",
"is_enabled": true
}POST /v1/opt-ins
Content-Type: application/json
{
"store": "acme.myshopify.com",
"cart": {"subtotal": 100.00, "currency": "USD"},
"estimated_offset": 2.000,
"estimator_version": "v1.0.0",
"session_id": "sess_123",
"customer": {"id": "cust_456", "email": "user@example.com"},
"order_ref": "order_789"
}GET /v1/merchant/acme.myshopify.com/monthly-summary?month=2024-01Response:
{
"store": "acme.myshopify.com",
"month": "2024-01",
"currency": "USD",
"totals": {
"opt_ins": 150,
"estimated_offset": 300.50
},
"daily": [
{"day": "2024-01-01", "opt_ins": 5, "estimated_offset": 10.25}
]
}API Service:
DB_URL- PostgreSQL database URL
Lambda Function:
RATE- Carbon offset rate (default: 0.02 = 2%)DEFAULT_CURRENCY- Default currency (default: USD)ESTIMATOR_VERSION- Version identifier