Trigger functions with Firestore documents (original) (raw)

Skip to main content

Trigger functions with Firestore documents

This guide shows examples of functions that are triggered when you make changes to a document inside of a specified collection.

Before you begin

Before you run the sample code in this guide, you'll need to do the following:

Examples

The following examples demonstrate how to write functions that respond to a Firestore trigger.

Example 1: Hello Firestore function

The following sample prints the fields of a triggering Firestore event:

Node.js

Python

Go

Java

C#

Deploy the Hello Firestore function

If you haven't already done so, set up yourFirestore database.

Click the tab for instructions using the tool of your choice.

Console

When you use the Google Cloud console to create a function, you can also add a trigger to your function. Follow these steps to create a trigger for your function:

  1. In the Google Cloud console, go to Cloud Run:
    Go to Cloud Run
  2. Click Write a function, and enter the function details. For more information about configuring functions during deployment, seeDeploy functions.
  3. In the Trigger section, click Add trigger.
  4. Select Firestore trigger.
  5. In the Eventarc trigger pane, modify the trigger details as follows:
    1. Enter a name for the trigger in the Trigger name field, or use the default name.
    2. Select a Trigger type from the list:
      • Google Sources to specify triggers for Pub/Sub, Cloud Storage, Firestore, and other Google event providers.
      • Third-party to integrate with non-Google providers that offer an Eventarc source. For more information, see Third-party events in Eventarc.
    3. Select Cloud Firestore from the Event provider list, to select a product that provides the type of event for triggering your function. For the list of event providers, see Event providers and destinations.
    4. Select type=google.cloud.firestore.document.v1.written from the Event type list. Your trigger configuration varies depending on the supported event type. For more information, see Event types.
    5. Leave the Event data content type field as is.
    6. In the Filters section, select a database, operation and attribute values, or use the default selections.
    7. If the Region field is enabled, select a locationfor the Eventarc trigger. In general, the location of an Eventarc trigger should match the location of the Google Cloud resource that you want to monitor for events. In most scenarios, you should also deploy your function in the same region. See Understand Eventarc locationsfor more details about Eventarc trigger locations.
    8. In the Service account field, select a service account. Eventarc triggers are linked to service accounts to use as an identity when invoking your function. Your Eventarc trigger's service account must have the permission to invoke your function. By default, Cloud Run uses the Compute Engine default service account.
    9. Optionally, specify the Service URL path to send the incoming request to. This is the relative path on the destination service to which the events for the trigger should be sent. For example:/, /route, route, and route/subroute.
  6. Once you've completed the required fields, click Save trigger.
  7. Click Create.
  8. In the Source tab, edit the source code if needed, then selectSave and redeploy.

gcloud

When you create a function using the gcloud CLI, you must firstdeploy your function, and then create a trigger. Follow these steps to create a trigger for your function:

  1. Run the following command in the directory that contains the sample code to deploy your function:
gcloud run deploy FUNCTION \  
        --source . \  
        --function FUNCTION_ENTRYPOINT \  
        --base-image BASE_IMAGE_ID \  
        --region REGION  

Replace:

  1. Run the following command to create a trigger that filters events:
gcloud eventarc triggers create TRIGGER_NAME  \  
    --location=EVENTARC_TRIGGER_LOCATION \  
    --destination-run-service=FUNCTION  \  
    --destination-run-region=REGION \  
    --event-filters=type=google.cloud.firestore.document.v1.written \  
    --event-filters=database='(default)' \  
    --event-data-content-type=application/protobuf \  
    --event-filters-path-pattern=document='users/{username}' \  
    --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com  

Replace:

Terraform

To create an Eventarc trigger for a Cloud Run function, see Create a trigger using Terraform.

Use the other fields as is:

Test the Hello Firestore function

To test the Hello Firestore function, set up a collection calledusers in your Firestore database:

  1. In the Google Cloud console, go to the Firestore databases page:
    Go to Firestore
  2. Click Start a collection.
  3. Specify users as the collection ID.
  4. To start adding the collection's first document, underAdd its first document accept the auto-generated Document ID.
  5. Add at least one field for the document, specifying a name and value. For example, in Field name, enter username, and in Field value, enter rowan.
  6. When you're done, click Save.
    This action creates a new document, thereby triggering your function.
  7. To confirm that your function was triggered, click the linked name of the function in the Google Cloud consoleCloud Run Overview page to open the Service details page.
  8. In the Observability tab, select the Logs tab and look for the following string:
    Function triggered by change to: //firestore.googleapis.com/projects/your-project-id/databases/(default)'

