Simply: you must NEVER have a client make direct calls to OpenAI. Not putting an API key in code, not putting an API key in memory, not putting an API key over the wire. Otherwise you are providing essentially your bank account password for all the snooping someone wants, to then make whatever unmoderated calls will empty your account balance and get it banned for safety violations.
Hi!
Since this is a question that has been asked very frequently over the years there is a dedicated help page guiding developers into the right direction.
Try redwoodsdk, pure typescript/ react server components built on top of vite and cloudflare. Full stack in the true sense of the word you don’t need an external server, simply store your key in your workers secrets and call a server component via an env variable.