Dear team,
...same here ... I also wanted to use chatgpt api and already had a paid openai account. so i just needed the openai api key - which one should be careful to never reveal publicly.
i made a basic webpage where the user can input a prompt, send that prompt to the server that communicates with the openai api and display the response on the web page.
this is the html
<!DOCTYPE html>
<html>
<head>
 <title>OpenAI API</title>
 <script>
  async function callAPI() {
   const promptText = document.getElementById("prompt").value;
   try {
    const response = await fetch("https://mybergland.com/chatgpt_api/serverside.prg", {
     method: "POST",
     headers: { "Content-Type": "application/json" },
     body: JSON.stringify({ prompt: promptText }),
    });
   Â
    // Check HTTP status code and log it
    console.log('HTTP Status Code:', response.status);
   Â
    // Read response as text first
    const textData = await response.text();
   Â
    // Log raw received data
    console.log('Received data:', textData);
    // Try to parse the text as JSON
    let jsonData;
    try {
     jsonData = JSON.parse(textData);
    } catch (error) {
     console.error('Invalid JSON:', textData);
     throw error;
    }
   Â
    // Do something with jsonData
    document.getElementById("result").innerText = jsonData.choices[0].message.content;
   Â
   } catch (error) {
    console.error('Fetch Error:', error);
   }
  }
 </script>
</head>
<body>
 <h1>OpenAI API </h1>
Â
 <label for="prompt">Prompt:</label>
 <input type="text" id="prompt" />
Â
 <button onclick="callAPI()">Call API</button>
 <pre id="result"></pre>
</body>
</html>
and this is serverside
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"
#define HB_CURLOPT_POST Â Â Â Â Â Â Â Â Â Â Â 47
#define HB_CURLOPT_URL Â Â Â Â Â Â Â Â Â Â Â Â 2
#define HB_CURLOPT_DL_BUFF_SETUP Â Â Â Â Â Â Â 1008
#define HB_CURLOPT_POSTFIELDS Â Â Â Â Â Â Â Â 15
#define HB_CURLOPT_WRITEFUNCTION Â Â Â Â Â Â Â 11
FUNCTION Main()
 LOCAL hPairs := AP_Body()
 LOCAL hPost := {}
 LOCAL cAPIKey := "" // Getting API Key from environment variable
 LOCAL cUrl := "https://api.openai.com/v1/chat/completions"
 LOCAL cPrompt := ""
 LOCAL cResponse := ""
Â
 LOCAL hCurl, cPayload
 LOCAL nResult := hb_jsonDecode( AP_Body(), @hPost )
 LOCAL aHeaders := ""
 LOCAL hPayload := HB_Hash()
 LOCAL aMessages := {}
 aHeaders := { "Content-Type: application/json", ;
         "Authorization: Bearer " + " }
Â
 IF nResult != 0  // Checks if the operation was successful
  IF HB_HHasKey( hPost, "prompt" )
   cPrompt := hPost[ "prompt" ]
  ENDIF
 Â
 ELSE
 Â
  cResponse := '{ "Error": "JSON decoding failed." }'
 Â
  AP_SetContentType( "application/json" )
  ?? cResponse  // Output the JSON error message and return
  RETURN NIL
 ENDIF
 aAdd( aMessages, { "role" => "system", "content" => "You are a helpful assistant." } )
 aAdd( aMessages, { "role" => "user", "content" => cPrompt } )
hb_HSet( hPayload, "model", "gpt-3.5-turbo" )
hb_HSet( hPayload, "messages", aMessages )
hb_HSet( hPayload, "max_tokens", 50 )
cPayload := hb_jsonEncode( hPayload )
Â
Â
Â
 hCurl := CURL_easy_init()
Â
 IF hCurl != NIL
Â
  // Prepare headers
Â
  // Set cURL options
  CURL_easy_setopt( hCurl, HB_CURLOPT_URL, cUrl )
  CURL_easy_setopt( hCurl, HB_CURLOPT_POST, .T. )
  CURL_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, cPayload )
  CURL_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
  CURL_easy_setopt( hCurl, HB_CURLOPT_WRITEFUNCTION, @cResponse )
  curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
  curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
  // Perform cURL request
  CURL_easy_perform( hCurl )
  cResponse = curl_easy_dl_buff_get(hCurl)
 Â
  // Cleanup
 Â
  CURL_easy_cleanup( hCurl )
  // Output response
  AP_SetContentType( "application/json" )
  AP_RPuts(cResponse)  // Send the response back
 ELSE
  cResponse := '{ "error": "Could not initialize cURL" }'
  AP_SetContentType( "application/json" )
  ?? cResponse  // Output the JSON error message
 ENDIF
Â
RETURN NIL
for me this project and information were very inspiring and helpful...
https://forums.fivetechsupport.com/viewtopic.php?f=3&t=43171&p=259779&hilit=chatgpt+reinaldo&sid=5fafcf6e61c1ca423166c2a61765081e&sid=5fafcf6e61c1ca423166c2a61765081e#p259779
kind regards and a nice weekend
ruth