n8n – Public REST APIをつかってみる

はじめに

こちらのページにしたがって、self-hosted n8n instanceでREST APIを使ってみます。

OpenAPI document

API Keyの作成

  • Settings > n8n API ページを開き、API Keyを生成する

API呼び出し

  • GETの例
# For a self-hosted n8n instance
curl -X 'GET' \
  '<N8N_HOST>:<N8N_PORT>/<N8N_PATH>/api/v<version-number>/workflows' \
  -H 'accept: application/json' \
  -H 'X-N8N-API-KEY: <your-api-key>'

Workflowリストの取得

export MY_API_KEY=xxxxxxxxxx

curl -X 'GET' \
  'http://localhost:5678/api/v1/workflows?active=true' \
  -H 'accept: application/json' \
  -H "X-N8N-API-KEY: ${MY_API_KEY}"

{
  "data": [
    {
      "createdAt": "2025-08-10T11:40:09.843Z",
      "updatedAt": "2025-08-10T23:35:07.765Z",
      "id": "RDgcHfND3z2twqxf",
      "name": "Nathan's workflow",
      "active": true,
      "isArchived": false,
      "nodes": [
        {
          "parameters": {
            "url": "https://internal.users.n8n.cloud/webhook/custom-erp",
            "authentication": "genericCredentialType",
            "genericAuthType": "httpHeaderAuth",
            "sendHeaders": true,
            "headerParameters": {
              "parameters": [
                {
                  "name": "unique_id",
                  "value": "c4308b47b010a1659a2ec71c7deac2d5"
                }
              ]
            },
            "options": {}
          },
          "type": "n8n-nodes-base.httpRequest",
          "typeVersion": 4.2,
          "position": [
            208,
            0
          ],
          "id": "485d2947-cccb-4858-bde5-c3796c2bd107",
          "name": "HTTP Request",
          "credentials": {
            "httpHeaderAuth": {
              "id": "QSKG5oHPU8omPZPg",
              "name": "Header Auth account"
            }
          }
        },
        {
          "parameters": {
            "operation": "create",
            "base": {
              "__rl": true,
              "value": "appmF64espQAOG4h1",
              "mode": "list",
              "cachedResultName": "beginner course",
              "cachedResultUrl": "https://airtable.com/appmF64espQAOG4h1"
            },
            "table": {
              "__rl": true,
              "value": "tbl6Mzc2oCuEc4QWT",
              "mode": "list",
              "cachedResultName": "processingOrders",
              "cachedResultUrl": "https://airtable.com/appmF64espQAOG4h1/tbl6Mzc2oCuEc4QWT"
            },
            "columns": {
              "mappingMode": "autoMapInputData",
              "value": {},
              "matchingColumns": [],
              "schema": [
                {
                  "id": "orderID",
                  "displayName": "orderID",
                  "required": false,
                  "defaultMatch": false,
                  "canBeUsedToMatch": true,
                  "display": true,
                  "type": "number",
                  "readOnly": false,
                  "removed": false
                },
                {
                  "id": "employeeName",
                  "displayName": "employeeName",
                  "required": false,
                  "defaultMatch": false,
                  "canBeUsedToMatch": true,
                  "display": true,
                  "type": "string",
                  "readOnly": false,
                  "removed": false
                }
              ],
              "attemptToConvertTypes": false,
              "convertFieldsToString": false
            },
            "options": {}
          },
          "type": "n8n-nodes-base.airtable",
          "typeVersion": 2.1,
          "position": [
            896,
            -16
          ],
          "id": "ae3cbdc1-36ac-4866-9823-e26b321c0cdd",
          "name": "Create a record",
          "credentials": {
            "airtableTokenApi": {
              "id": "2Pf5Pq5aSQmr6jRS",
              "name": "Airtable Personal Access Token account"
            }
          }
        },
        {
          "parameters": {
            "conditions": {
              "options": {
                "caseSensitive": true,
                "leftValue": "",
                "typeValidation": "strict",
                "version": 2
              },
              "conditions": [
                {
                  "id": "dcddecaf-b5fe-425f-a5be-078e711c0781",
                  "leftValue": "={{ $json.orderStatus }}",
                  "rightValue": "processing",
                  "operator": {
                    "type": "string",
                    "operation": "equals",
                    "name": "filter.operator.equals"
                  }
                },
                {
                  "id": "266b4382-8126-4d42-80d4-69d663e4df69",
                  "leftValue": "={{ $json.employeeName }}",
                  "rightValue": "Mario",
                  "operator": {
                    "type": "string",
                    "operation": "equals",
                    "name": "filter.operator.equals"
                  }
                }
              ],
              "combinator": "and"
            },
            "options": {}
          },
          "type": "n8n-nodes-base.if",
          "typeVersion": 2.2,
          "position": [
            368,
            16
          ],
          "id": "0d4e3ec6-c3b7-408b-b88b-4abd235ec79f",
          "name": "If"
        },
        {
          "parameters": {
            "assignments": {
              "assignments": [
                {
                  "id": "4b6cc025-364c-4dc4-ab6b-c970c6b9dfe9",
                  "name": "orderID",
                  "value": "={{ $json.orderID }}",
                  "type": "number"
                },
                {
                  "id": "e1d433bf-5a50-4dde-8fd5-8479e18a2467",
                  "name": "employeeName",
                  "value": "={{ $json.employeeName }}",
                  "type": "string"
                }
              ]
            },
            "options": {}
          },
          "type": "n8n-nodes-base.set",
          "typeVersion": 3.4,
          "position": [
            720,
            -64
          ],
          "id": "3672720f-b47d-4109-b390-6cc0713649dc",
          "name": "Edit Fields"
        },
        {
          "parameters": {
            "jsCode": "let items = $input.all();\nlet totalBooked = items.length;\nlet bookedSum = 0;\n\nfor (let i=0; i < items.length; i++) {\n  bookedSum = bookedSum + items[i].json.orderPrice;\n}\n\nreturn [{ json: {totalBooked, bookedSum} }];"
          },
          "type": "n8n-nodes-base.code",
          "typeVersion": 2,
          "position": [
            560,
            128
          ],
          "id": "cf8bf31b-f4a1-4cfa-a29f-866eb6d3f425",
          "name": "Code"
        },
        {
          "parameters": {
            "authentication": "webhook",
            "content": "=This week we've {{$json[\"totalBooked\"]}} booked orders with a total value of {{$json[\"bookedSum\"]}}. My Unique ID: {{ $('HTTP Request').params[\"headerParameters\"][\"parameters\"][0][\"value\"] }}",
            "options": {}
          },
          "type": "n8n-nodes-base.discord",
          "typeVersion": 2,
          "position": [
            896,
            160
          ],
          "id": "672365da-5f25-40fc-946f-984e5d3f150d",
          "name": "Discord",
          "webhookId": "9217ea18-4139-4711-93c2-1bf99bc6a796",
          "credentials": {
            "discordWebhookApi": {
              "id": "Rvqb7eRGgRzEzazP",
              "name": "Discord Webhook account"
            }
          }
        },
        {
          "parameters": {
            "rule": {
              "interval": [
                {
                  "field": "weeks",
                  "triggerAtDay": [
                    1
                  ],
                  "triggerAtHour": 9
                }
              ]
            }
          },
          "type": "n8n-nodes-base.scheduleTrigger",
          "typeVersion": 1.2,
          "position": [
            16,
            0
          ],
          "id": "7fa9bbdc-b008-412f-88d3-56b1f04282a7",
          "name": "Schedule Trigger"
        },
        {
          "parameters": {
            "jsCode": "let items = $input.all();\nlet totalBooked = items.length;\nlet bookedSum = 0;\n\nfor (let i=0; i < items.length; i++) {\n  bookedSum = bookedSum + items[i].json.orderPrice;\n}\n\nreturn [{ json: {totalBooked, bookedSum} }];"
          },
          "type": "n8n-nodes-base.code",
          "typeVersion": 2,
          "position": [
            528,
            -112
          ],
          "id": "7b37ab5e-b915-4dfa-a190-49067af2bf7e",
          "name": "Code1"
        }
      ],
      "connections": {
        "HTTP Request": {
          "main": [
            [
              {
                "node": "If",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "If": {
          "main": [
            [
              {
                "node": "Code1",
                "type": "main",
                "index": 0
              }
            ],
            [
              {
                "node": "Code",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Edit Fields": {
          "main": [
            [
              {
                "node": "Create a record",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Code": {
          "main": [
            [
              {
                "node": "Discord",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Schedule Trigger": {
          "main": [
            [
              {
                "node": "HTTP Request",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Code1": {
          "main": [
            [
              {
                "node": "Edit Fields",
                "type": "main",
                "index": 0
              }
            ]
          ]
        }
      },
      "settings": {
        "executionOrder": "v1"
      },
      "staticData": {
        "node:Schedule Trigger": {
          "recurrenceRules": []
        }
      },
      "meta": {
        "templateCredsSetupCompleted": true
      },
      "pinData": {},
      "versionId": "029e814e-e442-4f53-a62f-e6e7208b2567",
      "triggerCount": 1,
      "tags": []
    }
  ],
  "nextCursor": null
}

Credential data schemaの取得

curl -X 'GET' \
  'http://localhost:5678/api/v1/credentials/schema/googleDriveOAuth2Api' \
  -H 'accept: application/json' \
  -H "X-N8N-API-KEY: ${MY_API_KEY}"

{
  "additionalProperties": false,
  "type": "object",
  "properties": {
    "clientId": {
      "type": "string"
    },
    "clientSecret": {
      "type": "string"
    },
    "oauthTokenData": {
      "type": "json"
    },
    "notice": {
      "type": "notice"
    }
  },
  "required": [
    "clientId",
    "clientSecret"
  ]
}

関連記事

カテゴリー

アーカイブ

Lang »