In this tutorial, we will explore how to use the Gemini API using Node.js. We will cover the basics of setting up a Node.js project, installing the necessary dependencies, and making requests to the Gemini API to fetch data.
Gemini is a cryptocurrency exchange platform that provides a REST API for developers to access market data, manage their accounts, and place trades. By using the Gemini API, you can build trading bots, analyze market trends, and automate your trading strategies.
To get started with the Gemini API, you will need to create an account on the Gemini platform and generate API keys. These keys will allow you to access the Gemini API and make requests to their endpoints. Once you have your API keys, you can start using the Gemini API in your Node.js application.
Setting up a Node.js project
First, create a new directory for your Node.js project and navigate to it in your terminal. You can do this by running the following commands:
mkdir gemini-api-tutorial
cd gemini-api-tutorial
Next, initialize a new Node.js project by running the following command:
npm init -y
This will create a package.json
file in your project directory with default settings. You can open this file in a text editor to update the project information and dependencies as needed.
Installing the necessary dependencies
To interact with the Gemini API in your Node.js application, you will need to install the axios
package. Axios is a popular library for making HTTP requests in Node.js and is a great choice for working with APIs.
To install Axios, run the following command in your project directory:
npm install axios
Making requests to the Gemini API
Now that you have installed Axios, you can start making requests to the Gemini API in your Node.js application. To do this, you will need to use your API keys to authenticate your requests.
First, create a new JavaScript file in your project directory, for example app.js
, and open it in a text editor. In this file, you can write the code to make requests to the Gemini API using Axios.
Here is an example of how you can make a request to the Gemini API to get the current price of Bitcoin (BTC) in US dollars (USD):
const axios = require('axios');
const apiKey = 'YOUR_API_KEY';
const apiSecret = 'YOUR_API_SECRET';
const baseUrl = 'https://api.gemini.com/v1';
const endpoint = '/ticker/btcusd';
const timestamp = Date.now();
const payload = `${timestamp}GET${endpoint}`;
const signature = crypto.createHmac('sha384', apiSecret).update(payload).digest('hex');
axios.get(`${baseUrl}${endpoint}`, {
headers: {
'X-GEMINI-APIKEY': apiKey,
'X-GEMINI-PAYLOAD': payload,
'X-GEMINI-SIGNATURE': signature,
},
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
In this example, we are using Axios to make a GET request to the Gemini API’s /ticker/btcusd
endpoint to fetch the current price of Bitcoin (BTC) in US dollars (USD). We are authenticating the request using our API keys and generating a signature using our API secret.
You can run this code by executing the following command in your project directory:
node app.js
This will send a request to the Gemini API and log the response data to the console. You can then use this data in your application to perform further processing or analysis.
Conclusion
In this tutorial, we have learned how to set up a Node.js project, install the necessary dependencies, and make requests to the Gemini API using Axios. By following these steps, you can start building applications that interact with the Gemini platform and leverage its market data for trading and analysis.
For more information about the Gemini API and its endpoints, you can consult the official Gemini API documentation on the Gemini website. Happy coding!
Register to save your spot for Google I/O✨➡ https://goo.gle/io24-yt
$ node server.js
Error occurred: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async makeRequest (file:///D:/PROJECTS/New%20folder%20(2)/node_modules/@google/generative-ai/dist/index.mjs:366:20)
at async generateContent (file:///D:/PROJECTS/New%20folder%20(2)/node_modules/@google/generative-ai/dist/index.mjs:801:22)
at async run (file:///D:/PROJECTS/New%20folder%20(2)/server.js:12:20) Why i am getting this error while integrating Gemini AI on Node js? @GoogleDevelopers
Pleqase Solve
this is great! thanks for the tutorial @developerfilip
Great, but where is the github link? sorry
Hey where can i get the source code for this
Hi, I've been trying to access it, but it redirects me through various pages and displays this message:
"If you reached this page after attempting to open Google AI Studio, it may be because Google AI Studio is not available in your region, or you don't meet the age requirements (18+) for access. You can learn more about the available regions in the following section and other requirements in the terms."
I am originally from the Comoros, but I am currently residing in Mayotte Island, and I am 25 years old.
Nice video! My question is how can I teach gemin to respond me in the format I have provided…in json only no other text like this.
I just have one question. how can i run this on the web like on localhost?
doesnt show how to do streaming between backend and frontend
Somebody please teach me how to like this video twice
Great work mate! Thank you for making complex stuff easy
thank you.. for being awesome
Man this is so cool! Especially being able to upload images, I look forward to developing with Gemini!
Sourse code?
we should somehow store the 'history' in a kind of compressed image, and hope they infer teh right context from it
Great! Can we also do image processing using Gemini API?
Nice video, just to point out there are lots of unnecessary "await" in the source code (''await' has no effect on the type of this expression.ts(80007))
Do I need to set up billing for the API Key to work? Right now it says 'Free of charge' as its plan. When I run the gemini-pro.js file it says that my API_KEY_INVALID with a 400 status and BadRequest for its statusText.
📍17:00
2📍12:00
where is the open source code for this