Get started with Genkit
This guide shows you how to get started with Genkit in your preferred language and test it in the Developer UI.
Prerequisites
Section titled âPrerequisitesâBefore you begin, make sure your environment meets these requirements:
- Go 1.24 or later (Download and install)
This guide assumes youâre already familiar with building Go applications.
Set up your project
Section titled âSet up your projectâInitialize a new Go project directory:
mkdir genkit-intro && cd genkit-intro
go mod init example/genkit-introCreate a main.go file for your application entry point.
Install Genkit packages
Section titled âInstall Genkit packagesâFirst, install the Genkit CLI. This gives you access to local developer tools, including the Developer UI:
curl -sL cli.genkit.dev | bashThen, install the Genkit package for Go:
go get github.com/firebase/genkit/goThis provides Genkit core capabilities and access to Google AI Gemini models.
Configure your model API key
Section titled âConfigure your model API keyâGenkit can work with multiple model providers. This guide uses the Gemini API, which offers a generous free tier and doesnât require a credit card to get started.
To use it, youâll need an API key from Google AI Studio:
Get a Gemini API Key
Once you have a key, set the GEMINI_API_KEY environment variable:
export GEMINI_API_KEY=<your API key>Create your first application
Section titled âCreate your first applicationâA flow is a special Genkit function with built-in observability, type safety, and tooling integration.
Create a main.go file with the following sample code:
package main
import ( "context" "encoding/json" "fmt" "log" "net/http"
"github.com/firebase/genkit/go/ai" "github.com/firebase/genkit/go/genkit" "github.com/firebase/genkit/go/plugins/googlegenai" "github.com/firebase/genkit/go/plugins/server")
// Define input schematype RecipeInput struct { Ingredient string `json:"ingredient" jsonschema:"description=Main ingredient or cuisine type"` DietaryRestrictions string `json:"dietaryRestrictions,omitempty" jsonschema:"description=Any dietary restrictions"`}
// Define output schematype Recipe struct { Title string `json:"title"` Description string `json:"description"` PrepTime string `json:"prepTime"` CookTime string `json:"cookTime"` Servings int `json:"servings"` Ingredients []string `json:"ingredients"` Instructions []string `json:"instructions"` Tips []string `json:"tips,omitempty"`}
func main() { ctx := context.Background()
// Initialize Genkit with the Google AI plugin g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}), genkit.WithDefaultModel("googleai/gemini-flash-latest"), )
// Define a recipe generator flow recipeGeneratorFlow := genkit.DefineFlow(g, "recipeGeneratorFlow", func(ctx context.Context, input *RecipeInput) (*Recipe, error) { // Create a prompt based on the input dietaryRestrictions := input.DietaryRestrictions if dietaryRestrictions == "" { dietaryRestrictions = "none" }
prompt := fmt.Sprintf(`Create a recipe with the following requirements: Main ingredient: %s Dietary restrictions: %s`, input.Ingredient, dietaryRestrictions)
// Generate structured recipe data using the same schema recipe, _, err := genkit.GenerateData[Recipe](ctx, g, ai.WithPrompt(prompt), ) if err != nil { return nil, fmt.Errorf("failed to generate recipe: %w", err) }
return recipe, nil })
// Run the flow once to test it recipe, err := recipeGeneratorFlow.Run(ctx, &RecipeInput{ Ingredient: "avocado", DietaryRestrictions: "vegetarian", }) if err != nil { log.Fatalf("could not generate recipe: %v", err) }
// Print the structured recipe recipeJSON, _ := json.MarshalIndent(recipe, "", " ") fmt.Println("Sample recipe generated:") fmt.Println(string(recipeJSON))
// Start a server to serve the flow and keep the app running for the Developer UI mux := http.NewServeMux() mux.HandleFunc("POST /recipeGeneratorFlow", genkit.Handler(recipeGeneratorFlow))
log.Println("Starting server on http://localhost:3400") log.Println("Flow available at: POST http://localhost:3400/recipeGeneratorFlow") log.Fatal(server.Start(ctx, "127.0.0.1:3400", mux))}This code sample:
- Defines reusable input and output schemas using Go structs with JSON schema tags
- Configures the
gemini-flash-latestmodel as the default - Defines a Genkit flow to generate a structured recipe based on your input
- Runs the flow with a sample input and prints the structured result
Why use flows?
Section titled âWhy use flows?â- Type-safe inputs and outputs: Define clear schemas for your data
- Integrates with the Developer UI: Test and debug flows visually
- Easy deployment as APIs: Deploy flows as HTTP endpoints
- Built-in tracing and observability: Monitor performance and debug issues
Run your application
Section titled âRun your applicationâRun your application to see it in action:
go run .You should see a structured recipe output in JSON format, followed by the server starting on http://localhost:3400. The server will continue running to serve your flow as an HTTP endpoint and enable the Developer UI.
Test your flow via HTTP
Section titled âTest your flow via HTTPâWith the server running, you can test your flow by making HTTP requests to it. This demonstrates how your flow works as a web API.
In a new terminal window (while keeping your Go application running), try this curl command:
curl -X POST "http://localhost:3400/recipeGeneratorFlow" \ -H "Content-Type: application/json" \ -d '{"data": {"ingredient": "tomato", "dietaryRestrictions": "vegan"}}'You should receive a JSON response with a generated recipe. This shows how your Genkit flow can be called from any HTTP client, making it easy to integrate into web applications, mobile apps, or other services.
Test in the Developer UI
Section titled âTest in the Developer UIâThe Developer UI is a local tool for testing and inspecting Genkit components, like flows, with a visual interface.
Start the Developer UI
Section titled âStart the Developer UIâThe Genkit CLI is required to run the Developer UI. If you followed the installation steps above, you already have it installed.
Run the following command from your project root:
genkit start -- go run .This starts your app and launches the Developer UI at http://localhost:4000 by default. The server will continue running on http://localhost:3400 to serve your flows and enable the Developer UI to inspect them.
Run and inspect flows
Section titled âRun and inspect flowsâIn the Developer UI:
-
Select
recipeGeneratorFlowfrom the list of flows. -
Enter sample input:
{"ingredient": "avocado","dietaryRestrictions": "vegetarian"} -
Click Run
Youâll see the generated recipe as structured output, along with a visual trace of the AI generation process for debugging and optimization.
Next steps
Section titled âNext stepsâNow that youâve created and tested your first Genkit application, explore more features to build powerful AI-driven applications:
- Developer tools: Set up your local workflow with the Genkit CLI and Dev UI.
- Generating content: Use Genkitâs unified generation API to work with multimodal and structured output across supported models.
- Creating flows: Learn about streaming flows, schema customization, deployment options, and more.
- Tool calling: Enable your AI models to interact with external systems and APIs.
- Managing prompts with Dotprompt: Define flexible prompt templates using
.promptfiles or code.