---
schema_version: "1"
id: 4bcf14d8f3ce960280eacd41eaef0080a8640ef24d82859441594e4d38fa3ef9
title: Connect to FTP Servers Dynamically Example
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/ftp-connector/latest/ftp-dynamic-connection.md
canonical_url: https://docs.mulesoft.com/ftp-connector/latest/ftp-dynamic-connection
source_site: docs.mulesoft.com
content_type: documentation
content_hash: e9cfb19de636270ee102ee2338d3840cd71f811ccf0429bfba4e5edc04aa8387
description: MuleSoft Documentation Site
release: "2.0"
latest: true
---
---
product: FTP Connector
version: 2.0
is-latest-version: true
---

# Connect to FTP Servers Dynamically Example

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Many integrations require connecting to different servers based on a specific condition, such as:

- Connecting to different invoice storage servers, depending on the branch that sends an invoice.

- Connecting to different servers, depending on an integration subject, such as in a multi-tenant
  use case.

To accommodate these use cases, the `config` element supports parameter expressions that evaluate
these conditions and connect to the correct server.

The following example shows a dynamic multicast application that:

- Defines a configuration for the connector in which the `host`, `username`, and `password` are
  expressions.

- Describes a flow, triggered by an **HTTP Listener**, in which content is posted through HTTP.

- Uses the File Connector **Read** operation to load a `recipients.csv` file that contains a set of
  FTP destinations, with columns such as `host`, `user`, and `port`.

- Uses a **For Each** component and an FTP **Write** operation to write the contents into each of
  the FTP destinations. On each **For Each** iteration, the expressions set in the FTP global
  configuration element resolve to a different value and establish different connections to each of
  the servers.

To test this example, create the Mule application and run it using curl commands:

![FTP Servers Dynamic Connection Flow in Studio Canvas](/mulesoft/current/en-us/ftp-connector/latest/_images/ftp_dynamic_connect_flow.png)

## Create the Mule Application

To create the Mule application flow:

1.  In the **Mule Palette** view, select **HTTP > Listener**.

2.  Drag **HTTP Listener** to the Studio canvas.

3.  On the **HTTP Listener** configuration screen, optionally change the value of the **Display
    Name** field.

4.  Set **Path** to `/multitenant`.

5.  Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of HTTP Listener in the app.

6.  On the **General** tab, set the following fields:

    - **Host**: `All Interfaces [0.0.0.0] (default)`

    - **Port**: `8081`

7.  Click **OK**.

8.  Drag a **Set Variable** component to the right of the **HTTP Listener** source.

9.  Set **Name** to `content` and **Value** to `#[payload]`.

10. Drag the File Connector **Read** operation to the right of the **Set Variable** component.

11. Set **File Path** to `recipients.csv`. This reads a CSV file that contains a random set of FTP
    destinations with columns such as `host`, `user`, and `port`.

12. Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of File Connector in the app.

13. Accept the default settings and click **OK**.

14. On the **MIME Type** tab of the **Read** operation, for **MIME Type**, select
    **application/csv**.

15. In Studio, drag a **Transform Message** component to the right of the **Read** operation.

16. In the **Output** section of the component, add the following DataWeave code to map the columns
    of the CSV file:

    DataWeave script:

    ```dataweave
    %dw 2.0
    output application/java
    ---
    payload map using (row = $) {
       host: row.Host,
       user: row.User,
       password: row.Password}
    ```

17. Drag a **For Each** component to the right of the **Transform Message** component.

18. Drag an FTP **Write** operation into the **For Each** component.  
    On each For Each iteration, the **Write** operation writes the contents into each of the FTP
    destinations.

19. Click the plus sign (**+**) next to the **Connector configuration** field to configure a global
    element that can be used by all instances of FTP **Write** operation in the app, and set the
    following fields:

    - **Host**: `#[payload.host]`

    - **Username**: `#[payload.username]`

    - **Password**: `#[payload.password]`

20. Click **OK**.

21. Set **Path** to `demo.txt`.

22. Set the **Content** field to `payload`.

23. Drag a **Set Payload** component to the right of the **For Each** component.

24. Set the **Value** field to `Multicast Ok`.

## Run and Test Your Mule Application

After creating your Mule application, run and test it:

1.  In Studio, save your Mule app.

2.  Click the project name in **Package Explorer** and then click **Run** > **Run As** > **Mule
    Application**.

3.  Open a browser and type `http://0.0.0.0:8081/multitenant`.

    - Although the information posted through the HTTP Listener source is written to each FTP site
      multiple times, the repeatable streams feature prevents the overhead from consuming the stream
      multiple times.

    - The For Each component automatically goes through each line of the CSV file without needing to
      first transform it into a Java structure.

      When reading or listing files, you might be interested in the file’s metadata (for example,
      the file name, full path, size, timestamp). The connector uses the Mule message attributes to
      access this information.

## XML for Connecting to FTP Servers Dynamically

Paste this code into the **Configuration XML** tab in your Studio canvas to quickly load the flow
for this example into your Mule app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:sftp="http://www.mulesoft.org/schema/mule/sftp"
	xmlns:file="http://www.mulesoft.org/schema/mule/file"
	xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
	xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<file:config name="File_Config" doc:name="File Config" doc:id="" />
	<ftp:config name="FTP_Config" doc:name="FTP Config" doc:id="" >
		<ftp:connection host="#[payload.host]" username="#[payload.username]" password="#[payload.password]" />
	</ftp:config>
	<flow name="streaming-multitenantFlow" >
<http:listener path="/multitenant"
doc:name="Listener" config-ref="HTTP_Listener_config"/>
<set-variable variableName="content" value="#[payload]" doc:name="Variable" />
<file:read path="recipients.csv" doc:name="Read"
 outputMimeType="application/csv" config-ref="File_Config"/>
  <ee:transform doc:name="Transform Message" doc:id="" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload map using (row = $) {
   host: row.Host,
   user: row.User,
   password: row.Password
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<foreach doc:name="For Each" >
			<ftp:write doc:name="Write" doc:id="" path="recipients.csv" config-ref="FTP_Config"/>
</foreach>
<set-payload doc:name="Set Payload" value="Multicast OK"/>
</flow>
</mule>
```

## See Also

- [FTP Connector Examples](https://docs.mulesoft.com/ftp-connector/latest/ftp-examples)

- [Introduction to Anypoint Connectors](https://docs.mulesoft.com/connectors/introduction/introduction-to-anypoint-connectors)

- [Salesforce Help](https://help.salesforce.com)
