Quickstart using the Client Libraries

This quickstart shows you how to execute a workflow and view execution results using a client library. The sample assumes you have deployed the workflow, myFirstWorkflow using the Cloud Console or the gcloud command-line tool first.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Workflows API.

    Enable the Workflows API
  5. In the Cloud Console, go to the Service accounts page.

    Go to the Create Service Account page
  6. Select Create service account.
  7. In the Service account name field, enter a name.
  8. Click Create.
  9. From the Role list, select Logging >Logs Writer.

    Note: The Role field authorizes your service account to access resources. To send logs to Cloud Logging, assign the roles/logging.logWriter role. If you are developing a production app, always grant the least permissive roles possible. For more information, see granting roles to service accounts. For a list of Workflows roles, see the Access control reference.
  10. Click Continue.
  11. Click Done.

Get the sample code

  1. Clone the sample repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

  2. Change to the directory that contains the Cloud Functions sample code:

    cd nodejs-docs-samples/workflows/quickstart/
  3. Review the sample code:

    const {ExecutionsClient} = require('@google-cloud/workflows');
    const client = new ExecutionsClient();
     * Sleeps the process N number of milliseconds.
     * @param {Number} ms The number of milliseconds to sleep.
    function sleep(ms) {
      return new Promise(resolve => {
        setTimeout(resolve, ms);
    // Execute workflow
    try {
      const createExecutionRes = await client.createExecution({
        parent: client.workflowPath(projectId, location, workflow),
      const executionName = createExecutionRes[0].name;
      console.log(`Created execution: ${executionName}`);
      // Wait for execution to finish, then print results.
      let executionFinished = false;
      let backoffDelay = 1000; // Start wait with delay of 1,000 ms
      console.log('Poll every second for result...');
      while (!executionFinished) {
        const [execution] = await client.getExecution({
          name: executionName,
        executionFinished = execution.state !== 'ACTIVE';
        // If we haven't seen the result yet, wait a second.
        if (!executionFinished) {
          console.log('- Waiting for results...');
          await sleep(backoffDelay);
          backoffDelay *= 2; // Double the delay to provide exponential backoff.
        } else {
          console.log(`Execution finished with state: ${execution.state}`);
          return execution.result;
    } catch (e) {
      console.error(`Error executing workflow: ${e}`);

The program does the following:

  1. Sets up the Workflows client libraries.
  2. Executes a workflow.
  3. Polls the workflow's execution (using exponential backoff) until the execution terminates.
  4. Prints the execution results.

Install dependencies

npm install

Run the sample

To run the sample, run the following script:


Replace the following command-line arguments:

  • PROJECT_ID (required): Project ID of the Cloud Platform project
  • CLOUD_REGION: Cloud location for the workflow (default: us-central1)
  • WORKFLOW_NAME: ID of the workflow (default: myFirstWorkflow)

For example:

node . my-custom-project us-central1 userSignupWorkflow

Example output:

Execution finished with state: SUCCEEDED
["Sunday","Sunday in the Park with George","Sunday shopping","Sunday Bloody Sunday","Sunday Times Golden Globe Race","Sunday All Stars","Sunday Night (South Korean TV series)","Sunday Silence","Sunday Without God","Sunday Independent (Ireland)"]

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.

  1. Go to the Workflows page in the Cloud Console.

  2. Select the Delete button.

  3. In the prompt that appears, enter the name of the workflow.

  4. Select Delete.

What's next