This integration is for the Keywords AI gateway.
import from ‘nextra/components’

1. Overview

Keywords AI is compatible with the official Google GenAI SDK, enabling you to use Google’s models through our gateway. This guide will walk you through setting up the Google GenAI SDK to work with Keywords AI.

2. Quickstart

1

Step 1: Install the SDK
2

First, install the official Google GenAI SDK for TypeScript/JavaScript.
3

npm
npm install @google/generative-ai
yarn
yarn add @google/generative-ai
pnpm
pnpm add @google/generative-ai
4

Step 2: Initialize the client
5

Next, initialize the GoogleGenAI client, making sure to set the apiKey to your Keywords AI key and the baseUrl within httpOptions to the Keywords AI endpoint for Google.
6

TypeScript
import { GoogleGenAI } from "@google/genai";

const genAI = new GoogleGenAI({
  apiKey: process.env.KEYWORDSAI_API_KEY, // Your Keywords AI API key
  httpOptions: {
    baseUrl: "https://api.keywordsai.co/api/google",
  },
});
7

The baseUrl can be either https://api.keywordsai.co/api/google or https://endpoint.keywordsai.co/api/google.

3. Make Your First Request

Now you can use the genAI instance to make requests to Google’s models.
import { GoogleGenAI } from "@google/genai";

const genAI = new GoogleGenAI({
  apiKey: process.env.KEYWORDSAI_API_KEY, // Your Keywords AI API key
  httpOptions: {
    baseUrl: "https://api.keywordsai.co/api/google",
  },
});

async function run() {
  const result = await genAI.models.generateContent({
    model: "gemini-1.5-flash",
    contents: [{ role: "user", parts: [{ text: "Hello, world!" }] }]
  });

  console.log(result);
}

run();

4. Switch models

To switch between different Google models, simply change the model parameter in your request.
const result = await genAI.models.generateContent({
  model: "gemini-1.5-pro-latest", // Switched to a different model
  contents: [{ role: "user", parts: [{ text: "Tell me a joke." }] }]
});

5. Supported parameters

Keywords AI supports passing through native Google GenAI parameters and adding our own for enhanced functionality.

Keywords AI parameters

To pass Keywords AI-specific parameters, you need to include them in the extra_body of your request. This is not natively supported by the Google GenAI SDK, so you may need to make a custom request or use a different library that allows modifying the request body.

Google GenAI parameters

All standard Google GenAI parameters are supported and can be passed directly in the generateContent call.
const result = await genAI.models.generateContent({
  model: "gemini-1.5-flash",
  contents: [{ role: "user", parts: [{ text: "What is the capital of France?" }] }],
  // Google GenAI parameters
  generationConfig: {
    temperature: 0.9,
    topK: 1,
    topP: 1,
    maxOutputTokens: 2048,
  },
});

6. Next Steps