Skip to content

Get Started

This guide shows you how to set up a Genkit Java project and make your first AI generation call.

  • Java 21+
  • Maven 3.6+
  • An API key from your preferred model provider (e.g., OpenAI, Google AI)

Genkit Java artifacts are published to GitHub Packages. Even though the packages are public, you need to configure the repository in your project and provide authentication.

Add the following repository to your pom.xml:

<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/genkit-ai/genkit-java</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

GitHub Packages requires authentication even for public packages. Create or edit your ~/.m2/settings.xml file:

<settings>
<servers>
<server>
<id>github</id>
<username>YOUR_GITHUB_USERNAME</username>
<password>YOUR_GITHUB_TOKEN</password>
</server>
</servers>
</settings>

Replace YOUR_GITHUB_USERNAME with your GitHub username and YOUR_GITHUB_TOKEN with a personal access token (classic) that has at least the read:packages scope.

Add the following dependencies to your Maven pom.xml:

<!-- Core Genkit framework -->
<dependency>
<groupId>com.google.genkit</groupId>
<artifactId>genkit</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- Choose a model plugin (example: OpenAI) -->
<dependency>
<groupId>com.google.genkit</groupId>
<artifactId>genkit-plugin-openai</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- Choose a server plugin (example: Jetty) -->
<dependency>
<groupId>com.google.genkit</groupId>
<artifactId>genkit-plugin-jetty</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

Set the environment variable for your model provider:

Terminal window
# For OpenAI
export OPENAI_API_KEY=your-api-key
# For Google GenAI (Gemini)
export GOOGLE_GENAI_API_KEY=your-api-key
# For Anthropic (Claude)
export ANTHROPIC_API_KEY=your-api-key
import com.google.genkit.Genkit;
import com.google.genkit.GenkitOptions;
import com.google.genkit.ai.GenerateOptions;
import com.google.genkit.ai.GenerationConfig;
import com.google.genkit.ai.ModelResponse;
import com.google.genkit.plugins.openai.OpenAIPlugin;
import com.google.genkit.plugins.jetty.JettyPlugin;
import com.google.genkit.plugins.jetty.JettyPluginOptions;
public class Main {
public static void main(String[] args) throws Exception {
// Create server plugin
JettyPlugin jetty = new JettyPlugin(JettyPluginOptions.builder()
.port(8080)
.build());
// Create Genkit with plugins
Genkit genkit = Genkit.builder()
.options(GenkitOptions.builder()
.devMode(true)
.reflectionPort(3100)
.build())
.plugin(OpenAIPlugin.create())
.plugin(jetty)
.build();
// Generate text
ModelResponse response = genkit.generate(
GenerateOptions.builder()
.model("openai/gpt-4o-mini")
.prompt("Tell me a fun fact!")
.config(GenerationConfig.builder()
.temperature(0.9)
.maxOutputTokens(200)
.build())
.build());
System.out.println(response.getText());
// Start the HTTP server (blocks until stopped)
jetty.start();
}
}

Install the Genkit CLI and start with the developer UI:

Terminal window
# Install Genkit CLI
npm install -g genkit
# Start your app with Dev UI
genkit start -- mvn exec:java

The Dev UI launches at http://localhost:4000 where you can test flows, inspect traces, and debug your AI logic.

Swap the plugin to use Gemini models:

<dependency>
<groupId>com.google.genkit</groupId>
<artifactId>genkit-plugin-google-genai</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
import com.google.genkit.plugins.googlegenai.GoogleGenAIPlugin;
Genkit genkit = Genkit.builder()
.plugin(GoogleGenAIPlugin.create(System.getenv("GOOGLE_GENAI_API_KEY")))
.build();
ModelResponse response = genkit.generate(
GenerateOptions.builder()
.model("googleai/gemini-2.0-flash")
.prompt("Tell me a fun fact!")
.build());