How Setup Gemini AI for REST API using NodeJs
Tags: ai, javascript, cloud
Published: 2024-02-27 10:30:37


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye02pzcs2n9h94y6iza5.png)

Project Prerequisites:

- Node.js v18+
- npm
 
Setting up Gemini AI for a REST API using Node.js involves a few steps. Gemini AI is a machine learning platform that offers various features for building and deploying machine learning models. Here's a basic guide on how to set up Gemini AI with Node.js for a REST API:

1. **Sign up for Gemini AI**: If you haven't already, sign up for an account on the Gemini AI website (https://deepmind.google/technologies/gemini/#introduction) and Build with Gemini (https://ai.google.dev/).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2rhaspby9liuz8zx7ek.png)



2. **Create your API Key**: Train or upload a machine learning model using the Gemini AI platform. Follow the instructions provided by Gemini AI to create and train your model based on your specific requirements.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zavblrm20voy6j132htz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivr4nwzxs1guoes33zjd.png)
For Documentation:
(https://ai.google.dev/tutorials/node_quickstart)

3. **Create your project & Install Dependencies**: In your Node.js project directory, initialize a new project if you haven't already, and install necessary dependencies:

```bash
mkdir gemini_nodes_api
npm init -y
npm install express axios body-parser
```

4. **Set Up Your Node.js Server**: Create a new file, `index.js` and .env, and set up a basic Express server:

```javascript
const express = require('express');
const bodyParser = require('body-parser');

const { GoogleGenerativeAI } = require("@google/generative-ai");

// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);

console.log(genAI);

const app = express();
const port = process.env.PORT || 3001;

// Middleware to parse JSON bodies
app.use(bodyParser.json());

// Route to handle form submissions
app.post('/submit-form', (req, res) => {
    const formData = req.body;
    
    res.json(formData);
});

app.post('/text', async (req, res) => {
  const prompt = req.body.prompt;

  try {
    const model = genAI.getGenerativeModel({ model: "gemini-pro"});
    const result = await model.generateContent(prompt);
    const response = await result.response;
    const text = response.text();

    res.json({ text });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Failed to generate text' });
  }
});


// Start the server
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

```

Replace `'API_KEY'` with the actual URL provided by Gemini AI for making predictions.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1jfxlm7sugsw4a3rl36.png)



5. **Test Your API**: Start your Node.js server by running `node server.js` in your terminal. You can then send POST requests to `http://localhost:3000/text` with the input data you want to make predictions on.

```Bash
 node index.js 
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcdltxqr83wuhwfrqyrs.png)


Remember to handle authentication, error handling, and any other necessary features according to your project requirements and Gemini AI's API documentation.

Enjoy your reading