Stay organized with collections Save and categorize content based on your preferences.

Cloud Functions for Firebase

Cloud Functions for Firebase is a serverless framework thatlets you automatically run backend code in response to events triggered bybackground events, HTTPS requests, theAdmin SDK, orCloud Scheduler jobs. YourJavaScript, TypeScript or Python code is stored on Google Cloud infrastructureand runs in a managed environment. There's no need to manage and scale yourown servers.

Already using Cloud Functions inGoogle Cloud?Learn more about how Firebase fitsinto the picture.

Get startedUse cases

Key capabilities

Integrates Firebase features and connects Firebase with Google Cloud

The functions you write can respond to events generated by various Firebase andGoogle Cloud features, fromFirebase Authentication triggers toCloud Storage Triggers.

Integrate across Firebase features using theAdmin SDK together with Cloud Functions, and integrate with third-party services by writing your own webhooks.Cloud Functions minimizes boilerplate code, making it easier to use Firebase andGoogle Cloud inside your function.
Zero maintenanceDeploy your JavaScript, TypeScript, or Python code to our servers with one command from the command line. After that, Firebase automatically scales up computing resources to match the usage patterns of your users. You never worry about credentials, server configuration, provisioning new servers, or decommissioning old ones.
Keeps your logic private and secureIn many cases, developers prefer to control application logic on the server to avoid tampering on the client side. Also, sometimes it's not desirable to allow that code to be reverse engineered.Cloud Functions is fully insulated from the client, so you can be sure it is private and always does exactly what you want.

How does it work?

After you write and deploy a function, Google's servers begin to manage thefunction immediately. You can fire the function directly with an HTTP request,theAdmin SDK, or a scheduled job, or, in the case of background functions,Google's servers listen for events and run the function when it istriggered.

As the load increases ordecreases, Google responds by rapidly scaling the number of virtual serverinstances needed to run your function. Each function runs in isolation, in itsown environment with its own configuration.

Lifecycle of a background function

  1. You write code for a new function, selecting an event provider (such asCloud Firestore), and defining the conditions under which the function shouldexecute.
  2. When you deploy your function:
    1. TheFirebase CLI creates a.zip archive of the function code,which is then uploaded to aCloud Storage bucket (prefixed withgcf-sources) beforeCloud Functions creates anArtifact Registry repository (namedgcf-artifacts) in your project.
    2. Cloud Build retrieves the function code and builds the functionsource. You can viewCloud Build logs in theGoogle Cloud console.
    3. The container image for the built functions code is uploaded to aprivateArtifact Registry repository in your project(namedgcf-artifacts), and your new function is rolled out.
  3. When the event provider generates an event that matches the function'sconditions, the code is invoked.
  4. If the function is busy handling many events, Google creates more instancesto handle work faster. If the function is idle, instances are cleaned up.
  5. When you update the function by deploying updated code, instances for olderversions are cleaned up along with build artifacts inArtifact Registry, and replaced by new instances.
  6. When you delete the function, all instances and zip archives are cleaned up,along with related build artifacts inArtifact Registry.The connection between the function and the event provider is removed.

In addition to listening for events with a background function, you can callfunctions directly with an HTTP request or acall from the client. You can also trigger functions on a fixedschedule orenqueue task functions via theAdmin SDK.

Implementation path

Set upCloud FunctionsInstall theFirebase CLI and initializeCloud Functions in your Firebase project.
Write functionsWrite JavaScript code, TypeScript code, or Python code to handle events from Firebase services,Google Cloud services, or other event providers.
Test functionsUse thelocal emulator to test your functions.
Deploy and monitorEnable billing for your project and deploy your functions using theFirebase CLI. You can use the Google Cloud Console to view and search through your logs.

Next steps

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-03-11 UTC.