Skip to main content
RapidDev - Software Development Agency
bubble-tutorial

How to integrate a chatbot in Bubble

Integrate an AI chatbot into your Bubble app by connecting to the OpenAI or Claude API via the API Connector, building a chat UI with message history in a Repeating Group, maintaining conversation context by sending previous messages, and handling streaming responses for a real-time experience.

What you'll learn

  • How to connect to OpenAI or Claude API via the API Connector
  • How to build a chat UI with message history
  • How to maintain conversation context across multiple messages
  • How to handle API responses and display them in real time
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner5 min read20-25 minAll Bubble plans (API key required)March 2026RapidDev Engineering Team
TL;DR

Integrate an AI chatbot into your Bubble app by connecting to the OpenAI or Claude API via the API Connector, building a chat UI with message history in a Repeating Group, maintaining conversation context by sending previous messages, and handling streaming responses for a real-time experience.

Overview: Integrating a Chatbot in Bubble

This tutorial shows you how to embed an AI chatbot powered by OpenAI GPT or Anthropic Claude into your Bubble app. You will configure the API Connector, build a chat interface with message history, maintain conversation context, and handle responses.

Prerequisites

  • A Bubble account with an existing app
  • An OpenAI or Anthropic API key
  • API Connector plugin installed
  • Basic understanding of Bubble workflows and data types

Step-by-step guide

1

Configure the AI API in the API Connector

Go to Plugins tab, open API Connector, add a new API called ChatAI. Create a call named SendMessage: POST to https://api.openai.com/v1/chat/completions. Add header Authorization: Bearer [api_key] (Private) and Content-Type: application/json. The body includes model, messages array, and temperature. Initialize with sample data.

API Connector body
1{
2 "model": "gpt-4",
3 "messages": <messages_json>,
4 "temperature": 0.7,
5 "max_tokens": 1000
6}

Expected result: The API Connector is configured to send chat completions to OpenAI.

2

Create the chat data structure

Create a ChatSession data type with fields: user (User), title (text), created_at (date). Create a ChatMessage data type with fields: session (ChatSession), role (text: user or assistant), content (text), sent_at (date). This stores the full conversation history.

Expected result: ChatSession and ChatMessage data types exist for storing conversation history.

3

Build the chat interface

Create a chat page. Add a Repeating Group with type ChatMessage, data source: Search for ChatMessages (session = current session, sorted by sent_at ascending). In each cell, display the message content with conditional styling: user messages aligned right with a blue background, assistant messages aligned left with a gray background. Below, add a Multiline Input for the user's message and a Send button.

Pro tip: Auto-scroll to the bottom of the Repeating Group after new messages by using the Scroll to entry action on the last item.

Expected result: A chat interface displays messages with visual distinction between user and assistant messages.

4

Build the send message workflow

When Send is clicked: Action 1: Create ChatMessage (role=user, content=input value, session=current). Action 2: Build the messages_json parameter by formatting all ChatMessages in this session as a JSON array of {role, content} objects. Action 3: Call ChatAI - SendMessage with the messages array. Action 4: Create ChatMessage (role=assistant, content=API response's choices first item's message content). Action 5: Clear the input.

Expected result: User messages are sent to the AI and responses are displayed in the chat.

5

Add system prompts and conversation management

Prepend a system message to the messages array that defines the chatbot's personality and behavior: {role: system, content: You are a helpful assistant for [your app]}. Add a New Chat button that creates a new ChatSession. Show a sidebar listing previous sessions. For production chatbots with custom knowledge bases and fine-tuned responses, consider working with RapidDev.

Expected result: The chatbot follows your system prompt instructions and users can manage multiple conversations.

Complete working example

API Connector payload
1{
2 "API_Name": "ChatAI",
3 "Call_Name": "SendMessage",
4 "Method": "POST",
5 "URL": "https://api.openai.com/v1/chat/completions",
6 "Headers": {
7 "Authorization": "Bearer YOUR_API_KEY (Private)",
8 "Content-Type": "application/json"
9 },
10 "Body": {
11 "model": "gpt-4",
12 "messages": "<messages_json>",
13 "temperature": 0.7,
14 "max_tokens": 1000
15 },
16 "messages_json_format": [
17 {"role": "system", "content": "You are a helpful assistant."},
18 {"role": "user", "content": "First user message"},
19 {"role": "assistant", "content": "First assistant response"},
20 {"role": "user", "content": "Second user message"}
21 ],
22 "Data_Types": {
23 "ChatSession": {"user": "User", "title": "text", "created_at": "date"},
24 "ChatMessage": {"session": "ChatSession", "role": "text", "content": "text", "sent_at": "date"}
25 },
26 "Workflow": {
27 "Step_1": "Create ChatMessage (role=user)",
28 "Step_2": "Format all session messages as JSON array",
29 "Step_3": "Call ChatAI - SendMessage",
30 "Step_4": "Create ChatMessage (role=assistant, content=response)",
31 "Step_5": "Clear input and scroll to bottom"
32 }
33}

Common mistakes when integrating a chatbot in Bubble

Why it's a problem: Not sending conversation history with each API call

How to avoid: Build the messages array from all ChatMessages in the current session and send it with every API call.

Why it's a problem: Sending the entire conversation history without truncation

How to avoid: Limit the messages array to the most recent 20-30 messages, or use token counting to stay within the model's limit.

Why it's a problem: Exposing the API key in client-side elements

How to avoid: Always mark the API key as Private in the API Connector so it stays server-side.

Best practices

  • Mark API keys as Private in the API Connector
  • Send conversation history with each call for context continuity
  • Truncate old messages to stay within model context limits
  • Add a system prompt to define the chatbot's personality and boundaries
  • Store all messages in the database for conversation history
  • Show a loading indicator while waiting for the AI response
  • Add error handling for API timeouts and rate limits

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I am building a Bubble.io app with an AI chatbot using OpenAI's API. I need a chat interface with message history, conversation context management, and a system prompt. How do I configure the API Connector and build the chat workflows?

Bubble Prompt

Integrate an AI chatbot into my app. Connect to OpenAI's chat completions API via the API Connector. Build a chat interface with message history, send conversation context with each request, and add a configurable system prompt.

Frequently asked questions

How much does the OpenAI API cost?

GPT-4 costs $30 per million input tokens and $60 per million output tokens. GPT-3.5-turbo is significantly cheaper at $0.50/$1.50. A typical chat message costs $0.01-0.05 with GPT-4.

Can I use Claude instead of GPT?

Yes. Configure the API Connector to call Anthropic's API at https://api.anthropic.com/v1/messages with the x-api-key header and the Anthropic message format.

How do I limit what the chatbot can discuss?

Add strict instructions in the system prompt: Only answer questions about [your topic]. If asked about anything else, politely redirect to the relevant topic.

Can RapidDev help build an AI-powered app?

Yes. RapidDev specializes in Bubble development and can build sophisticated AI applications with custom chatbots, knowledge base integration, and fine-tuned model responses.

How do I handle slow API responses?

Show a typing indicator animation while the API call is in progress. Set a timeout on the API Connector call and show an error message if it takes too long.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.