> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini Api

> LoT connector for the Google Gemini API: generate content for alarm triage and shift summaries, create embeddings, count tokens and list models with an API key

Technical Preview

## Overview

Client-mode REST API connectors for Google Gemini: triage alarms and summarize shift logs with generateContent (structured JSON output), create embeddings, count tokens and list models with a Gemini API key, run Gemini and custom anomaly models on Vertex AI (predict on your endpoint), and get Google OAuth 2.0 access tokens.

This is one of the templates in the Google Gemini API and Vertex AI REST API app (`google-gemini-api-and-vertex-ai`). Deploy it alongside the other connectors of that app when they depend on each other (for example a token connector plus an API connector).

## Auth

Gemini API: header x-goog-api-key \{value.json.api\_key} (Google AI Studio / Cloud API key; Hub secret GEMINI\_API\_KEY via GET SECRET). Vertex AI: Authorization: Bearer \{value.json.access\_token}, OAuth 2.0 access token with scope [https://www.googleapis.com/auth/cloud-platform](https://www.googleapis.com/auth/cloud-platform) for an identity with roles/aiplatform.user (\~1 h lifetime); GoogleOAuthToken connector gets it (refresh\_token grant with client id/secret/refresh token, or service-account JWT bearer grant with a JWT signed outside the broker). Hub secrets GEMINI\_API\_KEY, GOOGLE\_OAUTH\_CLIENT\_ID, GOOGLE\_OAUTH\_CLIENT\_SECRET, GOOGLE\_OAUTH\_REFRESH\_TOKEN. Never commit keys, tokens or service-account JSON.

Auth headers used by the template: `x-goog-api-key: {value.json.api_key}`.

## Template parameters

| Placeholder | Type | Description | Required |
| - | - | - | - |
| `RouteName` | string | Name of the Gemini Api connector | no |
| `BaseUrl` | string | Gemini Api base URL without trailing slash (e.g. [https://generativelanguage.googleapis.com](https://generativelanguage.googleapis.com)) | yes |

`BASE_ADDRESS` default: `https://generativelanguage.googleapis.com`.

## Events

| Event | Method | Endpoint | Request topic | Response topic | Payload fields |
| - | - | - | - | - | - |
| `GenerateContent` | POST | `/v1beta/models/{value.json.model}:generateContent` | `ai/gemini/content/generate` | `ai/gemini/content/generate/response` | `api_key`, `body`, `model` |
| `CountTokens` | POST | `/v1beta/models/{value.json.model}:countTokens` | `ai/gemini/tokens/count` | `ai/gemini/tokens/count/response` | `api_key`, `body`, `model` |
| `EmbedContent` | POST | `/v1beta/models/{value.json.model}:embedContent` | `ai/gemini/embedding/create` | `ai/gemini/embedding/create/response` | `api_key`, `body`, `model` |
| `BatchEmbedContents` | POST | `/v1beta/models/{value.json.model}:batchEmbedContents` | `ai/gemini/embedding/batch` | `ai/gemini/embedding/batch/response` | `api_key`, `body`, `model` |
| `ListModels` | GET | `/v1beta/models?pageSize={value.json.page_size}&pageToken={value.json.page_token}` | `ai/gemini/model/list` | `ai/gemini/model/list/response` | `api_key`, `page_size`, `page_token` |
| `GetModel` | GET | `/v1beta/models/{value.json.model}` | `ai/gemini/model/get` | `ai/gemini/model/get/response` | `api_key`, `model` |

## Deploy

Get the template card (JSON with `template` and `metadata`) from the broker, then deploy the connector:

```bash theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
mosquitto_pub -t '$SYS/Coreflux/Command' -m "-connectorCode GEMINI_API"
```

The deployable connector from the REST API app, with `BASE_ADDRESS` already filled in:

```lot theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR GeminiApiRestApi WITH TYPE REST_API
    ADD REST_API_CONFIG
        WITH BASE_ADDRESS "https://generativelanguage.googleapis.com"
        WITH ENABLE_CLIENT "true"
        WITH USE_SSL "true"
        WITH REQUEST_TIMEOUT '120'
        WITH DEFAULT_CONTENT_TYPE "application/json"
    ADD EVENT GenerateContent
        WITH SOURCE_TOPIC "ai/gemini/content/generate"
        WITH DESTINATION_TOPIC "ai/gemini/content/generate/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1beta/models/{value.json.model}:generateContent"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "x-goog-api-key" "{value.json.api_key}"
        WITH BODY "{value.json.body}"
    ADD EVENT CountTokens
        WITH SOURCE_TOPIC "ai/gemini/tokens/count"
        WITH DESTINATION_TOPIC "ai/gemini/tokens/count/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1beta/models/{value.json.model}:countTokens"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "x-goog-api-key" "{value.json.api_key}"
        WITH BODY "{value.json.body}"
    ADD EVENT EmbedContent
        WITH SOURCE_TOPIC "ai/gemini/embedding/create"
        WITH DESTINATION_TOPIC "ai/gemini/embedding/create/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1beta/models/{value.json.model}:embedContent"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "x-goog-api-key" "{value.json.api_key}"
        WITH BODY "{value.json.body}"
    ADD EVENT BatchEmbedContents
        WITH SOURCE_TOPIC "ai/gemini/embedding/batch"
        WITH DESTINATION_TOPIC "ai/gemini/embedding/batch/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1beta/models/{value.json.model}:batchEmbedContents"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "x-goog-api-key" "{value.json.api_key}"
        WITH BODY "{value.json.body}"
    ADD EVENT ListModels
        WITH SOURCE_TOPIC "ai/gemini/model/list"
        WITH DESTINATION_TOPIC "ai/gemini/model/list/response"
        WITH METHOD "GET"
        WITH ENDPOINT "/v1beta/models?pageSize={value.json.page_size}&pageToken={value.json.page_token}"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "x-goog-api-key" "{value.json.api_key}"
    ADD EVENT GetModel
        WITH SOURCE_TOPIC "ai/gemini/model/get"
        WITH DESTINATION_TOPIC "ai/gemini/model/get/response"
        WITH METHOD "GET"
        WITH ENDPOINT "/v1beta/models/{value.json.model}"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "x-goog-api-key" "{value.json.api_key}"
```

Deploy it with `-addConnector` (do not include `ADD METADATA`).

## Usage example

Call `GenerateContent` by publishing a JSON payload to its request topic, and read the reply on the response topic:

```bash theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
mosquitto_sub -t 'ai/gemini/content/generate/response' &
mosquitto_pub -t 'ai/gemini/content/generate' -m '{"api_key": "<api_key>", "body": {}, "model": "<model>"}'
```

## Related

* [REST API Connectors](https://docs.coreflux.org/v2.22/lot-language/routes/rest-api-routes.md)
* [REST API Apps catalog](https://docs.coreflux.org/v2.22/lot-language/routes/rest-api-apps.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.