--- title: "Configure the Event function" url: https://docs.catalyst.zoho.com/en/tutorials/workdrivesync/nodejs/configure-event-function/ description: "Build a web app that syncs file uploads and deletions between Catalyst Stratus and Zoho WorkDrive through APIs, using Catalyst functions and Event Listener." last_updated: 2026-11-04T10:10:44Z service: All Services type: tutorial related_links: - name: "Event Functions" url: /en/serverless/help/functions/event-functions --- # Configure the Event Function We will now begin coding the WorkDrive Sync app by configuring the Event function. > **Note:** We will need to code and deploy the application code to the Catalyst remote console before configuring the event listener in the Catalyst console, because we have to associate it with the Event function. The [function directory](https://docs.catalyst.zoho.com/en/cli/v1/project-directory-structure/functions-directory) `functions/workdrivesync` contains: * The `index.js` main function file * The `catalyst-config.json` configuration file * Node modules * `[package.json](https://docs.npmjs.com/files/package.json)` and `[package-lock.json](https://docs.npmjs.com/configuring-npm/package-lock-json.html)` dependency files You can use any IDE to configure the function. ### Install Packages for Node.js The Node.js Event function requires three packages to be installed: [`axios`](https://www.npmjs.com/package/axios), [`form-data`](https://www.npmjs.com/package/form-data), and [`fs`](https://www.npmjs.com/package/fs). #### `axios` `axios` is a promise-based HTTP client that we will use to send asynchronous HTTP requests to the WorkDrive API endpoint to post files. To install `axios`, navigate to the Node function's directory (`functions/workdrivesync`) and execute the following command: ```sh npm install axios ``` This will install the module. ![catalyst_workdrive_npm_axios](https://catalyst-docs-development.zohostratus.in/images/tutorials/workdrivesync/catalyst_workdrive_npm_axios.webp) #### `form-data` We will use `form-data` to upload the file to WorkDrive, after reading it from the event data sent in by the event listener. To install `form-data`, navigate to the Node function's directory (`functions/workdrivesync`) and execute the following command: ```sh npm install form-data ``` This will install the module. ![catalyst_workdrive_npm_formdata](https://catalyst-docs-development.zohostratus.in/images/tutorials/workdrivesync/catalyst_workdrive_npm_formdata.webp) #### `fs` The `fs` module enables us to access the physical file system and create a read stream to fetch the file from the event data. To install `fs`, navigate to the Node function's directory (`functions/workdrivesync`) and execute the following command: ```sh npm install fs ``` This will install the module. ![catalyst_workdrive_npm_fs](https://catalyst-docs-development.zohostratus.in/images/tutorials/workdrivesync/catalyst_workdrive_npm_fs.webp) #### `qs` The `qs` module helps with parsing and stringify URL query strings when working with APIs or data that need to be sent or received through query parameters. To install `qs`, navigate to the Node function's directory (`functions/workdrivesync`) and execute the following command: ```sh npm install qs ``` Information about these packages will also be updated in the [`package.json`](https://docs.npmjs.com/files/package.json) file of the Event function. ```json { "name": "workdrivesync", "version": "1.0.0", "main": "index.js", "author": "emma@zylker.com", "dependencies": { "axios": "^1.13.1", "form-data": "^4.0.4", "fs": "^0.0.1-security", "qs": "^6.14.0", "zcatalyst-sdk-node": "latest" } } ``` You can now add the code in the function file. Copy the code below and paste it in `index.js` located in `functions/workdrivesync` directory and save the file. > **Note:** Please go through the code in this section to make sure you fully understand it. We will discuss the function and client code, after you configure the client. **index.js** ```javascript const catalyst = require('zcatalyst-sdk-node'); const axios = require('axios').default; const FormData = require('form-data'); const fs = require('fs'); const credentials = { WorkDriveConnector: { client_id: '{{YOUR_CLIENT_ID}}', //Enter your Client ID client_secret: '{{YOUR_CLIENT_SECRET}}', //Enter your Client Secret auth_url: 'https://accounts.zoho.com/oauth/v2/token', refresh_url: 'https://accounts.zoho.com/oauth/v2/token', refresh_token: '{{YOUR_REFRESH_TOKEN}}' //Enter your Refresh Token } } const FOLDERID = '5m0kq28f2efdbc2464a37866cec7d6580cb47'; //Enter your WorkDrive Folder ID module.exports = async (event, context) => { try { const eventData = event.getRawData().events[0]; const eventType = eventData.event_config.api_name; const fileName = eventData.data.object_details[0].key; const app = catalyst.initialize(context); const query = `SELECT ROWID, WorkDriveFileID FROM FileVault where FileName='${fileName}'`; const queryResult = await app.zcql().executeZCQLQuery(query); console.log(queryResult); const ROWID = queryResult[0].FileVault.ROWID; const accessToken = await app.connection(credentials).getConnector('WorkDriveConnector').getAccessToken(); if (eventType === "stratus_object_uploaded") { const stratus = app.stratus(); const bucket = stratus.bucket("file-vault-demo"); // Replace your bucket name let fileStream = await bucket.getObject(fileName); const chunks = []; for await (const chunk of fileStream) { chunks.push(chunk); } const buffer = Buffer.concat(chunks); fs.writeFileSync(__dirname + '/' + fileName, buffer); var data = new FormData(); data.append('content', fs.createReadStream(__dirname + '/' + fileName)); const config = { method: 'POST', url: `https://workdrive.zoho.com/api/v1/upload?filename=${fileName}&override-name-exist=true&parent_id=${FOLDERID}`, headers: { 'Authorization': `Zoho-oauthtoken ${accessToken}`, ...data.getHeaders() }, data: data }; const response = await axios(config); console.log(response); const WorkDriveSync = 'Uploaded'; const body = response.data; const WorkDriveFileID = body.data[0].attributes.resource_id; const catalystTable = app.datastore().table('FileVault'); await catalystTable.updateRow({ WorkDriveFileID, WorkDriveSync, ROWID }); } else if (eventType === "stratus_object_deleted") { const WorkDriveFileID = queryResult[0].FileVault.WorkDriveFileID; const config = { method: "PATCH", url: `https://workdrive.zoho.com/api/v1/files/${WorkDriveFileID}`, headers: { Authorization: `Zoho-oauthtoken ${accessToken}`, Accept: "application/vnd.api+json", }, data: JSON.stringify({ data: { attributes: { status: "51", }, type: "files", }, }), }; console.log(config); await axios(config); const table = app.datastore().table("FileVault"); await table.deleteRow(ROWID); } context.closeWithSuccess(); } catch (err) { console.log(err); context.closeWithFailure(); } }; ``` > **Note:** After you copy and paste this code in your function file, ensure that you provide the following values in it as indicated by the comments: * **[Client ID](https://docs.catalyst.zoho.com/en/tutorials/workdrivesync/nodejs/register-client)** * **[Client Secret](https://docs.catalyst.zoho.com/en/tutorials/workdrivesync/nodejs/register-client)** * The Zoho service links used in this code (for example, accounts.zoho.com, workdrive.zoho.com, etc.) are Data Center (DC) specific. Replace .com with your corresponding Zoho data center domain. Ensure that all URLs in the code use the same DC domain to avoid authentication or API errors. * **[Refresh Token](https://docs.catalyst.zoho.com/en/tutorials/workdrivesync/nodejs/generate-refresh-token)** * [WorkDrive Folder ID](https://docs.catalyst.zoho.com/en/tutorials/workdrivesync/nodejs/create-workdrive-folder): You can obtain this value by opening the [folder you created in WorkDrive earlier](https://docs.catalyst.zoho.com/en/tutorials/workdrivesync/nodejs/create-workdrive-folder). The URL contains the Folder ID of the WorkDrive folder. Copy the ID displayed after `folders/` from the URL. ![catalyst_workdrive_folderid](https://catalyst-docs-development.zohostratus.in/images/tutorials/workdrivesync/workdrive-folder-id-through-link.webp) The Event function is now configured.