"It's just a fetch call, bro." β Every junior dev, moments before learning a hard lesson.
So you want to build a ChatGPT wrapper. Fair enough. It sounds embarrassingly simple β you've got an API key, a text box, and ambition. How hard can it be?
Turns out: pretty hard. Not rocket science hard, but "oh god why is my chat broken on refresh" hard.
Let's walk through the three stages of enlightenment.
π’ Stage 1: The Junior Dev Move
"Ship it. It works on my machine."
The junior dev opens a blank React project, installs axios (or doesn't, and just uses fetch ), and writes something like this:
Frontend:
const res = await fetch ( ' /api/chat ' , { method : ' POST ' , body : JSON . stringify ({ msg : input }) }); const { result } = await res . json (); setReply ( result );
Enter fullscreen mode
Exit fullscreen mode
Backend:
app . post ( ' /api/chat ' , async ( req , res ) => { const result = await ai . chat ( req . body . msg ); res .β¦