Skip to main content
Version: 5.1

Build a File-Driven Integration

TimeUnder 10 minutes
What you'll buildA file integration that adds an onModify handler to track file changes and uses printInfo to log file modification events.

File integrations are ideal for batch uploads, scheduled file processing, and ETL workflows triggered by files appearing in a folder or FTP server.

Prerequisites​

A working WSO2 Integrator environment.

Local setup

Install and launch WSO2 Integrator on your machine.

WSO2 Cloud Account

For AI assisted development and WSO2 Cloud deployments.

Also required, a file at the listener path to watch. Create one if you don't have one:

echo "test" > /tmp/testfile.txt

Step 1: Create the integration​

Note

If you're using the cloud editor, a project is already open, so you can skip this step and go directly to Step 2: Add a file integration artifact.

  1. Open WSO2 Integrator.

  2. Click Create in the Create a Project card.

    WSO2 Integrator home screen with the Create a Project cardWSO2 Integrator home screen with the Create a Project card
  3. Set Project Name to file-integration.

  4. Set Integration Name to FileTracker.

  5. Click Create.

    Create a Project form with Project name set to file-integration and Integration name set to FileTrackerCreate a Project form with Project name set to file-integration and Integration name set to FileTracker

Step 2: Add a file integration artifact​

  1. Select your integration from the project overview canvas.

  2. In the design view, click Add Artifact Manually.

    Integration design view with the Add Artifact manually button highlightedIntegration design view with the Add Artifact manually button highlighted
  3. Select Local Files under File Integration.

    Artifacts page with Local Files highlighted under File IntegrationArtifacts page with Local Files highlighted under File Integration
  4. Set Path to the folder you want to watch:

    /tmp
  5. Click Create.

    Create Local Files form with Path set to /tmpCreate Local Files form with Path set to /tmp

Step 3: Add onModify event handler​

  1. In the service designer view, click + Add Handler.

  2. Select onModify.

    Select Handler to Add panel with onModify highlightedSelect Handler to Add panel with onModify highlighted
  3. In the Configure onModify Handler panel, clear both On Success and On Error under File Handling Options, then click Save. Both options arrive ticked and set to Move, which moves each processed file out of the watched directory. Clearing them keeps testfile.txt in /tmp, where the later steps expect it. For details, see Post-processing: moving or deleting files.

Step 4: Add file tracking logic​

  1. Click + in the flow diagram.

  2. Search for printInfo and select printInfo.

    Node panel search results showing printInfo under logNode panel search results showing printInfo under log
  3. Set Msg to File modified.

  4. Click Save.

    log:printInfo panel with Msg set to File modifiedlog:printInfo panel with Msg set to File modified

Step 5: Run and test​

  1. Click Run in the toolbar.

  2. Run the modify command in your terminal to trigger the handler:

echo "modify" > /tmp/testfile.txt
  1. Confirm the run terminal shows the log line File modified.

    Flow designer showing the integration running with log:printInfo emitting File modifiedFlow designer showing the integration running with log:printInfo emitting File modified

Source code​

View the generated Ballerina code

The steps above generate the following complete, runnable Ballerina program:

Windows

Change the listener path from "/tmp" to "C:\\tmp" (backslash escaped) before running the program.

import ballerina/file;
import ballerina/log;

listener file:Listener fileListener = new (path = "/tmp", recursive = false);

service file:Service on fileListener {
remote function onModify(file:FileEvent event) returns error? {
do {
log:printInfo("File modified");
} on fail error err {
// handle error
return error("unhandled error", err);
}
}


}

What's next​

  • Local files — Full Local Files listener reference (events, recursive watching, file handlers)
  • FTP/SFTP — Watch and process files on remote FTP or SFTP servers
  • Streaming large files — Process large files without loading them fully into memory
  • CSV fault tolerance — Handle errors and partial failures when processing CSV files