
How I Solved WebSocket Authentication in FastAPI (And Why Depends() Wasn't Enough)
I'm a developer transitioning from enterprise systems to building my own products. This is one of the real problems I solved along the way. The Problem I was building a multi-tenant RAG chatbot. One backend serving AI chat widgets across multiple client websites. Each widget needed to identify itself to the backend, and I assumed I'd handle that the way I always had: with a header. The stack was simple: FastAPI backend and a Vite frontend widget. That's it. I also wanted real-time streaming so users could see the AI response being generated word by word and stop it mid-stream if needed. Like we all do with ChatGPT. That meant WebSocket connections instead of plain REST APIs. In order to manage chat history, knowledge base, leads and automations, I needed three pieces of information from the start: tenant id to keep the bot in personality and constrain its knowledge base, session id to track the chat session, and anonymous user id to identify returning users. That, my friends, is what I
Continue reading on Dev.to Python
Opens in a new tab



