Skip to content

Firebase Cloud Functions

Expose Genkit flows as Firebase Cloud Functions using OnCallGenkit. This lets you deploy AI-powered functions that scale automatically on Google Cloud.

<dependency>
<groupId>com.google.genkit</groupId>
<artifactId>genkit-plugin-firebase</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

You also need the Cloud Functions Framework:

<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.1.0</version>
<scope>provided</scope>
</dependency>
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.genkit.Genkit;
import com.google.genkit.ai.GenerateOptions;
import com.google.genkit.plugins.firebase.FirebasePlugin;
import com.google.genkit.plugins.firebase.functions.OnCallGenkit;
import com.google.genkit.plugins.googlegenai.GoogleGenAIPlugin;
public class GeneratePoemFunction implements HttpFunction {
private final OnCallGenkit genkitFunction;
public GeneratePoemFunction() {
Genkit genkit = Genkit.builder()
.plugin(GoogleGenAIPlugin.create(System.getenv("GEMINI_API_KEY")))
.plugin(FirebasePlugin.builder().build())
.build();
genkit.defineFlow("generatePoem", String.class, String.class,
(ctx, topic) -> genkit.generate(GenerateOptions.builder()
.model("googleai/gemini-2.0-flash")
.prompt("Write a poem about: " + topic)
.build()).getText());
this.genkitFunction = OnCallGenkit.fromFlow(genkit, "generatePoem");
}
@Override
public void service(HttpRequest request, HttpResponse response)
throws IOException {
genkitFunction.service(request, response);
}
}

OnCallGenkit supports Firebase Authentication and App Check for securing your functions:

this.genkitFunction = OnCallGenkit.fromFlow(genkit, "generatePoem")
.withAuthPolicy(authContext -> {
if (authContext.getUid() == null) {
throw new RuntimeException("Authentication required");
}
})
.enforceAppCheck(true)
.consumeAppCheckToken(true)
.withCors("https://myapp.example.com");
MethodDescription
withAuthPolicy(policy)Validate the Firebase Auth context before running the flow
enforceAppCheck(true)Reject requests without a valid App Check token
consumeAppCheckToken(true)Consume the App Check token (replay protection)
withCors(origin)Set allowed CORS origins

OnCallGenkit automatically supports streaming via Server-Sent Events (SSE). Clients that send requests with Accept: text/event-stream receive chunked responses.

Deploy with the Google Cloud CLI:

Terminal window
gcloud functions deploy generatePoem \
--runtime java21 \
--trigger-http \
--entry-point=com.example.GeneratePoemFunction \
--set-env-vars GEMINI_API_KEY=your-key

Or deploy with the Firebase CLI:

Terminal window
firebase deploy --only functions
  • Firebase project on the Blaze (pay-as-you-go) plan
  • Application Default Credentials or a service account JSON
  • GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variable (auto-detected)
  • Java 21 runtime

See the firebase sample for a Cloud Functions deployment example.