n8n CLI Command Line Interface Automation Scripting
I use the n8n CLI for everything I can automate from the terminal. GUI is great for building workflows visually. But when I need to push changes, run headless executions, or integrate with CI/CD pipelines, the CLI is where I live.
The n8n CLI bridges the gap between visual workflow building and programmatic automation. It’s not just a wrapper around the API — it has its own command set designed for developers who manage n8n instances regularly.
I manage 12 n8n instances across development, staging, and production. The CLI lets me switch between them, export workflows, and trigger executions without opening a browser. That workflow saves me at least 30 minutes per day.
Installing the n8n CLI
The CLI ships as an npm package. Install it globally:
`bash
npm install -g n8n
`
Verify the installation:
`bash
n8n –version
`
You should see the CLI version number. If you get a command not found error, check that your npm global bin directory is in your PATH:
`bash
echo $PATH
npm config get prefix
`
The CLI and the n8n server share the same package. Installing one gives you both. You can run the server with n8n start and invoke the CLI tools with n8n followed by a subcommand.
For existing installations, upgrade with:
`bash
npm update -g n8n
`
The CLI version tracks the main n8n release. Major and minor version changes arrive together. Patch versions may introduce CLI improvements without changing the server version.
Core CLI Commands
Five commands form the backbone of daily CLI usage:
`bash
Start the n8n server
n8n start
Run in headless mode (no web UI, API only)
n8n start –tunnel
Execute a single workflow by ID
n8n execute –workflowId=123
Export all workflows to JSON
n8n export –all –output=./backups/workflows.json
Import workflows from JSON
n8n import –all –input=./backups/workflows.json
`
The execute command is particularly powerful. It runs a workflow immediately and prints the execution result to stdout. I use it in shell scripts to trigger data syncs from cron jobs. No API key needed, no HTTP requests to manage.
For example, here’s a shell script that runs a data pipeline workflow and logs the result:
`bash
#!/bin/bash
echo “$(date): Starting data sync…”
RESULT=$(n8n execute –workflowId=456)
echo “$RESULT” >> /var/log/n8n-executions.log
echo “$(date): Sync complete. Exit code: $?”
`
Make it executable and schedule it with cron:
`bash
chmod +x sync-data.sh
(crontab -l 2>/dev/null; echo “0 2 * /path/to/sync-data.sh”) | crontab –
`
The workflow runs at 2 AM daily. The CLI executes it headlessly. The result logs to a file. No browser, no manual intervention.
Workflow Management
Export and import workflows for backup and migration:
`bash
Export specific workflow
n8n export –workflowId=123 –output=./workflows/backup-123.json
Export all workflows
n8n export –all –output=./workflows/all-backup.json
Import single workflow
n8n import –workflowId=./workflows/new-workflow.json
Import all workflows (creates or overwrites based on ID)
n8n import –all –input=./workflows/all-backup.json
`
The export command includes workflow metadata, node configurations, and credential references. It does not include the actual credential values — those are stored separately in the n8n database.
When migrating between instances, export from the source and import to the destination. The target instance must have the same credential connections configured. Workflow IDs don’t need to match — the import command handles ID assignment automatically.
For credential management, use the credential export/import commands:
`bash
Export all credentials
n8n export –credentials –output=./backups/credentials.json
Import credentials
n8n import –credentials –input=./backups/credentials.json
`
Be careful with credential imports. They overwrite existing credentials with matching names. Test on a staging instance first.
Headless Mode and Tunnel Support
Headless mode strips out the web UI and serves only the API. This reduces memory usage by roughly 40% and eliminates the overhead of serving static assets.
`bash
n8n start –headless
`
In headless mode, you can still use the CLI to execute workflows and manage resources. The web interface at http://localhost:5678 returns a 404. Only the API endpoints respond.
For local development and testing, the tunnel feature exposes your n8n instance to the internet through a secure relay:
`bash
n8n start –tunnel
`
This creates a public URL that forwards to your local n8n instance. Webhooks from external services route through the tunnel to your development environment. It’s perfect for testing webhook-triggered workflows without deploying to a public server.
The tunnel URL changes each time you start n8n. For production webhook testing, use a static URL with a proper domain and SSL certificate.
Integrating with CI/CD Pipelines
The CLI integrates cleanly with GitHub Actions, GitLab CI, and Jenkins. Here’s a GitHub Actions workflow that tests and deploys n8n workflows:
`yaml
name: n8n Workflow Deploy
on:
push:
paths:
– ‘n8n-workflows/**’
jobs:
deploy:
runs-on: ubuntu-latest
steps:
– uses: actions/checkout@v4
– name: Install n8n CLI
run: npm install -g n8n
– name: Export current workflows
run: n8n export –all –output=pre-deploy-backup.json
env:
N8N_BASIC_AUTH_ACTIVE: “true”
N8N_BASIC_AUTH_USER: ${{ secrets.N8N_USER }}
N8N_BASIC_AUTH_PASSWORD: ${{ secrets.N8N_PASSWORD }}
– name: Import new workflows
run: n8n import –all –input=n8n-workflows/deployment.json
– name: Validate workflows
run: |
for wf in n8n-workflows/*.json; do
n8n validate –input=”$wf” || exit 1
done
`
This pipeline triggers whenever workflow files change in the repository. It backs up the current state, imports new workflows, and validates them before deployment. If validation fails, the deployment stops and the backup remains intact.
For Jenkins, the equivalent pipeline uses shell steps:
`groovy
pipeline {
agent any
stages {
stage(‘Export’) {
steps {
sh ‘n8n export –all –output=pre-deploy-backup.json’
}
}
stage(‘Import’) {
steps {
sh ‘n8n import –all –input=workflows/staging.json’
}
}
stage(‘Validate’) {
steps {
sh ‘n8n validate –input=workflows/staging.json’
}
}
}
}
`
Environment Configuration
The CLI respects the same environment variables as the n8n server. Configure authentication, database, and storage through environment variables or a .env file:
`bash
Authentication
export N8N_BASIC_AUTH_ACTIVE=true
export N8N_BASIC_AUTH_USER=admin
export N8N_BASIC_AUTH_PASSWORD=securepassword
Database
export DB_TYPE=postgresdb
export DB_POSTGRESDB_HOST=localhost
export DB_POSTGRESDB_DATABASE=n8n
export DB_POSTGRESDB_USER=n8n
export DB_POSTGRESDB_PASSWORD=dbpassword
Storage
export N8N_EDITOR_BASE_URL=https://n8n.yourdomain.com
`
Set these in your shell profile, a .env file loaded by dotenv, or in your CI/CD pipeline’s environment variables section. The CLI reads them on startup and passes them to the server process.
For multi-instance management, create separate configuration files:
`bash
Development
n8n start –config=./configs/dev.env
Staging
n8n start –config=./configs/staging.env
Production
n8n start –config=./configs/prod.env
`
Each configuration file contains different database connections, authentication settings, and environment-specific values. Switch between instances by specifying the config file.
Connecting to What You Already Have
If you’re running n8n in Docker, the n8n Docker Deployment guide covers the container setup. The CLI runs inside the container or on the host — the commands are identical either way.
For Docker Compose setups, the n8n Docker Compose production stack includes CLI-accessible endpoints. Execute workflows from outside the container with docker exec.
The n8n Node Configuration guide covers the parameters and expressions you’ll use inside workflows triggered by the CLI. Understanding these makes CLI execution results much more actionable.
Action Card: Daily CLI Checklist
These five commands cover 90% of daily n8n management:
`bash
1. Check running workflows
n8n execute –workflowId=1 –dryRun
2. Export today’s backup
n8n export –all –output=./backups/$(date +%Y%m%d).json
3. Check execution logs
n8n execution list –limit 10
4. Trigger a specific workflow
n8n execute –workflowId=42 –saveDataOnError
5. Validate a new workflow before importing
n8n validate –input=./workflows/new-workflow.json
`
Run these in sequence after any workflow change. Five minutes of CLI work prevents hours of debugging broken automations.
