How to add an AI assistant to a Bubble app safely
Call the AI model from the server, never from the page: keep the API key private in the API Connector, or in a small backend such as a Cloudflare Worker. Send the model only what the current user is allowed to see, ground it in content you approve, let your own workflows make any changes after the user confirms, cap usage per user, and log conversations so you can check them.
What you are building
An assistant inside your app that your users can ask questions. It answers from content you approve, and perhaps from the user's own records, and it can do one or two safe jobs, such as creating a booking request. Done well, it saves your users time. Done carelessly, it can leak your API key, show one user another user's data, or run up a large AI bill. The steps below avoid all three.
1. Keep the API key off the page
Anything that reaches the browser can be read by anyone who opens the developer tools, so the key that pays for the AI must stay on the server.
- In the API Connector: use the Private key in header authentication option, and tick Private on any parameter that holds a secret. Private values stay on Bubble's server and are not sent to the browser.
- Never put a key in a text element, a custom state, an option set, a database field that users can read, or a JavaScript plugin field.
- Or use a small backend: a service such as a Cloudflare Worker holds the AI key, and your app calls the Worker with a separate secret of its own. This also gives you one place to add usage limits and logs.
2. Decide exactly what the model may see
Treat the AI model like a new contractor: give it only what the job needs.
- Send only data that the current user is already allowed to see. Build the request from searches that respect your privacy rules, never from a whole data type.
- If the call runs in a backend workflow, leave Ignore privacy rules when running the workflow switched off unless you have a strong reason, so searches still respect what the user can see.
- Assume that anything in the instructions you send the model could be repeated back to the user. Never put secrets, internal notes or other users' details in them.
3. Ground it in content you approve
Models make things up when they answer from general knowledge. Keep your answers in the app instead: a data type, such as "Assistant answer", that an admin can edit, holding your services, prices, policies and common questions. Send the relevant entries with each question, and tell the model to answer only from them and to say plainly when it does not know.
4. Let your workflows make the changes
If the assistant should create something, such as a lead or a booking request, ask the model to return the details in a fixed structure. Show them to the user, and when the user confirms, a normal Bubble workflow creates the record. The model never changes your database directly, so a confused or manipulated answer cannot delete or overwrite anything.
5. Give users a way to reach a person
Every conversation needs a way out: a contact form, a support ticket or an email. Hand off when the user asks, when the question is outside the assistant's limits, or when it is not sure.
6. Cap usage, and watch workload units
- Count requests per user per day in a field or a small data type, and stop at a sensible limit.
- Set a monthly spending limit with the AI provider, so a busy or abused month cannot surprise you.
- Each API call your app makes uses workload units. Keep the conversation history you send short, and send only the approved answers that relate to the question.
7. Log conversations and review them
Save each question and answer to a data type that only admins can read, protected by a privacy rule. Read a sample every week for the first month, and fix the approved content wherever the assistant was wrong or unsure. Tell users they are talking to an AI assistant, and mention it in your privacy policy.
What it costs to run
AI providers charge by how much text the model reads and writes. A short support conversation on a small, efficient model can cost less than a cent, while the most capable models cost many times more. Add the workload units your calls use and any backend hosting, and choose the smallest model that answers your questions well.
Before you launch
- The key is private and never reaches the browser.
- A test user cannot get another user's data out of the assistant, however the question is worded.
- The assistant says it does not know when the answer is not in your approved content.
- Any change it suggests is confirmed by the user and made by your own workflow.
- Per-user limits and a provider spending limit are switched on.
Want a second pair of eyes on what your app exposes? The Privacy Audit checks every privacy rule and data type. Or have the whole thing set up, hosted and looked after for you with the Done-for-You AI Assistant.