DEV Community

Cover image for How I Solved the "API Cost" Problem with a Dual-Mode Engine in FastAPI
Yousaf Khalil
Yousaf Khalil

Posted on

How I Solved the "API Cost" Problem with a Dual-Mode Engine in FastAPI

The Problem
Like many developers building portfolio projects, I wanted to integrate the Twilio SMS API. However, I faced two hurdles: 1) Managing API costs and 2) Ensuring my project remained functional for recruiters even if my credentials were inactive.

The Solution: The Dual-Mode Architecture
Instead of letting the app crash, I engineered a logic gate using Python's os.getenv.

if SID and TOKEN and NUMBER:
# Real API logic
else:
# Demo Mode logic

Key Features I Implemented:

State Simulation: In Demo Mode, the backend uses asyncio.sleep to simulate network latency, providing a high-fidelity experience.

Local Persistence: Every message—demo or real—is committed to a local SQLite database to ensure an audit trail exists.

Data Portability: I added a StreamingResponse endpoint to export database logs as a CSV file directly to the user's machine.

UI Polish: As a Graphic Designer, I couldn't settle for a basic form. I used Tailwind CSS to build a glassmorphism "Cyberpunk" console that reflects the high-stakes nature of a dispatch system.

python #fastapi #webdev #showdev #api #coding


Top comments (0)