export
Export n8n workflows to JSON files for backup, sharing, or migration. Specify individual workflows or export all at once with customizable output formatting.
Instructions
Export workflows from n8n - replaces "n8n export:workflow" command
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | No | Workflow ID to export | |
| all | No | Export all workflows | |
| outputPath | No | Output directory path (defaults to workflows/flows) | |
| pretty | No | Format JSON output prettily (default: true) |
Implementation Reference
- src/n8n/manager.ts:543-647 (handler)Core implementation of the export tool: executes n8n CLI export:workflow command, handles single workflow by ID or all workflows, saves them as separate JSON files in the specified output directory (defaults to workflows/flows), preserving workflow IDs.async 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}`); } }
- src/tools/handler.ts:147-154 (handler)ToolHandler switch case that receives tool calls for 'export' and delegates to N8nManager.exportWorkflow with parsed arguments.case '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:243-267 (schema)Tool schema definition including name, description, and input schema for the 'export' tool, returned by getToolDefinitions().{ name: '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-85 (registration)Registers the tools (including 'export') with the MCP server by handling ListToolsRequest (via getToolDefinitions) and CallToolRequest (via ToolHandler).this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: getToolDefinitions(), })); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { return await this.toolHandler.handleTool( request.params.name, request.params.arguments ); });