---
schema_version: "1"
id: bef22d158b8bf49328ed0474bafbe9c34fee182cb9ba4f053ceea06d87f62e83
title: Box Connector 5.3 Examples
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/box-connector/latest/box-connector-examples.md
canonical_url: https://docs.mulesoft.com/box-connector/latest/box-connector-examples
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 00b6b3468b85902188a98016d0189bbee7567d8156058ee598162a7e2160ebb4
description: MuleSoft Documentation Site
release: "5.3"
latest: true
---
---
product: Box Connector
version: 5.3
is-latest-version: true
---

# Box Connector 5.3 Examples

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

Try out the following examples using Studio or XML.

## Anypoint Studio Example: Folders

This example shows how to create a folder, retrieve a folder, and delete a folder.

1.  Create a new Mule project in Anypoint Studio.

2.  Add the following properties to the `mule-artifact.properties` file and place it in the
    project’s `src/main/resources` directory.

    ```text
    config.clientId=<Client ID>
    config.clientSecret=<Client Secret>
    ```

3.  Configure the HTTP Listener by adding a new HTTP global element. Click the **+** next to the
    Connector configuration field.

4.  Configure the global element according to the following table:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Name</p></td><td><p>HTTP_Listener_config</p></td></tr><tr><td><p>Protocol</p></td><td><p>HTTP</p></td></tr><tr><td><p>Host</p></td><td><p>localhost</p></td></tr><tr><td><p>Port</p></td><td><p>8081</p></td></tr></tbody></table>

5.  Drag an HTTP Listener onto the canvas and configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>HTTP Listener</p></td></tr><tr><td><p>Connector configuration</p></td><td><p>Select the newly created HTTP Listener Configuration.</p></td></tr><tr><td><p>Path</p></td><td><p><code>/boxdemo</code></p></td></tr></tbody></table>

6.  Set the folder properties for the folder you are going to create using a DataWeave component.
    Drag the **DataWeave** component next to the HTTP Listener and use the following script.

    ```dataweave
    %dw 2.0
    output application/json
    ---
    {
    	parent: {
    		id: 0
    	},
    	"type": "folder",
    	name: "SampleFolder"
    }
    ```

7.  Drag the **Create folder** operation of the Box Connector next to the DataWeave component to
    create a folder.

8.  Configure Box Connector by adding a new Box global element. Click the **+** next to the
    Connector configuration field.

9.  Configure the global element according to the following table.

    <table><colgroup><col> <col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th><th>Comments</th></tr></thead><tbody><tr><td><p>clientId</p></td><td><p><code>${config.clientId}</code></p></td><td><p>The client identifier as assigned by the authorization server when the client application was registered.</p></td></tr><tr><td><p>clientSecret</p></td><td><p><code>${config.clientSecret}</code></p></td><td><p>The client application’s client secret.</p></td></tr><tr><td><p>host</p></td><td><p><code>api.box.com</code></p></td><td><p>The default value. Leave as is.</p></td></tr><tr><td><p>port</p></td><td><p><code>443</code></p></td><td><p>The default value. Don’t change.</p></td></tr><tr><td><p>basePath</p></td><td><p><code>/2.0</code></p></td><td><p>The default value. Don’t change.</p></td></tr><tr><td><p>protocol</p></td><td><p><code>HTTPS</code></p></td><td><p>The default value. Don’t change.</p></td></tr><tr><td><p>localCallbackPath</p></td><td><p><code>/callback</code></p></td><td><p>The default value. Don’t change.</p></td></tr><tr><td><p>localCallbackConfig</p></td><td><p><code>HTTP_Listener_config</code></p></td><td><p>Select the HTTP Listener configuration created earlier.</p></td></tr><tr><td><p>externalCallbackUrl</p></td><td><p><code>http://localhost:8081/callback</code></p></td><td><p>Use the redirect URL that is configured in the client application.</p></td></tr><tr><td><p>localAuthorizationUrl</p></td><td><p><code>http://localhost:8081/authorize</code></p></td><td><p>Enter this URL after deploying the Mule application to initiate <code>OAUTH2</code> dance.</p></td></tr><tr><td><p>authorizationUrl</p></td><td><p><code>https://account.box.com/api/oauth2/authorize</code></p></td><td><p>The authorization URL to request for an authorization code.</p></td></tr><tr><td><p>accessTokenUrl</p></td><td><p><code>https://api.box.com/oauth2/token</code></p></td><td><p>The access token URL to request an access token.</p></td></tr></tbody></table>

    The corresponding XML configuration should be as follows:

    ```xml
    <mule-box-connector:config name="Mule_box_connector_Config"
     doc:name="Mule-box-connector Config"
     property_clientId="#{config.clientId}"
     property_clientSecret="#{config.clientSecret}"
     property_localCallbackConfig="HTTP_Listener_config"
     property_externalCallbackUrl="http://localhost:8081/callback" />
    ```

