Accessing ChatGPT API Key Securely for Public Website

50 views Asked by At

I have made a website that uses the ChatGPT API - but I am struggling to figure out how to securely use my API key without exposing it to the public. So far I have tried using AWS Lambda (and AWS Secrets Manager) in order to securely retrieve my API key. Below is the progress I have made thus far, and the current issues I am running into. Any help or guidance would be much appreciated.

Accomplished

  • The code for the website is currently working if I hard-code my API key in the code. The current website is just made using basic HTML/CSS/JS (No React, etc.)
  • My AWS Lambda function is able to return a JSON object in the format of "Key: value"

Issues (and what I have tried)

  • I have not been successful in being able to invoke/call my AWS Lambda function. I have tried using code from a mix of tutorials, guides, and ChatGPT (code I am currently using in an .msj file and trying to import it to my .js file) but nothing has worked.
  • Errors I have received:
  1. Uncaught ReferenceError: require is not defined
  2. Uncaught SyntaxError: Cannot use import statement outside a module (at reverse_dictionary_JS.js:1:1)

Solutions? At this point I had a few ideas to solve this problem but would like to get some guidance from anyone that can help.

Code I am currently using to invoke AWS Lambda function:

const AWS = require('aws-sdk'); 

AWS.config.update({ region: 'us-east-1' }); 

const lambda = new AWS.Lambda();
const params = { FunctionName: 'ChatGPT_API_KEY_lambda', Payload: JSON.stringify({ key: 'value' }) };

lambda.invoke(params, (err, data) => {
    if (err) {
        console.error(err, err.stack);
    } else {
        console.log(data); // Log the response from the Lambda function
        // Handle the response data here 
    }
});

function LambdaFunction() {
    lambda.invoke(params, (err, data) => {
        if (err) { 
            console.error(err, err.stack);
        } else {
            console.log(data); // Log the response from the Lambda function
            
            // Handle the response data here         
        }
    })
};

I expected to be able to call my AWS Lambda function, parse the response into my apiKey variable and run my code from there

0

There are 0 answers