All Workflows code samples

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

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

Aggregate data using a parallel loop

Separate queries to a public BigQuery dataset each return the number of words in a document, or set of documents. A shared variable allows the count of the words to accumulate and be read after all the iterations complete.

View in documentation

Assign variables

Assigns string and number values to variables. Variable assignments are executed sequentially.

View in documentation

Calculate math floor

After receiving an HTTP request, extracts input from the JSON body, calculates its math.floor, and returns the result.

Catch errors

Implements a custom exception handler based on the HTTP status code returned by a GET request. The workflow catches a potential exception and returns a predefined error message.

View in documentation

Conditional jump to a specific step

Jumps to a specific step depending on the value returned by the first step of the workflow.

View in documentation

Conditional jump to a specific step

Jumps to a specific step depending on the day of the week returned by the first step of the workflow.

View in documentation

Connector Cloud Resource Manager

Workflows connector that defines the built-in function used to access Cloud Resource Manager within a workflow.

Connector for AI Platform Training and Prediction

Workflows connector that defines the built-in function used to access VertexAI (AI Platform) within a workflow.

Connector for BigQuery

Workflows connector that defines the built-in function used to access BigQuery within a workflow.

View in documentation

Connector for BigQuery Data Transfer

Workflows connector that defines the built-in function used to access BigQuery Data Transfer within a workflow.

Connector for Cloud Build

Workflows connector that defines the built-in function used to access Cloud Build within a workflow.

View in documentation

Connector for Cloud Functions

Workflows connector that defines the built-in function used to access Cloud Functions within a workflow.

Connector for Cloud Natural Language

Workflows connector that defines the built-in function used to access Cloud Natural Language within a workflow.

Connector for Cloud Run

Workflows connector that defines the built-in function used to access Cloud Run within a workflow.

Connector for Cloud Scheduler

Workflows connector that defines the built-in function used to access Cloud Scheduler within a workflow.

Connector for Cloud Spanner

Workflows connector that defines the built-in function used to access Cloud Spanner within a workflow.

View in documentation

Connector for Cloud SQL Admin

Workflows connector that defines the built-in function used to access Cloud SQL Admin within a workflow.

Connector for Cloud Storage

Workflows connector that defines the built-in function used to access Cloud Storage within a workflow.

Connector for Cloud Tasks

Workflows connector that defines the built-in function used to access Cloud Tasks within a workflow.

View in documentation

Connector for Cloud Translation

Workflows connector that defines the built-in function used to access Cloud Translation within a workflow.

Connector for Compute Engine

Workflows connector that defines the built-in function used to access Compute Engine within a workflow.

Connector for Dataflow

Workflows connector that defines the built-in function used to access Dataflow within a workflow.

View in documentation

Connector for Document AI

Workflows connector that defines the built-in function used to access Document AI within a workflow.

Connector for Firestore

Workflows connector that defines the built-in function used to access Firestore within a workflow.

Connector for Firestore (read)

Reads from Firestore using a Workflows connector.

Connector for Firestore (write)

Writes to Firestore using a Workflows connector.

Connector for Kubernetes Engine container

Workflows connector that defines the built-in function used to access a Kubernetes Engine container within a workflow.

Connector for Pub/Sub

Workflows connector that defines the built-in function used to access Pub/Sub within a workflow.

View in documentation

Connector for Secret Manager

Workflows connector that defines the built-in function used to access Secret Manager within a workflow.

Connector for Storage Transfer Service

Workflows connector that defines the built-in function used to access Storage Transfer Service within a workflow.

Connector for Workflows definitions

Workflows connector that defines the built-in function used to access Workflows definitions.

View in documentation

Connector for Workflows executions

Workflows connector that defines the built-in function used to access Workflows executions.

Create a workflow using Python or Ruby

Creates a workflow using Python or Ruby.

Create execution

Creates a workflow execution.

Define a map

Defines a map (dictionary) that can hold a user-defined structure of variables or lists.

View in documentation

Define lists

Defines a few lists.

View in documentation

Delete a workflow using Python or Ruby.

Deletes a workflow using Python or Ruby.

Demonstrate local scope of variable created inside a for loop

Demonstrates how any variable created in a loop does not exist outside of that loop.

View in documentation

Demonstrate variable scope in a parallel step

Demonstrates the scope of a shared variable, as well as variables that are local to their respective branch scopes.

View in documentation

Explicitly step to the next workflow step

Uses the next: command to explicitly define the sequence of workflow steps and executes steps in a different order than they appear in the workflow definition.

View in documentation

Generate a random number and return result in JSON

