export
Export n8n workflows to JSON files for backup, sharing, or deployment purposes. Specify individual workflow IDs or export all workflows at once with customizable output formatting.
Instructions
Export workflows from n8n - replaces "n8n export:workflow" command
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| all | No | Export all workflows | |
| id | No | Workflow ID to export | |
| outputPath | No | Output directory path (defaults to workflows/flows) | |
| pretty | No | Format JSON output prettily (default: true) |
Input Schema (JSON Schema)
{
"properties": {
"all": {
"description": "Export all workflows",
"type": "boolean"
},
"id": {
"description": "Workflow ID to export",
"type": "string"
},
"outputPath": {
"description": "Output directory path (defaults to workflows/flows)",
"type": "string"
},
"pretty": {
"description": "Format JSON output prettily (default: true)",
"type": "boolean"
}
},
"type": "object"
}
Implementation Reference
- src/tools/handler.ts:147-154 (handler)Handler function in ToolHandler class that dispatches 'export' tool calls to N8nManager.exportWorkflowcase 'export': return await this.n8nManager.exportWorkflow({ id: args?.id as string, all: args?.all as boolean, outputPath: args?.outputPath as string, pretty: args?.pretty as boolean, });
- src/tools/registry.ts:244-267 (schema)Input schema and definition for the 'export' MCP toolname: 'export', description: 'Export workflows from n8n - replaces "n8n export:workflow" command', inputSchema: { type: 'object', properties: { id: { type: 'string', description: 'Workflow ID to export', }, all: { type: 'boolean', description: 'Export all workflows', }, outputPath: { type: 'string', description: 'Output directory path (defaults to workflows/flows)', }, pretty: { type: 'boolean', description: 'Format JSON output prettily (default: true)', }, }, }, },
- src/server/mcflow.ts:76-78 (registration)Registration of all tools (including 'export') via ListToolsRequestHandler using getToolDefinitions()this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: getToolDefinitions(), }));
- src/n8n/manager.ts:543-647 (helper)Core implementation of exportWorkflow method in N8nManager that executes the actual export logic using n8n CLI commandsasync exportWorkflow(options: { id?: string; all?: boolean; outputPath?: string; pretty?: boolean; } = {}): Promise<any> { try { const outputDir = options.outputPath || path.join(this.workflowsPath, 'flows'); // Ensure output directory exists await fs.mkdir(outputDir, { recursive: true }); // Build command - n8n requires a file path, not directory let command = 'n8n export:workflow'; if (options.all) { // For all workflows, we need to export to a temp file and process const tempFile = path.join('/tmp', `n8n-export-${Date.now()}.json`); command += ` --all --output="${tempFile}"`; if (options.pretty !== false) { command += ' --pretty'; } console.error(`Executing: ${command}`); const { stdout, stderr } = await execAsync(command); if (this.hasRealError(stderr, stdout)) { throw new Error(stderr); } // Read the exported data const exportedData = await fs.readFile(tempFile, 'utf-8'); const workflows = JSON.parse(exportedData); // Save each workflow separately, preserving ID for (const workflow of workflows) { const fileName = `${workflow.name.toLowerCase().replace(/\s+/g, '-')}.json`; const filePath = path.join(outputDir, fileName); // Store the full workflow object with ID await fs.writeFile(filePath, JSON.stringify(workflow, null, 2)); } // Clean up temp file await fs.unlink(tempFile).catch(() => {}); return { content: [{ type: 'text', text: `β Exported ${workflows.length} workflows to ${outputDir}` }] }; } else if (options.id) { // For single workflow, export directly const tempFile = path.join('/tmp', `n8n-export-${Date.now()}.json`); command += ` --id=${options.id} --output="${tempFile}"`; if (options.pretty !== false) { command += ' --pretty'; } console.error(`Executing: ${command}`); const { stdout, stderr } = await execAsync(command); if (this.hasRealError(stderr, stdout)) { throw new Error(stderr); } // Read the exported data const exportedData = await fs.readFile(tempFile, 'utf-8'); const workflows = JSON.parse(exportedData); const workflow = Array.isArray(workflows) ? workflows[0] : workflows; // Save with the workflow's name, preserving ID const fileName = `${workflow.name.toLowerCase().replace(/\s+/g, '-')}.json`; const filePath = path.join(outputDir, fileName); // Store the full workflow object with ID await fs.writeFile(filePath, JSON.stringify(workflow, null, 2)); // Clean up temp file await fs.unlink(tempFile).catch(() => {}); return { content: [{ type: 'text', text: `β Exported workflow: ${workflow.name}\n` + `π File: ${fileName}\n` + `π ID: ${workflow.id}` }] }; } // If no specific options, show error return { content: [{ type: 'text', text: 'β Please specify either --id or --all for export' }] }; } catch (error: any) { throw new Error(`Failed to export workflow: ${error.message}`); } }