Skip to main content

Write Variables to Google Cloud Storage

What This Example Does​

This example shows how to store the Web Service module payload (process variables) as a JSON file in Google Cloud Storage. It is a useful pattern when you want to keep a copy of every chat submission for analysis, audit, or downstream processing.

Each request creates one JSON file with the current process variables. The file name is a timestamp so that no file is overwritten.

Why Use a Cloud Function?​

A Google Cloud Function gives you a safe execution environment next to Google Cloud Storage. You can extend the example later, for example to transform the data before storing it or to call further Google Cloud APIs.

Prerequisites​

  • A Google Cloud project with billing enabled.
  • A Cloud Storage bucket in that project.
  • The Node.js 18 (or newer) runtime, which comes with the needed libraries.

Deploying the Cloud Function​

  1. Create a new Cloud Function in the Google Cloud Console and choose a name.
  2. Allow unauthenticated invocations. Authentication is handled inside the function code via an Authorization header, so the function URL is not a security risk on its own. Do not publish the URL.
  3. Add the environment variables in the function configuration:
    • BUCKET_NAME: the name of your Cloud Storage bucket.
    • AUTH_SECRET: a secret value that you also set as Authorization header in the Web Service module.
  4. Paste the code below into index.js.
const { Storage } = require('@google-cloud/storage');
const functions = require('@google-cloud/functions-framework');

const bucketName = process.env.BUCKET_NAME || '';
const authSecret = process.env.AUTH_SECRET || '';

functions.http('writeVariablesToStorage', async (req, res) => {
const { authorization } = req.headers || {};

// set the Authorization header like this in the Web Service module
if (authSecret && authorization !== `Bearer ${authSecret}`) {
res.status(403).send('Forbidden');
return;
}

// req.body contains:
// req.body.params - params set in the web service module config
// req.body.variables - variables of the process instance / customer

const vars = req && req.body ? req.body.variables : null;
if (!vars) {
res.status(400).send('Missing variables');
return;
}

const storage = new Storage();
const bucket = storage.bucket(bucketName);
const fileName = `${Date.now()}.json`;

await bucket.file(fileName).save(JSON.stringify(vars), {
contentType: 'application/json',
});

res.status(200).send({ ok: true, file: fileName });
});

Using It in LoyJoy​

  1. Add a Web Service module to your process.
  2. Enter the URL of your deployed function.
  3. Set the Authorization header to Bearer <AUTH_SECRET> with the same value as in the function configuration.
  4. Publish and test: after the module runs, the JSON file with the current variables appears in your bucket.
note

This is example code. Check the bucket name, permissions, and authentication in your own Google Cloud environment before using it productively.