Example 2: Convert to Uppercase function

The following example retrieves the value added by the user, converts the string at that location to uppercase, and replaces the value with the uppercase string:

Node.js

Use protobufjs to decode the event data. Include the google.events.cloud.firestore.v1 data.protoin your source.

Python

Go

Java

C#

Deploy the Convert to Uppercase function

If you haven't already done so, set up yourFirestore database.

Click the tab for instructions using the tool of your choice.

Console

When you use the Google Cloud console to create a function, you can also add a trigger to your function. Follow these steps to create a trigger for your function:

  1. In the Google Cloud console, go to Cloud Run:
    Go to Cloud Run
  2. Click Write a function, and enter the function details. For more information about configuring functions during deployment, seeDeploy functions.
  3. In the Trigger section, click Add trigger.
  4. Select Firestore trigger.
  5. In the Eventarc trigger pane, modify the trigger details as follows:
    1. Enter a name for the trigger in the Trigger name field, or use the default name.
    2. Select a Trigger type from the list:
      • Google Sources to specify triggers for Pub/Sub, Cloud Storage, Firestore, and other Google event providers.
      • Third-party to integrate with non-Google providers that offer an Eventarc source. For more information, see Third-party events in Eventarc.
    3. Select Firestore from the Event provider list, to select a product that provides the type of event for triggering your function. For the list of event providers, see Event providers and destinations.
    4. Select type=google.cloud.firestore.document.v1.written from the Event type list. Your trigger configuration varies depending on the supported event type. For more information, see Event types.
    5. Leave the Event data content type field as is.
    6. In the Filters section, select a database, operation and attribute values, or use the default selections. If you named your database, enter the name in the Attribute value 1 field.
    7. If the Region field is enabled, select a locationfor the Eventarc trigger. In general, the location of an Eventarc trigger should match the location of the Google Cloud resource that you want to monitor for events. In most scenarios, you should also deploy your function in the same region. See Understand Eventarc locationsfor more details about Eventarc trigger locations.
    8. In the Service account field, select a service account. Eventarc triggers are linked to service accounts to use as an identity when invoking your function. Your Eventarc trigger's service account must have the permission to invoke your function. By default, Cloud Run uses the Compute Engine default service account.
    9. Optionally, specify the Service URL path to send the incoming request to. This is the relative path on the destination service to which the events for the trigger should be sent. For example:/, /route, route, and route/subroute.
  6. Once you've completed the required fields, click Save trigger.
  7. Click Create.
  8. In the Source tab, edit the source code if needed, then selectSave and redeploy.

gcloud

When you create a function using the gcloud CLI, you must firstdeploy your function, and then create a trigger. Follow these steps to create a trigger for your function:

  1. Run the following command in the directory that contains the sample code to deploy your function:
gcloud run deploy FUNCTION \  
        --source . \  
        --function FUNCTION_ENTRYPOINT \  
        --base-image BASE_IMAGE_ID \  
        --region REGION  

Replace:

  1. Run the following command to create a trigger that filters events:
gcloud eventarc triggers create TRIGGER_NAME  \  
    --location=EVENTARC_TRIGGER_LOCATION \  
    --destination-run-service=FUNCTION  \  
    --destination-run-region=REGION \  
    --event-filters=type=google.cloud.firestore.document.v1.written \  
    --event-filters=database='(default)' \  
    --event-data-content-type=application/protobuf \  
    --event-filters-path-pattern=document='messages/{pushId}' \  
    --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com  

Replace:

Terraform

To create an Eventarc trigger for a Cloud Run function, see Create a trigger using Terraform.

Use the other fields as is:

Test the Convert to Uppercase function

To test the Convert to Uppercase function you just deployed, set up a collection called messages in yourFirestore database:

  1. In the Google Cloud console, go to the Firestore databases page:
    Go to Firestore
  2. Select the Database ID for your Firestore database.
  3. Click Start a collection.
  4. Specify messages as the collection ID.
  5. To start adding the collection's first document, underAdd its first document accept the auto-generated Document ID.
  6. To trigger your deployed function, add a document where the Field name isoriginal and the Field value is minka.
  7. When you save the document, you can see the lowercase word in the value field convert to uppercase.
    If you subsequently edit the field value to contain lowercase letters, that triggers the function again, converting all lowercase letters to uppercase.

Limitations for functions

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

Last updated 2026-06-15 UTC.