The utilization of external services and APIs has become an increasingly integral aspect of software product development. Recently OpenAI’s API has become a popular tool for developers wishing to add exciting AI powered features to their applications. However, when it comes to integrating with such services, the need to address crucial security concerns arises. This becomes particularly relevant in the context of ensuring the confidentiality of API keys.
Take for example QuickBlox’s AI-enhanced React UIKit that depends on integration with OpenAI API. The UI Kit allows developers to build chat applications with speed and efficiency. OpenAI integration allows the implementation of stunning AI features, including AI Answer Assist, AI Translate and AI Rephrase. While we want developers to enjoy the full benefits of our cutting-edge AI features, we also wish to support the security of their applications. That’s why we’ve created a proxy server.
In this article we will explain further the security issue around API keys, demonstrate the value of using a proxy server, and show you how to use our React UIKit and OpenAI API Integration securely by way of our Proxy Server.
Note: This tutorial serves as a continuation of a series of guides related to QuickBlox React UIKit.
QuickBlox has introduced a series of AI extensions designed to enhance any ordinary chat application. These extensions– AI Answer Assist, AI Translate, and AI Rephrase – are powered by OpenAI and have been pre-enabled in the QuickBlox React UIKit.
You can find detailed information about these AI extensions here:
QuickBlox Documentation
AI Rephrase Implementation Guide for React Apps
AI Translate Implementation Guide for React Apps
Supercharge Your Web Chat Apps with ChatGPT AI Answer Assist
AI Answer Assist Video tutorial
As our AI enhanced React UIKit depends on OpenAI API integration, we want to ensure the security of your application. However, integrating with OpenAI API from client applications, such as React web applications, can expose you to a series of significant challenges:
Key Security: Directly passing the API key from client-side code can lead to inadvertent key exposure. This can result in unauthorized access to the OpenAI account, data theft, or even resource abuse, negatively impacting your application’s security and data integrity.
Client-Side Limitation: Calling the OpenAI API directly from the browser introduces limitations to your application’s functionality due to browser security policy restrictions (CORS). This can lead to issues with authentication, error handling, and overall performance.
To ensure security and efficient operation with the OpenAI API, it is recommended to utilize a proxy server. A proxy server acts as an intermediary between the client application and the OpenAI API, addressing the aforementioned challenges:
Key Concealment: A proxy server enables you to keep the API key in a secure server environment accessible only to authorized personnel. The client application will send requests to the proxy server, which ensures secure interaction with the OpenAI API.
Browser Restriction Overcome: The proxy server resolves CORS-related issues by allowing your application to send requests to your own server, which then forwards them to the OpenAI API. This circumvents browser security policy limitations and facilitates more flexible error handling and authentication procedures.
Now that we understand the security issues at stake and how best to overcome them, let’s delve into how to deploy a proxy server for AI features in QuickBlox React UIKit.
Getting started with OpenAI involves creating an account on their platform and obtaining API keys. These keys are crucial for securely connecting your application to OpenAI’s capabilities. Here’s a simple guide to walk you through the process:
To access the powerful AI features offered by OpenAI and integrate them into your QuickBlox React UIKit application, follow these steps:
By following these steps, you’ll be well-equipped to harness the power of OpenAI’s AI capabilities while ensuring a secure and seamless integration with your QuickBlox React UIKit project.
The developers at QuickBlox have designed a simple and easy-to-understand example application for setting up a proxy server. You can get the source code of this application by following this link: https://github.com/QuickBlox/qb-ai-assistant-proxy-server. We suggest you download this source code, and using this example, let’s see how to set up and run the proxy server.
With this application example in hand, we can now take a closer look at the process of configuring and launching the proxy server. Let’s begin by breaking down the key steps that will help you successfully set up and integrate the proxy server for secure interaction with OpenAI API in your QuickBlox React UIKit application.
QBAIPROXY was integrated with QuickBlox to establish a secure way to use the AI API. By combining QBAIPROXY with QuickBlox, a two-step process was put in place to make sure that only people with permission can use the API. This process involves checking if the person trying to use the AI API is both registered and allowed to use it. This extra step helps prevent any unauthorized individuals from interfering and keeps the API safe from potential attacks by malicious users.
To incorporate QuickBlox with the QBAIPROXY server, just follow these steps:
For your QBAIPROXY server, you’ll need to make sure you have the following tools ready:
Node.js (v16 and above): QBAIPROXY requires Node.js, a software platform for building and running JavaScript applications. You should have version 16 or a newer version installed on your system.
npm (v8 and above): npm is the package manager for Node.js that helps you manage libraries and dependencies in your projects. You’ll need npm version 8 or a newer version to work effectively with QBAIPROXY.
Having Node.js and npm at the specified versions ensures that your QBAIPROXY server can function properly and take advantage of the latest features and security updates.
After downloading the QBAIPROXY server project from the repository, the next step is to install the required libraries. To achieve this, follow these simple instructions:
Executing npm install fetches the required libraries from the internet and sets up your project environment with everything it needs to run smoothly. It’s an essential step to ensure that your QBAIPROXY server functions as intended and can effectively communicate with QuickBlox and OpenAI API.
Configuring the QBAIPROXY server involves the following steps:
SSL_KEY_FILE='' SSL_CERT_FILE=''
HOST=localhost
PORT=3000
QUICKBLOX_API_URL=https://api.quickblox.com
To use QuickBlox, you’ll need to create an app and obtain the necessary API URL.Follow the instructions in the article “How to Create an App and Use QuickBlox Admin Panel” to create your app and find the appropriate API URL.
AI_API_KEY=your_ai_api_key_here
You must get your API key from the OpenAI platform. How to get an API key, we discussed above in this article.
Following these steps will help configure your QBAIPROXY server according to your requirements. This customization ensures your server operates smoothly during development, allowing you to focus on integrating OpenAI API securely within your QuickBlox React UIKit application.
When preparing your QBAIPROXY server for production, follow these guidelines:
SSL_KEY_FILE='certificates/privatekey.pem' SSL_CERT_FILE='certificates/certificate.pem'
HOST=192.168.0.100
NOTE: If you want the server to be accessible from all network interfaces, set the HOST parameter to 0.0.0.0. This configuration is suitable for both production and development scenarios.
Following these production setup steps ensures that your QBAIPROXY server is configured securely and correctly, enabling safe and reliable integration of the OpenAI API into your QuickBlox React UIKit application.
After you’ve made changes to the .env file to configure your QBAIPROXY server, it’s time to start the server itself. Here’s what you need to do:
npm start
This command initiates the process of starting the server.
With these steps, your QBAIPROXY server will begin running, facilitating a secure connection between your QuickBlox React UIKit application and the OpenAI API. This interaction is now established and ready for you to utilize the AI capabilities effectively.
Now let’s dive into the process of integrating QuickBlox React UI Kit with the proxy server and discover the benefits of this setup for your AI-powered features.
In your QuickBlox React UIKit application, managing the integration of AI features with the proxy server is made simple through the QBConfig.ts configuration file. This file holds a dedicated section called configAIApi, where you can set up parameters for each AI feature.
For each AI feature, you have the proxyConfig configuration section, which contains the following settings:
api: This specifies which API endpoint to call for the particular AI feature.
servername: The name or URL of the proxy server you’re using.
port: The port number on which the proxy server operates.
Here’s an example of how the AI Assist Message configuration could look:
configAIApi: { AIAnswerAssistWidgetConfig: { apiKey: '', // must be empty useDefault: true, proxyConfig: { api: 'v1/chat/completions', servername: 'http://localhost', port: '3011', sessionToken: '', }, }, // ... Other AI feature configurations },
In this example, you can see that for the AI Assist Message feature, the proxyConfig is specified with the necessary parameters. You can similarly configure other AI features within the configAIApi section.
This level of customization empowers you to fine-tune each AI feature’s interaction with the proxy server, tailoring it to your application’s specific needs.
In this article, we’ve explored the essential steps to seamlessly integrate a proxy server into your QuickBlox React UIKit application, enhancing its capabilities and security while accessing AI features through OpenAI API. By incorporating the proxy server, you address concerns such as API key protection and browser limitations, creating a secure environment for AI interactions.
We’ve discussed the importance of configuring both the QBAIPROXY server and the QuickBlox React UIKit application to work in harmony. From setting up environment variables and specifying connection protocols to configuring AI feature parameters within the configAIApi section, the process ensures a robust and secure integration.
Once you’ve successfully completed the configuration process outlined in this article, your QuickBlox React UIKit application will seamlessly harness the capabilities of AI features in conjunction with the proxy server. This integration marks a significant achievement, combining the strengths of QuickBlox’s UI toolkit and the security of the proxy server architecture.
With everything in place, you’re now poised to experience the true potential of AI-driven enhancements within your application. Your users will be able to utilize AI Answer Assist, AI Translate, AI Rephrase, and other AI features securely, as your application communicates seamlessly with the OpenAI API through the proxy server.