Scalable API Infrastructure for E-Commerce Tools
Digital process optimization needs a stable foundation. When we build internal tools (like price calculators, AI bots, or stock checkers), they need to be secure, fast, and reliably accessible.
In this project, I built a microservices infrastructure that serves as the foundation for such business applications. It combines the speed of Python (FastAPI) with the security of a modern reverse proxy (Caddy).
Why This Architecture?
For e-commerce companies, “time-to-market” is critical. This architecture allows new small tools (“microservices”) to be deployed extremely quickly without having to set up the complete server infrastructure from scratch each time.
- Security First: Automatic HTTPS encryption protects internal company data.
- Scalability: The Caddy server can distribute requests across multiple internal services (load balancing).
- Efficiency: FastAPI processes requests asynchronously, ideal for I/O-heavy tasks like database queries or AI integrations.
The Backend: Services in Detail
FastAPI is used as the backend framework. Here are two example business functions:
/generate: A text analysis pipeline (e.g., for automated product descriptions)./ask-ai: An interface to Large Language Models (LLM) for the internal support bot.
Note: Production code is intentionally not published here. Below you will see a simplified reference example that explains the architectural principle only.
Simplified example (example_api.py):
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()
class TextPayload(BaseModel):
text: str = Field(..., max_length=500)
class AssistantPayload(BaseModel):
prompt: str = Field(..., max_length=2000)
model: str = Field("example/free-model")
@app.post("/api/text-tools")
async def text_tools(payload: TextPayload):
value = payload.text
return {
"uppercase": value.upper(),
"length": len(value),
}
@app.get("/api/models")
async def list_models():
# In practice, free models are loaded from a provider.
return {
"models": [
{"id": "provider/model-a:free", "name": "Model A Free"},
{"id": "provider/model-b:free", "name": "Model B Free"},
]
}
@app.post("/api/assistant")
async def assistant(payload: AssistantPayload):
# In practice: validation, rate limiting, provider request, error handling.
return {"result": f"Explanation for model {payload.model}"}
The Web Server: Caddy
To make the API available under the same domain as the frontend, I use Caddy. We use handle_path to strip the /api prefix before forwarding the request to FastAPI.
Excerpt from the Caddy configuration:
handle_path /api/* {
reverse_proxy 127.0.0.1:8000
}
Live Demo: Text Analyzer
Enter a text to have it analyzed by the Python backend. (Make sure the updated script is running locally.)
Text Analysis API
Extension: AI Code Explainer
This second example uses the new /ask-ai endpoint. Enter a code snippet, and the backend will securely forward it to an LLM (via OpenRouter).
The System Prompt: A major advantage of this architecture is that we can assign the model a fixed role that the user cannot change. In the Python backend, this system prompt is prepended to every request:
“You are a code explainer. Explain the code briefly and concisely.”
This ensures that the AI always responds helpfully and focused, regardless of what the user enters. Your API key remains securely on the server.