Create and Write CSV Files Using File Connector in MuleSoft

Getting Started

First, open Anypoint Studio and Create a New Mule Project.

New Mule Project

Enter a project name and selection location and click finish.

Project Settings

Click on Add Modules in the Mule palette and find File in the featured components.

Add Modules

Drag and drop File onto the Mule palette before adding it to the project.

It should be like this after drag and drop.

Mule Palette

Now let's add a new Mule Configuration File and give the name as global.

Mule Configuration File

Enter the file name as global.

Give file name

Go to Global Elements, click the Create button, and add an HTTP Listener config.

Choose Type

Leave everything as default and click ok.

HTTP Listener Config

Also, add a new File Config and click OK.

Add new file config

Select File Config and click OK.

Next, provide the Working Directory path in the General tab.

File config

Next, drag and drop HTTP >> Listener from Mule palette to Message flow and give Display Name, select Connector configuration, and enter the Path.

File Connector

Also, in Advanced properties, enter POST in Allowed methods.

Advanced Property

Next, drag and drop the logger component from the Core palette to log the payload data.


Now let's create a JSON object which we will use in this example.

Json Code

Save it as a JSON file on a desktop or any location.

We will use this file to create metadata type for all payload inputs.

Go back to HTTP listener and select Metadata in properties and click on the button Add metadata and click on the little icon to add new metadata.


Next, click on Add button and provide the type name "fileExample" and click Create type button.

Create type button

Next, select metadata type, Select type ad JSON and select Example from the drop-down, and browse JSON file from the desktop and click Select.

User defined

So, this step will allow us to auto-complete our DataWeave expressions.

Next, go to Logger properties and click on Show graphical properties icon.

Go to Logger

Click on Define metadata.


Select JSON object.

Metadata type

To test the object data, click on the Preview button.


Next, drag and drop the Set Variable component from the Core Mule palette.

Set Variable

Go to Metadata properties and select Json object.

Json Object

Next, provide the Name of the variable in Settings and Value expression.

Set Variable

Set the DataWeave expression.


You can check the preview of data as well.

Finally, drag and drop the file write connector from the File module.

Set Variable

Next, enter the Path and dynamically define the name for the upcoming file by concatenating the variable to the file's name.

Write to file

Next, set the content of the file.

Click on content, select Define metadata first, then define data weave expression.


After setting app properties in Connector configuration, Path, and Content, it will look like this.

Mule image

Let's run the application.

Run the application

Open Postman and enter Endpoint to see the output result.


Also, enter the Headers values and Body text.


Hit Send button.


As you see, the output is 200, which means api is running without any issues. Now go back to Endpoint Studio to check the log data.


Logger also logging the data correctly.

Now the final step is to test if the file has been created and data is written in the giver directory.


As you can see, a file has been created, and customer JSON data is also posted.


In this article, we learned to create and write CSV files using File Connector in MuleSoft and display results in Postman.

Similar Articles