Get Started
This guide shows you how to set up a Genkit Java project and make your first AI generation call.
Prerequisites
Section titled “Prerequisites”- Java 21+
- Maven 3.6+
- An API key from your preferred model provider (e.g., OpenAI, Google AI)
Configure the Maven Repository
Section titled “Configure the Maven Repository”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.
1. Add the GitHub Packages repository
Section titled “1. Add the GitHub Packages repository”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>2. Configure authentication
Section titled “2. Configure authentication”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.
Installation
Section titled “Installation”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>Configure your API key
Section titled “Configure your API key”Set the environment variable for your model provider:
# For OpenAIexport 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-keyCreate your first application
Section titled “Create your first application”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(); }}Running with the Dev UI
Section titled “Running with the Dev UI”Install the Genkit CLI and start with the developer UI:
# Install Genkit CLInpm install -g genkit
# Start your app with Dev UIgenkit start -- mvn exec:javaThe Dev UI launches at http://localhost:4000 where you can test flows, inspect traces, and debug your AI logic.
Using Google GenAI (Gemini) instead
Section titled “Using Google GenAI (Gemini) instead”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());