ts/docs/advanced/auto-upload-download.md
Composio SDK includes an automatic file handling system that manages file uploads and downloads when executing tools. This guide explains how the system works, how to configure it, and how to handle files manually when auto-handling is disabled.
The file handling system in Composio SDK is:
When a tool's input parameter is marked with file_uploadable: true, the SDK will:
Example tool schema with file upload:
const toolSchema = {
inputParameters: {
type: 'object',
properties: {
file: {
type: 'string',
file_uploadable: true
}
}
}
}
When executing a tool with file upload:
// With auto upload enabled (default)
const result = await composio.tools.execute('your-tool', {
arguments: {
file: '/path/to/local/file.txt' // Local file path
// or
file: 'https://example.com/file.txt' // Remote URL
}
});
// The SDK automatically:
// 1. Reads the file content
// 2. Uploads it to S3
// 3. Replaces the path with metadata:
// {
// name: string; // The original filename
// mimetype: string; // The file's mime type
// s3key: string; // The S3 key for the uploaded file
// }
When a tool's output contains an S3 URL and mimetype, the SDK will:
Example tool response with file download:
// Original response from tool
{
data: {
file: {
s3url: 'https://s3.example.com/file.txt',
mimetype: 'text/plain'
}
}
}
// After auto-download
{
data: {
file: {
uri: '/path/to/local/file.txt',
file_downloaded: true,
s3url: 'https://s3.example.com/file.txt',
mimeType: 'text/plain'
}
}
}
You can disable automatic file handling when initializing the SDK:
import { Composio } from '@composio/core';
const composio = new Composio({
apiKey: 'your-api-key',
autoUploadDownloadFiles: false
});
When auto file handling is disabled, you'll need to handle the file operations yourself using the composio.files API:
When auto-upload is disabled, you need to handle file uploads manually:
// Upload file manually
const fileData = await composio.files.upload({
filePath: '/path/to/file.txt', // Local path or URL
toolSlug: 'your-tool', // Tool slug
toolkitSlug: 'your-toolkit' // Toolkit slug
});
// Execute tool with file data
const result = await composio.tools.execute('your-tool', {
arguments: {
file: fileData // Contains name, mimetype, and s3key
}
});
When auto-download is disabled, you'll need to handle file downloads manually:
// Execute tool
const result = await composio.tools.execute('your-tool', {
arguments: {
// your arguments
}
});
// Download file manually if response contains S3 URL
if (result.data.file?.s3url) {
const downloadedFile = await composio.files.download({
s3Url: result.data.file.s3url,
toolSlug: 'your-tool',
mimeType: result.data.file.mimetype || 'application/txt'
});
// downloadedFile contains:
// {
// name: string; // Generated filename (toolSlug_timestamp.ext)
// mimeType: string; // The file's mime type
// s3Url: string; // The original S3 URL
// filePath: string; // Local path to the downloaded file
// }
}
Downloaded files are stored in a temporary directory:
~/.composio/files/ (user's home directory){toolSlug}_{timestamp}.{extension}The SDK includes specific error types for file operations:
import { ComposioFileUploadError } from '@composio/core';
try {
await composio.tools.execute('your-tool', {
arguments: {
file: '/path/to/file.txt'
}
});
} catch (error) {
if (error instanceof ComposioFileUploadError) {
console.error('File upload failed:', error.message);
// Handle file upload error
// Possible fixes will be included in error.possibleFixes
}
throw error;
}