I wanted to put a chat GPT chat window on the Wicks homepage, so I studied hard while watching the YouTuber’s video(The Wix Wiz - How To Create an AI Chatbot with OpenAI and WIX). However, there is an error when trying to run it. I even used a new API code just in case. What’s wrong with my code? 
(sorry… i can’t put over 2 pictures because i’m new user…

So)
1 Like
Hi,
Your code looks mostly correct! I noticed that you used the Wix-Secrets-Backend API which is a great way to make sure your authorization key stays secure.
The issue I see is that the authorization header uses single quotes (') instead of backquotes (`) which is required for template literals in JavaScript. Additionally, you have to make sure your API Key is stored in a variable named “apiKey” instead of “apiKei” based on your authorization header.
The following modifications should fix your code:
Line 6
const apiKey = await getSecret("OPENAI-API-KEY");
Line 17
'Authorization': `Bearer ${apiKey}`
1 Like
-
Instead of passing your API, key from backend (where it WAS secure) to frontend, do both processes on —> BACKEND → a) FETCH-REQUEST including b) SECRETS-REQUEST and send only the RESULTS you got from CHAT-GPT back to FRONT-END.
-
Your structure should look like…
a) backend-code-1 jsw-module —> openAi.jsw
b) backend-code-2 jsw-module → secrets.jsw
-
Process-flow…
Step-1:
Calling the fetch-request on FRONT-END to start the fetch-process on BACKEND.
Step-2: Inside of your fetch-request, you first call (asynchronous) → wix-secrets from secrets.jsw (which is also on backend) to get your API-Key first.
Step-3: You got your API-KEy from SECRETS.JSW → your fetch-process continues and gets all data from OPEN-AI.
Step-4: You return the found OPEN-AI-RESULTS back to → FRONT-END.
ABSOLUTELY SAFE PROCESS, WITHOUT ANY RISKS TO EXPLOITE YOUR SECRETS (API-KEYS) on —> FRONTEND !!! —> SAFE-PROCESS.
If you need a complete Open-Ai CHAT-GPT-Manager → velo-ninja@outlook.com
Fully integrated into your Wix-Website with lot of features and functions.