I am trying this piece of code:
const generateDescriptionSuggestion = async () => {
if (!form.title || !form.description) {
alert("Enter both a title and a description to get a suggestion!");
return;
}
const apiKey = "sk-proj-your-new-key-here"; // Replace with the new, working key
console.log("Using API key:", apiKey ? "Key exists" : "Key is missing");
const prompt = `
Create a compelling and engaging description for an offer based on the following:
Title: "${form.title}"
Current description: "${form.description}"
The description should be concise, enticing, and a maximum of 100 words long.
`;
try {
const response = await axios.post(
"https://api.openai.com/v1/chat/completions",
{
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
max_tokens: 100,
temperature: 0.7,
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
}
);
const suggestion = response.data.choices[0].message.content.trim();
setForm({ ...form, description: suggestion });
} catch (error) {
console.error("Error generating text suggestion:", error.response ? error.response.data : error.message);
alert("Could not generate a suggestion. Check the API key and try again.");
}
};
I get an error: invalid key.
The key is right, I know that.
What can be wrong?
its not a good idea to hardcode keys, have it linked to a file preferable in a 7zip folder with a password, google instructions on this or ask your ai helper) and only unlock and open it when initialising code, after initialisation close the zip file, should still be loaded into ram i believe, there are also ways that you can encrypt the key in code by pulling the key from a file encrypting it with the code and saving the encrypted code in a seperate file, and then delete the original file for the key, keys to code need to be very secure even in small programs. and from the looks of your code, it didnt contain the key, im a little confused on what you are asking exactly, do you mean you tried that exact code? as there is the comment by the ai that is helping you to replace the api key with your actual on. and please make sure you dont post your keys online if you didnt know that.
I didn´t post my key. this is just for learning.
This is just a small piece of the code but I don´t understand why I get the errror: invalid key.
Is there someway I can test my apikey?
just check in case it was reset after an api update, something the keys get wiped on updates to api endpoints, not 100% though, could just need to reset the key, theres an auto detection program they have in case the key was leaked or being used in weird ways.
What Permissions should I use?
sorry missed this post, it depends if you want the code to have ability to change settings (ill advised for basic stuff) then increase its permisions, but if its simply a straight forward code to do a specific task, just keep it as the base permissions. that way if your key does accidentally get leak its not gonna be as bad. leaks can happen especially in the learning phase, accidents happen.