Skip to main content
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. Comprehensive Example

Here are comprehensive examples showcasing various parameters, including system instructions, safety settings, and tools.
from google import genai
from google.genai import types
import os

client = genai.Client(
    api_key=os.environ.get("KEYWORDSAI_API_KEY"),
    http_options={
        "base_url": "https://api.keywordsai.co/api/google/gemini",
    }
)

# Example: Configure tools for grounding
grounding_tool = types.Tool(
    google_search=types.GoogleSearch()
)

# Example: Comprehensive GenerateContentConfig showcasing various parameters
config = types.GenerateContentConfig(
    # System instruction to guide the model's behavior
    system_instruction="You are a helpful assistant that provides accurate, concise information about sports events.",
    
    # Sampling parameters
    temperature=0.7,
    top_p=0.95,
    top_k=40,
    
    # Output controls
    max_output_tokens=1024,
    stop_sequences=["\n\n\n"],
    
    # Tools and function calling
    tools=[grounding_tool],
    
    # Thinking configuration (for models that support it)
    thinking_config=types.ThinkingConfig(thinking_budget=0),
    
    # Safety settings
    safety_settings=[
        types.SafetySetting(
            category=types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
            threshold=types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
        ),
        types.SafetySetting(
            category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
            threshold=types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
        )
    ],
)

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Who won the euro 2024?",
    config=config,
)

print(response.text)
You can find the full source code for these examples on GitHub: