The Problem: AI Guesses, Math Doesn't
We live in the age of LLMs. You can ask ChatGPT, "What is the carbon footprint of a cotton t-shirt?" and it will give you a convincing answer. But ask it three times, and you might get three different numbers.
For e-commerce brands and supply chain tools, "hallucinated" data isn't good enough. You need deterministic, consistent, and auditable numbers.
That’s why I built the Product Impact API. It’s a dedicated engine that uses hard-coded environmental factors to calculate a consistent "Impact Score" for products based on their materials and shipping weight.
The Tech Stack
I chose this stack for speed and type safety:
Language: Python 3.12 (because modern Python is fast).
Framework: FastAPI (for async performance and auto-generated docs).
Validation: Pydantic (to ensure no bad data gets in).
Deployment: Railway (for effortless CI/CD).
How It Works
The core logic revolves around a weighted scoring system. A product is defined by its material (e.g., Cotton, Polyester) and its weight.
Here is the Pydantic model that enforces the data structure:
Python
from pydantic import BaseModel
class Product(BaseModel):
name: str
material: str
weight_kg: float
shipping_distance_km: float
# Example payload for documentation
model_config = {
"json_schema_extra": {
"examples": [
{
"name": "Eco T-Shirt",
"material": "cotton",
"weight_kg": 0.2,
"shipping_distance_km": 1500
}
]
}
}
The calculation engine uses a dictionary lookup for materials. This is O(1) complexity—super fast compared to querying an LLM or a heavy database.
Python
MATERIAL_SCORES = {
"cotton": 4.5, # High water usage
"polyester": 5.5, # Microplastics/Oil based
"bamboo": 2.0, # Sustainable
"recycled": 1.5 # Best option
}
def calculate_impact(product: Product):
# Base score from material
base_score = MATERIAL_SCORES.get(product.material.lower(), 5.0)
# Logistics penalty (0.1 points per 100km)
transport_penalty = (product.shipping_distance_km / 100) * 0.1
total_score = base_score * product.weight_kg + transport_penalty
return round(total_score, 2)
The Challenge: CORS & Security
One hurdle I hit during development was connecting my frontend (a simple HTML/JS dashboard) to the hosted API. I kept getting 401 Unauthorized errors.
I learned that even for public APIs, you need to be careful with CORS (Cross-Origin Resource Sharing). I had to explicitly allow my frontend's origin in the FastAPI middleware:
Python
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, lock this down!
allow_methods=["*"],
allow_headers=["*"],
)
I also implemented API Key authentication using fastapi.security.api_key to prevent abuse.
The Result
The API is now live. It accepts a JSON payload and returns a detailed breakdown of the environmental cost, CO2 estimates, and a "Green Score."
You can check out the auto-generated Swagger documentation here: https://web-production-a1f91.up.railway.app/docs#/
What's Next?
I plan to expand the MATERIAL_SCORES database and add a "Suggestion Engine" that recommends greener alternatives (e.g., "Switching to Bamboo would save 1.2kg of CO2").
If you're looking for a developer who understands both backend logic and sustainability metrics, feel free to connect!

Top comments (0)