How do I connect OpenAI?
Last checked 31 Aug 2026
Short answer
For most cases, use Lovable AI (the built-in AI Gateway) so users don't need their own key; if you specifically need OpenAI, ask Lovable to store your API key as a secret and call it from a server function.
Check whether you need OpenAI specifically
Lovable AI (its built-in AI Gateway) lets your app call AI models without visitors supplying their own API key, and without you managing a separate OpenAI account. For most "add AI to my app" requests — chat features, text generation, summarisation — this is the simpler starting point:
"Add an AI chat feature to the [page] page using Lovable AI."
If you specifically need OpenAI's API
Some projects need a specific OpenAI model, fine-tune, or feature Lovable AI doesn't expose. In that case:
- Get an API key from your OpenAI account.
- Ask Lovable to store it as a server-side secret.
- Ask Lovable to create a server function that sends the request to OpenAI's API and returns the result.
- Call that server function from your UI — never call OpenAI directly from front-end code.
"Create a server function that sends the user's message to OpenAI's chat completions endpoint using my API key, and return the response to the chat UI."
Cost and rate limits
OpenAI billing is separate from your Lovable credits — usage against your OpenAI key is charged by OpenAI directly. Watch for rate limits on your account tier; a spike in traffic can throttle the app if you're not monitoring usage.
What usually goes wrong
- Exposing the OpenAI key in client code, which lets anyone extract it and run up your bill.
- No usage limits per user, so a single visitor can rack up large costs by spamming a chat feature — ask Lovable to add basic rate limiting.
- Long-running or streaming responses timing out on the server function — clarify whether you need streaming support up front.
For production AI features with real usage, budget monitoring and abuse protection matter — if that's more than a quick add-on, PromptSites.com can build it properly.
Sources
Was this answer helpful?
Related questions
- AnswerHow do I install Google Tag Manager?Ask Lovable to add your GTM container snippet to the site's shared head and body, then manage individual tags (analytics, ads, pixels) from within Tag Manager itself.
- AnswerHow do I connect an API to Lovable?Describe the API and what you want it to do in Chat; Lovable will typically wire the call through a server function (Edge Function) so any API key stays off the client.
- AnswerHow do I connect Meta Pixel?Ask Lovable to add your Meta Pixel ID as a script in the site's shared head, and fire a PageView (or custom) event on each route change since Lovable apps are single-page apps.
- AnswerHow do I use webhooks?Webhooks let external services notify your Lovable app when something happens; ask Lovable to create a server function URL as the webhook endpoint, and configure that URL in the third-party service.