10. In the properties editor of Box Connector, configure the parameters required for the **Create
    folder** operation:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Create folder</p></td></tr><tr><td colspan="2"><p>Basic Settings</p></td></tr><tr><td><p>Connector configuration</p></td><td><p>Select the global Box Connector element that you created.</p></td></tr><tr><td colspan="2"><p>General</p></td></tr><tr><td><p>Create folder request data</p></td><td><p><code>#[payload]</code></p></td></tr></tbody></table>

11. Drag a **Logger** component to log the newly created folder ID and configure the following
    properties:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Specifies the logger to use to log the created folder ID.</p></td></tr><tr><td colspan="2"><p>Generic</p></td></tr><tr><td><p>Message</p></td><td><p>Specifies the folder created with ID : <code>#[payload.id]</code></p></td></tr></tbody></table>

12. Drag the **Get folder** operation of Box Connector next to the **Logger** component. This
    retrieves the folder information and configures the following properties:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Get folder</p></td></tr><tr><td colspan="2"><p>Basic Settings</p></td></tr><tr><td><p>Connector configuration</p></td><td><p>Select the global Box Connector element that you created.</p></td></tr><tr><td colspan="2"><p>General</p></td></tr><tr><td><p>Folder id</p></td><td><p><code>#[payload.id]</code></p></td></tr></tbody></table>

13. Drag a **Logger** component to log the retrieved folder information and configure the following
    properties:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Specifies the logger that logs the retrieved folder information.</p></td></tr><tr><td colspan="2"><p>Generic</p></td></tr><tr><td><p>Message</p></td><td><p><code>#[payload]</code></p></td></tr></tbody></table>

14. Drag the Box Connector **Delete folder** operation to delete the folder that was just created
    and configure the following properties:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Delete folder</p></td></tr><tr><td colspan="2"><p>Basic Settings</p></td></tr><tr><td><p>Connector configuration</p></td><td><p>Select the global Box Connector element that you created.</p></td></tr><tr><td colspan="2"><p>General</p></td></tr><tr><td><p>Folder id</p></td><td><p><code>#[payload.id]</code></p></td></tr></tbody></table>

15. Drag the **DataWeave** component to set the payload to display the result of the flow to the
    user, and use the following script:

    ```dataweave
    %dw 2.0
    output application/json
    ---
    {
    	result : "Folder was created, retrieved and deleted successfully."
    }
    ```

16. Save and run the project as a Mule app.

17. Use the URL `http://localhost:8081/authorize` from a browser that you set for
    `localAuthorizationUrl` in the global Box configuration element to initiate the `OAUTH2` dance.

    This displays the page asking the Box user to either grant or deny access to the client
    application (which reads and writes all of files and folders stored in Box).

18. Click **Grant access to Box** to grant the read and write permissions.

19. Open a web browser and access `http://localhost:8081/boxdemo`.

    You should get a JSON response with the following content:

    Result: `"Folder created, retrieved and deleted successfully."`

## XML Example: Demonstrate Create Folder, Retrieve Folder, and Delete Folder Operations

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

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:mule-box-connector="http://www.mulesoft.org/schema/mule/mule-box-connector"
	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/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/mule-box-connector
http://www.mulesoft.org/schema/mule/mule-box-connector/current/mule-mule-box-connector.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

	<http:listener-config name="HTTP_Listener_config"
		doc:name="HTTP Listener config">
		<http:listener-connection host="localhost"
			port="8081" />
	</http:listener-config>

	<mule-box-connector:config name="Mule_box_connector_Config"
		doc:name="Mule-box-connector Config"
		property_clientId="#{config.clientId}"
		property_clientSecret="#{config.clientSecret}"
		property_localCallbackConfig="HTTP_Listener_config"
		property_externalCallbackUrl="http://localhost:8081/callback" />

	<flow name="Create-Get-Delete-Folder-Flow">

		<http:listener doc:name="HTTP Listener"
			path="/boxdemo"
			config-ref="HTTP_Listener_config"
		/>

		<ee:transform doc:name="DataWeave to set folder properties">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
	parent: {
		id: 0
	},
	"type": "folder",
	name: "SampleFolder"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<box-connector:create-folder
			doc:name="Create folder" config-ref="Mule_box_connector_Config" />

		<logger level="INFO" doc:name="Logger to log the created Folder ID"
			message="Folder created with ID : #[payload.id]" />

		<box-connector:get-folder doc:name="Get folder"
			config-ref="Mule_box_connector_Config" folder-id="#[payload.id]" />

		<logger level="INFO" doc:name="Logger to log the retrieved folder info"
			message="#[payload]" />

		<box-connector:delete-folder
			doc:name="Delete folder" config-ref="Mule_box_connector_Config"
			folder-id="#[payload.id]" />

		<ee:transform doc:name="DataWeave to show the result">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
	result : "Folder got created, retrieved and deleted successfully"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
	</flow>