After receiving an HTTP request, this function generates a random number between 1 and 100, and then returns the number in JSON format.

Get a workflow using Python or Ruby.

Gets a workflow using Python or Ruby.

Handle errors with custom predicate

Defines a custom error handler, including a custom predicate and custom backoff parameters.

View in documentation

Implicitly step to the next workflow step

Executes the steps of a workflow in the order that they appear in the workflow definition.

View in documentation

Invoke a Cloud Run workload that requires authentication

Makes an authenticated request within a workflow, using OpenID Connect (OIDC) to connect with Cloud Run.

View in documentation

Iterate through a list

Uses a combination of conditional jumps, variables, and the len() function to iterate through a list.

View in documentation

List workflows using Python or Ruby

Lists workflows using Python or Ruby.

Make a call to a sample API

Makes a call to a sample API. The returned day of the week is passed to the Wikipedia API. Relevant articles on Wikipedia about the current day of the week are returned.

Make an external HTTP GET request with headers

Makes an HTTP GET request with a custom header.

View in documentation

Make an external HTTP POST request

Makes a POST request to an external HTTP endpoint.

View in documentation

Multiply by 2 and return result in JSON

After receiving an HTTP request, this function extracts the input from the JSON body, multiplies it by 2, and returns the result in JSON format.

My first workflow

A basic workflow

Paginate a BigQuery result set

Uses a page token to paginate a BigQuery result set and loop through a page of results at a time.

View in documentation

Perform a parallel for:in loop

Uses a parallel for:in loop to translate text.

Perform a parallel for:range loop

Uses a parallel for:range loop to translate text, specifying the beginning and end of an iteration range.

Perform a parallel loop

Processes items in parallel using a parallel loop.

View in documentation

Perform a parallel step using branches

Executes parallel branches concurrently, with the steps in each branch executing sequentially.

View in documentation

Perform a parallel step with the secondary workflow inlined

Includes the source code for a secondary workflow directly within the main workflow for easier readability.

Perform error handling in a parallel step

Uses a try/except structure for error handling in a parallel step

View in documentation

Perform operations in parallel using branches

Executes a workflow that has multiple and different sets of steps at the same time by placing them in parallel branches.

View in documentation

Perform parallel iteration using for loops

Executes loop iterations in parallel to reduce the end-to-end execution time. A shared variable is updated in each iteration.

View in documentation

Retry after an HTTP 500 error

Implements a custom retry policy that retries HTTP requests that have returned an HTTP 500 status code.

View in documentation

Retry steps using a default retry policy

Uses a built-in retry policy for HTTP requests.

View in documentation

Retry steps with custom configuration

Uses a standard retry predicate that determines when to perform a retry, and a custom maximum number of retries and backoff parameters.

View in documentation

Run a batch translation using the Cloud Translation connector

Runs a batch translation using the Cloud Translation connector.

Run multiple BigQuery query jobs in parallel

Runs multiple BigQuery query jobs in parallel, demonstrating an improvement in performance when compared to running the jobs serially, one after the other.

Run multiple queries serially using the connector for BigQuery.

Runs multiple queries serially, one after the other, using the connector for BigQuery.

Stop a Compute Engine instance through an HTTP request

Makes an authenticated request within a workflow, using OAuth 2.0 to stop a Compute Engine instance.

Translate text using the Cloud Translation API

Makes an HTTP POST request to the Cloud Translation API to translate text from English to Russian.

Update a workflow using Python or Ruby

Updates a workflow using Python or Ruby.

Use a simple subworkflow

Implements a simple subworkflow that is defined after the main workflow definition.

View in documentation

Use an embedded switch statement to execute steps

Uses a switch structure to directly execute steps when a condition is met, without jumping to other steps.

View in documentation

Use an expression to assign a variable value

Uses an expression to assign the value of a variable previously defined.

View in documentation

Use arguments in a workflow

Accesses runtime arguments passed to the workflow as part of the execution request and declared as a parameter of the main workflow.

Use client libraries to execute a workflow

Use client libraries to execute a workflow. Polls the workflow's execution using exponential backoff until the execution terminates and then prints the results.

Use OIDC to authenticate when making a request to Cloud Functions

Makes an HTTP request using OIDC by adding an auth section to the args section of the workflow's definition, after specifying the URL.

View in documentation

Validate a translation request using a callback endpoint

Validates translation requests by supporting a callback endpoint that waits for HTTP requests to arrive at that endpoint, resuming the execution of the workflow at a later point in time.

Write to Firestore through an HTTP request

Makes an authenticated request within a workflow, using OAuth 2.0 to write to Firestore.