All Cloud Functions code samples

This page contains code samples for Cloud Functions. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Avoid infinite retries

Discards all events older than 10 seconds.

View in documentation

Cached library

HTTP Cloud Function that uses a cached client library instance to reduce the number of connections required per function invocation.

View in documentation

Cloud Function Concepts: Function Termination

Signaling function termination in Node

View in documentation

Cloud Storage integration tests

Demonstrates how to integration test a function triggered by Cloud Storage.

Cloud Storage system tests

Demonstrates how to system test a function triggered by Cloud Storage.

Cloud Storage unit tests

Demonstrates how to unit test a function triggered by Cloud Storage.

Environment variables

Demonstrates how to use environment variables within a Cloud Function.

View in documentation

Execution after HTTP response

Example of code that won't execute after an HTTP response is sent.

Execution after timeout

Sample that demonstrates code that should (but won't) execute after a GCF call times out.

File system

Shows how to access a Cloud Functions instance's file system.

Firebase Analytics

Triggers a function when a Firebase Analytics event is received.

Firebase Auth

Triggers a function when a Firebase Auth user object changes.

View in documentation

Firebase remote config

Process changes to Firebase remote config values.

View in documentation

Firebase RTDB trigger

Triggers a function when a Firebase realtime database is updated.

View in documentation

Firestore trigger

Triggers a function in response to a Firestore database update.

View in documentation

Global versus function scope

Creates a heavy object only once per function instance, and shares it across all function invocations reaching the given instance.

Hello World error

Demonstrates how to report an error.

View in documentation

HTTP Auth with CORS

HTTP function that supports CORS requests with credentials.

HTTP Bearer token

Shows how to make Google Cloud-authenticated HTTP requests.

View in documentation

HTTP CORS

Shows how to make CORS-enabled requests with Cloud Functions.

View in documentation

HTTP integration tests

Demonstrates how to integration test an HTTP function.

View in documentation

HTTP method types

Shows how to handle HTTP method types (such as GET, PUT, and POST) in Cloud Functions.

View in documentation

HTTP parse multipart/form-data

Cloud Function that parses HTTP form requests.

View in documentation

View in documentation

HTTP system tests

Demonstrates how to system test an HTTP function.

View in documentation

HTTP unit tests

Demonstrates how to unit test an HTTP function.

View in documentation

HTTP XML

Parses an HTTP request that contains "application/xml" content.

View in documentation

ImageMagick tutorial step 1 - Analyze image

Demonstrates how to use the Google Cloud Vision API and ImageMagick to detect and blur offensive images that get uploaded to a Cloud Storage bucket.

View in documentation

ImageMagick tutorial step 2 - Blur image

Demonstrates how to use the Google Cloud Vision API and ImageMagick to detect and blur offensive images that get uploaded to a Cloud Storage bucket.

View in documentation

Lazy globals

Demonstrates waiting to initialize global variables until they're used.

View in documentation

Memorystore for Redis

Demonstrates a Cloud Function that connects to a managed Redis instance.

OCR tutorial step 1 - Process image

Demonstrates how to upload image files to Cloud Storage, and extract and translate text from the images by using the Vision API and Translation API.

OCR tutorial step 2 - Detect text

Demonstrates how to upload image files to Cloud Storage, and extract and translate text from the images by using the Vision API and Translation API.

OCR tutorial step 3 - Translate text

Demonstrates how to upload image files to Cloud Storage, and extract and translate text from the images by using the Vision API and Translation API.

OCR tutorial step 4 - Save result

Demonstrates how to upload image files to Cloud Storage, and extract and translate text from the images by using the Vision API and Translation API.

PHP Info

Get PHP info

View in documentation

Pub/Sub integration tests

Demonstrates how to integration test a function triggered by Pub/Sub.

Pub/Sub parse message

Demonstrates how to interact with Pub/Sub from Cloud Functions.

View in documentation

Pub/Sub publish message

Demonstrates how to interact with Pub/Sub from Cloud Functions.

View in documentation

Pub/Sub system tests

Demonstrates how to system test a function triggered by Pub/Sub.

Pub/Sub unit tests

Demonstrates how to unit test a function triggered by Pub/Sub.

React to value change by updating a value

Updates a value whenever it is changed in Firestore.

View in documentation

Retry on error

Tells your function whether or not to retry execution when an error happens.

View in documentation

Send HTTP requests

Shows how to make an HTTP request from a Cloud Function.

Statelessness

An example of what variable state function instances do (and don't) store.

Trigger function from Cloud Logging

Triggers a function based on Cloud Logging entries.

Use HTTP connection pooling

Shows how to recycle HTTP connections using HTTP connection pools.

View in documentation