How to add ChatGPT to Bubble via API Connector
Andero Avastu

Web Developer, CEO of Framify

Article

How to add ChatGPT to Bubble via API Connector

Learn how to seamlessly integrate ChatGPT into your Bubble.io application with this step-by-step guide on using the API Connector. Enhance your web app with powerful AI chatbot capabilities.

    Understanding ChatGPT and Bubble

    Introduction to ChatGPT for Web Applications

    Chatbots have become an essential part of modern web applications, providing instant customer support and enhancing user engagement. ChatGPT, powered by OpenAI, is a cutting-edge AI that mimics human-like conversations, making it an ideal choice for web app integration. In this blog post, we'll explore how to add ChatGPT to a Bubble.io app through the API Connector so that you can leverage the power of artificial intelligence in your no-code project.

    Setting Up Your OpenAI Account for ChatGPT Integration

    Creating an Account with OpenAI

    Before you can integrate ChatGPT with your Bubble app, you'll need to set up an account with OpenAI. Simply visit the OpenAI website and sign up for an account. After verifying your email, you will have access to various AI models including ChatGPT. It's a straightforward process, and once your account is active, you're ready to move on to the next step.

    Generating Your API Keys for ChatGPT Access

    API keys are like secret passwords that allow your Bubble app to talk to ChatGPT. Generating these is crucial for a secure and successful integration. Inside your OpenAI dashboard, navigate to the API section and generate a new key. Make sure to store this key safely—it's your private gateway to leveraging ChatGPT within your Bubble application. Remember, these keys should never be shared or exposed to the public.

    Introduction to Bubble's API Connector Plugin

    Exploring Bubble's Plugin Ecosystem

    Bubble's plugin ecosystem is vast and varied, offering a multitude of tools to enhance your application's capabilities. Plugins range from simple widgets to complex APIs, and one of the most powerful among them is the API Connector. This plugin acts as a bridge between your Bubble app and external services, like ChatGPT, allowing you to send and receive data programmatically.

    Installing and Configuring the API Connector Plugin

    To start the integration process, you'll need to install the API Connector Plugin from Bubble's marketplace. Once installed, you'll find it among your other plugins, ready to be configured. The setup may seem technical, but we'll guide you through it step by step, making the process approachable for even bubble beginners.

    Configuring ChatGPT API in Bubble

    Understanding API Configuration Parameters

    Configuring the API involves understanding various parameters such as API base URLs, headers, body formats, and more. These parameters help define how your app communicates with ChatGPT, ensuring the requests made are understood and the responses are received correctly. It might sound complex, but we'll break it down into easily digestible steps.

    In this example we are setting our "Use as" Data, "Data type" JSON, "Request" POST.

    Setting Up API Endpoints for ChatGPT

    An API endpoint is a specific path to which your application sends a request. For ChatGPT, you'll need to set up endpoints for sending user messages and receiving AI responses. Bubble's API Connector makes this process largely intuitive, and we'll show you how to define these endpoints so your app can start having conversations with ChatGPT.

    Lets set our Endpoint as https://api.openai.com/v1/chat/completions

    Securely Storing API Keys in Bubble

    Security should be a top priority when handling API keys. In Bubble, you can securely store these keys by using the 'Private' feature. This ensures that your keys are not exposed in your app's front-end code and are only used where absolutely necessary. We'll take a look at best practices for keeping your keys under wraps while enabling the ChatGPT functionality.

    Implementing Headers for ChatGPT API

    Authorization headers are a crucial part of API security. They act as an additional layer of authentication, confirming that the request sent to ChatGPT is authorized. Setting up these headers in the API Connector involves adding your API key in a specific format. We'll guide you through implementing these headers correctly for secure communication with the ChatGPT API.

    Set your Headers like so, replacing the sk-.. with your own key:
    Content-Type   application/json

    Authorization  Bearer sk-....

    Configuring the Body

    Firstly choose the most suitable model for you, those can be found on OpenAI's website. After you've found your model it is time to think of the system prompt, what will the Chat bot do for you - whether will it be to assist users, by replying to their messages as in conversation or formatting some kind of text.
    {
        "model": "gpt-4-1106-preview",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant, you will help users with questions related to Web development"     }
        ]
    }

    Now for the most part our part here is done and we can click on "Initialize Call", if everything was done correctly we should see a popup, which we can just close

    Debugging API Connection Issues

    Even with careful setup, you might encounter issues where your Bubble app isn't communicating properly with ChatGPT. Debugging is a normal part of the development process, and Bubble provides tools to help you troubleshoot these problems. Through logs and test features in the API Connector, you can identify where things are going wrong and take steps to correct them.

    Verifying Successful ChatGPT Responses

    Once you've resolved any connection issues, it's time to build out the full chatbot experience. Lets build it & send some messages through your app's interface and check if ChatGPT's responses come through as expected. Successful responses are a good sign that your integration is correctly configured. Testing with different types of queries will help you ensure robust functionality.

    Building the User Interface in Bubble

    Introduction

    A user-friendly interface is essential for a successful chatbot experience. In Bubble, you can design input fields where users will type their messages to ChatGPT. We'll provide tips on creating intuitive and appealing input fields that blend seamlessly with the rest of your application's design, ensuring a cohesive look and feel.

    Once a user sends a message, it's important to display ChatGPT's response in a way that's easy to read and understand. Bubble's visual programming capabilities allow you to create dynamic text elements that update in real-time as ChatGPT replies. We'll cover how to set these up, so your chatbot conversations flow naturally.

    Creating a Database table

    Lets start by creating an extremely simple database table. You may want to modify it later, so users could have multiple sessions, but in this example we are just showing how to have a continious conversation with the AI.

    Bubble.io Database type

    Creating a Chat interface

    Here you can let your imagination flow, but you can also find this chat template from Framify.io and simply copy it into your project with only small modifications needed. Framify Component ID: xqeyv7jxk8e462i

    Firstly we need to pull in our Chats data from the database set conditional formatting to the chats so only Assistant's or User's message would be shown based on the data and show the text & image accordingly.

    Conditional formatting in bubble

    Like you can see on the picture above, we need to store a Loading state, where you are going to save it, is up to you. Loading state can be added by selecting an element and clicking the "i" icon on the top right corner. Set the state to be yes/no, default being no.

    Creating the workflows

    Now here things might get difficult for some. When user has typed their input and pressed "Send" button we start a workflow, where we first set the Loading state to yes after which we will create a new entry to the database, with the user input's value and setting the role to User.

    After we have made an entry to the database we will make another one with some modifications. More specifically this next entry message needs to be an API Request to the Endpoint we created previously. In the JSON Body we will now add the Repeating Grid where our chats are in and :format as text, after the system input, where we will enter such data

    ,{
         "role": "This Chat's role",
         "content": This Chat's message:formatted as JSON-safe
    }

    Now we can set the Loading state back to No again, and that is it!