I am trying to use Google Gemini API to, for that.. I wrote the following code in main.js -
import { GoogleGenerativeAI } from "@google/generative-ai";// Initialize the modelconst genAI = new GoogleGenerativeAI(`${import.meta.env.GEM_API_KEY}`);const model = genAI.getGenerativeModel({ model: "gemini-pro" });async function getResponse() { const message = await model.generateContent("hi"); const response = await message.response; const text = response.text(); console.log(text);}getResponse();
I have ".env" file containing the api key in the root directory. Its is exactly like..
GEM_API_KEY=my_api_key
It isn't working.. It is showing the the following error -
main.js:9 POST https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent 400 (Bad Request)makeRequest @ @google_generative-ai.js?v=557eda8f:94generateContent @ @google_generative-ai.js?v=557eda8f:340generateContent @ @google_generative-ai.js?v=557eda8f:513getResponse @ main.js:9(anonymous) @ main.js:16Show 3 more framesShow lessmain.js:14 Uncaught (in promise) Error: [400 ] API key not valid. Please pass a valid API key. [{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID","domain":"googleapis.com","metadata":{"service":"generativelanguage.googleapis.com"}}] at makeRequest (@google_generative-ai.js?v=557eda8f:113:13) at async generateContent (@google_generative-ai.js?v=557eda8f:340:20) at async getResponse (main.js:9:21)
You can also see the screen-shot of the error message on the html rendered page's console..
the error on the html rendered page's console..
I want the response of "hi" from the gemini.. It will be something like, "Hi there, how may I help you?", in the console of the rendered page..I have cross-checked the api_key, I even created one from my another google account, and.. I have neither shared my api_key to anybody.. nor hosted it on any online platform..
So, I don't know the reason and how to help it. Not getting any help on the browser or gpt..Would really appreciate the help.