DEV Community

Cover image for How Setup Gemini AI for REST API using NodeJs
Adewumi Saheed Adewale
Adewumi Saheed Adewale

Posted on

How Setup Gemini AI for REST API using NodeJs

Image description

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

  1. 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

Image description
For Documentation:
(https://ai.google.dev/tutorials/node_quickstart)

  1. Create your project & Install Dependencies: In your Node.js project directory, initialize a new project if you haven't already, and install necessary dependencies:
mkdir gemini_nodes_api
npm init -y
npm install express axios body-parser
Enter fullscreen mode Exit fullscreen mode
  1. Set Up Your Node.js Server: Create a new file, index.js and .env, and set up a basic Express server:
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}`);
});

Enter fullscreen mode Exit fullscreen mode

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

Image description

  1. 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.
 node index.js 
Enter fullscreen mode Exit fullscreen mode

Image description

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

Top comments (1)

Collapse
 
kenenwogu profile image
KeneNwogu

It was a great read, concise and very clear. Thanks for this. Bookmarking for a re-read already.