You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程在Node-RED中导入导出流与配置流?可用API有哪些?

Great question! Node-RED has a solid set of built-in HTTP APIs that let you programmatically handle importing and exporting both flows and configuration flows. Let me walk you through the key ones, along with practical examples:

Core APIs for Flow & Config Flow Import/Export

Exporting Flows & Config Nodes

Node-RED uses a single endpoint for exporting, with query parameters to filter what you get.

Exporting Full Flow Set (Including Config Nodes)

To grab everything—regular flow nodes and config nodes (like MQTT brokers, DB connections)—use the base GET /flows endpoint:

curl -X GET http://localhost:1880/flows -H "Node-RED-API-Key: YOUR_ADMIN_API_KEY"

The response is a JSON array containing all nodes in your instance.

Exporting Only Regular Flows

If you want to exclude config nodes, add the type=flow query parameter:

curl -X GET http://localhost:1880/flows?type=flow -H "Node-RED-API-Key: YOUR_ADMIN_API_KEY"

Exporting Only Config Nodes

To pull just the configuration flows (config nodes), use type=config:

curl -X GET http://localhost:1880/flows?type=config -H "Node-RED-API-Key: YOUR_ADMIN_API_KEY"

You can also target a specific flow by adding ?id=YOUR_FLOW_ID to any of these requests.

Importing Flows & Config Nodes

Importing uses the POST /flows endpoint, with options to control how new nodes interact with existing ones.

Basic Import (Merge Mode)

By default, importing uses merge mode—new nodes are added, and existing nodes with matching IDs are updated. Send your flow JSON in the request body:

curl -X POST http://localhost:1880/flows \
  -H "Node-RED-API-Key: YOUR_ADMIN_API_KEY" \
  -H "Content-Type: application/json" \
  -d @your-flow-file.json

Replace All Flows

If you want to overwrite all existing flows with your imported set, use the importMode=replace query parameter:

curl -X POST http://localhost:1880/flows?importMode=replace \
  -H "Node-RED-API-Key: YOUR_ADMIN_API_KEY" \
  -H "Content-Type: application/json" \
  -d @your-full-flow-set.json

Import as a Subflow

To import a flow as a reusable subflow, use importMode=node:

curl -X POST http://localhost:1880/flows?importMode=node \
  -H "Node-RED-API-Key: YOUR_ADMIN_API_KEY" \
  -H "Content-Type: application/json" \
  -d @your-subflow.json

Importing Config Nodes Alone

Config nodes can be imported by themselves—just send a JSON array containing only the config node objects in your POST request. If the config nodes are referenced by regular flow nodes, it’s best to import them together to avoid dependency errors.

Critical Tips for Programmatic Use

  • Authentication: Always include the Node-RED-API-Key header if you’ve enabled adminAuth in your settings.js. API keys are far more reliable for scripted access than session cookies.
  • Revision Conflicts: When updating flows, include the rev property (from the /flows export response) in your POST body. Node-RED uses this to prevent concurrent edit conflicts—omitting it may result in a 409 Conflict error.
  • Error Handling: Keep an eye on HTTP status codes:
    • 200 OK: Success
    • 400 Bad Request: Invalid payload (check your JSON formatting)
    • 401 Unauthorized: Auth failure (double-check your API key)
    • 409 Conflict: Revision mismatch (you need to fetch the latest flow revision first)
  • Node.js Example: If you’re using Node.js to automate this, here’s a quick snippet with axios:
    const axios = require('axios');
    const RED_API_KEY = 'YOUR_ADMIN_API_KEY';
    const RED_BASE_URL = 'http://localhost:1880';
    
    // Export all flows
    async function exportAllFlows() {
      try {
        const res = await axios.get(`${RED_BASE_URL}/flows`, {
          headers: { 'Node-RED-API-Key': RED_API_KEY }
        });
        console.log('Exported flows:', res.data);
        return res.data;
      } catch (err) {
        console.error('Export failed:', err.response?.data || err.message);
      }
    }
    
    // Import flows with merge mode
    async function importFlows(flowData) {
      try {
        const res = await axios.post(`${RED_BASE_URL}/flows`, flowData, {
          headers: {
            'Node-RED-API-Key': RED_API_KEY,
            'Content-Type': 'application/json'
          },
          params: { importMode: 'merge' }
        });
        console.log('Import successful:', res.data);
      } catch (err) {
        console.error('Import failed:', err.response?.data || err.message);
      }
    }
    

内容的提问来源于stack exchange,提问作者Rocco Musolino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:30:39