</mule>
```

## Example: Upload a File

This example demonstrates how to upload files to use with other Box operations you have created.

1.  Create a new Mule project in Anypoint Studio.

2.  Create a folder called `input` in `src/main/resources directory`.

3.  Copy the file you want to upload to the `input directory`.

4.  Configure the HTTP Listener by adding a new HTTP global element.

5.  Click the **Global Elements** tab at the bottom of the canvas.

6.  In the **Global Configuration Elements** screen, click **Create**.

7.  In the **Choose Global Type** wizard, expand the Connector Configuration.

8.  Select **HTTP Listener** and click **OK**.

9.  In the **HTTP Listener** screen configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Name</p></td><td><p>HTTP_Listener_config</p></td></tr><tr><td><p>Protocol</p></td><td><p>HTTP</p></td></tr><tr><td><p>Host</p></td><td><p>localhost</p></td></tr><tr><td><p>Port</p></td><td><p>8081</p></td></tr></tbody></table>

10. Drag an HTTP Listener onto the canvas and configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>HTTP Listener</p></td></tr><tr><td><p>Connector configuration</p></td><td><p>Select the HTTP Listener Configuration that has been previously created.</p></td></tr><tr><td><p>Path</p></td><td><p><code>/boxUploadFile</code></p></td></tr></tbody></table>

11. Login to Box and generate the Box developer token, which is active for 60 minutes.

12. Set this Developer Token using a DataWeave component.

13. Drag the DataWeave component next to the HTTP Listener and configure as follows.

    ```dataweave
    %dw 2.0
    output application/java
    		//Set the generated developer token here
    var developerToken = "{developer Token}"
    ---
    "Bearer " ++ developerToken
    ```

14. Drag a **Set Variable** component onto the canvas and configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Save developer token</p></td></tr><tr><td><p>Name</p></td><td><p>developerToken</p></td></tr><tr><td><p>Value</p></td><td><p><code>#[payload]</code></p></td></tr></tbody></table>

15. Configure the File connector by adding a new **File Global Element**.

16. Click the **Global Elements** tab at the bottom of the canvas.

17. From the **Global Configuration Elements** screen, click **Create**.

18. From the **Choose Global Type** wizard, expand Connector Configuration.

19. Select **File Config** and click **OK**.

20. In the File Config screen configure the following parameter:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Working Directory</p></td><td><p><code>${mule.home}/apps/${app.name}/</code></p></td></tr></tbody></table>

21. Drag a **Read File** operation onto the canvas and configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Read File</p></td></tr><tr><td><p>Connector configuration</p></td><td><p>Select the File Configuration that has been previously created in global elements.</p></td></tr><tr><td><p>File Path</p></td><td><p><code>input/yourFileName</code></p></td></tr></tbody></table>

22. Transform this payload to a `multipart/form-data` type of request.

23. Specify a folder to upload by setting the ID of the parent folder.

24. Drag a DataWeave component next to the **Read File** component and use the following script:

    ```dataweave
    %dw 2.0
    output multipart/form-data
    ---
    {
      parts : {
      	attributes : {
      			headers : {
            		"Content-Type": "application/json"},
          		content : {
          			"name": message.attributes.fileName ,
    	// Set the ID of the parent folder. Use "0" for the root folder.
          	 		"parent": {
          	 		  "id":"0"
    				}
    			}
    	},
        file : {
          headers : {
            "Content-Disposition" : {
                "name": "file",
                "filename": message.attributes.fileName
            }
          },
          content : payload
        }
      }
    }
    ```

25. Configure the HTTP Request by adding a new HTTP Request global element.

26. Click the **Global Elements** tab at the bottom of the canvas.

27. In the **Global Configuration Elements** screen, click **Create**.

28. In the **Choose Global Type** wizard expand Connector Configuration, select **HTTP Request
    Configuration** and click **OK**.

29. In the HTTP Request Configuration screen configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Name</p></td><td><p>HTTP_Request_configuration</p></td></tr><tr><td><p>Base path</p></td><td><p>/</p></td></tr><tr><td><p>Protocol</p></td><td><p>HTTP(Default)</p></td></tr><tr><td><p>Host</p></td><td><p>80</p></td></tr><tr><td><p>Port</p></td><td></td></tr><tr><td><p>Use persistent connections</p></td><td><p>✓</p></td></tr><tr><td><p>Max Connections</p></td><td><p>-1</p></td></tr><tr><td><p>Connection idle timeout</p></td><td><p>30000</p></td></tr><tr><td><p>Response buffer size</p></td><td><p>1024</p></td></tr><tr><td><p>TLS configuration</p></td><td><p>None</p></td></tr><tr><td><p>Proxy</p></td><td><p>None</p></td></tr><tr><td><p>Authentication</p></td><td><p>None</p></td></tr><tr><td><p>Client socket properties</p></td><td><p>None</p></td></tr><tr><td><p>Use reconnection</p></td><td><p>unchecked</p></td></tr></tbody></table>

30. Drag an **HTTP Request** onto the canvas and configure the following parameters:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbody><tr><td><p>Display Name</p></td><td><p>Request - Box Upload File</p></td></tr><tr><td><p>Configuration</p></td><td><p>Select the HTTP request configuration that you created in global elements.</p></td></tr><tr><td><p>Method</p></td><td><p>POST</p></td></tr><tr><td><p>Path</p></td><td></td></tr><tr><td><p>URL</p></td><td><p><code><a href="https://upload.box.com/api/2.0/files/content">https://upload.box.com/api/2.0/files/content</a></code></p></td></tr><tr><td><p>Body</p></td><td><p>payload</p></td></tr></tbody></table>

31. Create an Authorization header in the HTTP Request.

32. Click the **Headers** tab next to the Body in the HTTP request and click **+** to configure the
    following parameter:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Value</th></tr></thead><tbody><tr><td><p>"Authorization"</p></td><td><p><code>vars.developerToken</code></p></td></tr></tbody></table>

33. Drag the DataWeave component to set the payload to display the result of the flow to the user
    and configure as follows:

    ```dataweave
    %dw 2.0
    output application/json
    ---
    {
    	result : "File has been successfully created"
    }
    ```

34. Save and run the project as a Mule application.

35. Enter the URL `[http://localhost:8081/boxUploadFile](http://localhost:8081/boxUploadFile)` in
    the browser. You should get a JSON response with the following content:

    `result : "File has been successfully created"`

## XML Example: Upload File

Here is the XML example to upload a file.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mule-box-connector="http://www.mulesoft.org/schema/mule/mule-box-connector" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:file="http://www.mulesoft.org/schema/mule/file"
	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/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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.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/mule-box-connector http://www.mulesoft.org/schema/mule/mule-box-connector/current/mule-mule-box-connector.xsd">

	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>

	<file:config name="File_Config" doc:name="File Config">
		<file:connection workingDir="${mule.home}/apps/${app.name}/" />
	</file:config>

	<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" >
		<http:request-connection host="80" />
	</http:request-config>

	<flow name="UploadFileToBox" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/boxUploadFile" />
		<ee:transform doc:name="Set Developer Token Variable">
			<ee:message>
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
		//Set the generated developer token here
var developerToken = "{developer Token}"
---
"Bearer " ++ developerToken]]></ee:set-payload>
			</ee:message>
			<ee:variables>
			</ee:variables>
		</ee:transform>
		<set-variable value="#[payload]" doc:name="Save developer token" variableName="developerToken"/>
		<file:read doc:name="Read File" config-ref="File_Config" path="input/yourFileName"/>
		<ee:transform doc:name="Transform to multipart/form-data">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output multipart/form-data
---
{
  parts : {
  	attributes : {
  			headers : {
        		"Content-Type": "application/json"},
      		content : {
      			"name": message.attributes.fileName ,
	// Set the ID of the parent folder. Use "0" for the root folder.
      	 		"parent": {
      	 		  "id":"0"
				}
			}
	},
    file : {
      headers : {
        "Content-Disposition" : {
            "name": "file",
            "filename": message.attributes.fileName
        }
      },
      content : payload
    }
  }
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<http:request method="POST" doc:name="Request - Box Upload File" url="https://upload.box.com/api/2.0/files/content" config-ref="HTTP_Request_configuration">
			<http:headers ><![CDATA[#[output application/java
---
{
	"Authorization" : vars.developerToken,
	"Key" : "Value"
}]]]></http:headers>
		</http:request>
		<ee:transform doc:name="Transform Response">
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	result : "File has been successfully created"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
	</flow>
</mule>
```

## See Also

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