-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathn8n_api_examples.sh
More file actions
232 lines (195 loc) · 5.32 KB
/
n8n_api_examples.sh
File metadata and controls
232 lines (195 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
# n8n API Examples
# Thay thế các biến này với thông tin của bạn
N8N_HOST="localhost"
N8N_PORT="5678"
N8N_PATH=""
API_KEY="your-api-key"
BASE_URL="$N8N_HOST:$N8N_PORT/$N8N_PATH"
# Hàm để thực hiện các cuộc gọi API
call_api() {
local method=$1
local endpoint=$2
local data=$3
if [ -z "$data" ]; then
curl -X "$method" \
"$BASE_URL/api/v1/$endpoint" \
-H "accept: application/json" \
-H "X-N8N-API-KEY: $API_KEY"
else
curl -X "$method" \
"$BASE_URL/api/v1/$endpoint" \
-H "accept: application/json" \
-H "X-N8N-API-KEY: $API_KEY" \
-H "Content-Type: application/json" \
-d "$data"
fi
}
echo "=== 1. API Người dùng (User) ==="
# Lấy tất cả người dùng
echo "Lấy tất cả người dùng:"
call_api "GET" "users?includeRole=true"
echo -e "\n"
# Tạo người dùng mới
echo "Tạo người dùng mới:"
call_api "POST" "users" '[{"email": "user@example.com", "role": "global:member"}]'
echo -e "\n"
# Lấy người dùng theo ID
echo "Lấy người dùng theo ID:"
call_api "GET" "users/123e4567-e89b-12d3-a456-426614174000"
echo -e "\n"
# Thay đổi vai trò của người dùng
echo "Thay đổi vai trò của người dùng:"
call_api "PATCH" "users/123e4567-e89b-12d3-a456-426614174000/role" '{"newRoleName": "global:admin"}'
echo -e "\n"
echo "=== 2. API Workflow ==="
# Lấy tất cả workflow
echo "Lấy tất cả workflow:"
call_api "GET" "workflows?active=true"
echo -e "\n"
# Tạo workflow mới
echo "Tạo workflow mới:"
call_api "POST" "workflows" '{
"name": "Workflow Example",
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [100, 300]
}
],
"connections": {
"main": []
},
"settings": {
"saveExecutionProgress": true,
"saveManualExecutions": true,
"saveDataErrorExecution": "all",
"saveDataSuccessExecution": "all",
"executionTimeout": 3600,
"timezone": "America/New_York"
},
"staticData": {}
}'
echo -e "\n"
# Lấy workflow theo ID
echo "Lấy workflow theo ID:"
call_api "GET" "workflows/2tUt1wbLX592XDdX"
echo -e "\n"
# Kích hoạt workflow
echo "Kích hoạt workflow:"
call_api "POST" "workflows/2tUt1wbLX592XDdX/activate"
echo -e "\n"
# Vô hiệu hóa workflow
echo "Vô hiệu hóa workflow:"
call_api "POST" "workflows/2tUt1wbLX592XDdX/deactivate"
echo -e "\n"
# Cập nhật workflow
echo "Cập nhật workflow:"
call_api "PUT" "workflows/2tUt1wbLX592XDdX" '{
"name": "Workflow Example Updated",
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [100, 300]
}
],
"connections": {
"main": []
},
"settings": {
"saveExecutionProgress": true,
"saveManualExecutions": true,
"saveDataErrorExecution": "all",
"saveDataSuccessExecution": "all",
"executionTimeout": 3600,
"timezone": "America/New_York"
},
"staticData": {}
}'
echo -e "\n"
echo "=== 3. API Thực thi (Execution) ==="
# Lấy tất cả thực thi
echo "Lấy tất cả thực thi:"
call_api "GET" "executions?includeData=true"
echo -e "\n"
# Lấy thực thi theo ID
echo "Lấy thực thi theo ID:"
call_api "GET" "executions/1000?includeData=true"
echo -e "\n"
# Xóa thực thi
echo "Xóa thực thi:"
call_api "DELETE" "executions/1000"
echo -e "\n"
echo "=== 4. API Thông tin xác thực (Credential) ==="
# Tạo thông tin xác thực mới
echo "Tạo thông tin xác thực mới:"
call_api "POST" "credentials" '{
"name": "Github Credentials Example",
"type": "github",
"data": {
"token": "your-github-token"
}
}'
echo -e "\n"
# Lấy schema thông tin xác thực
echo "Lấy schema thông tin xác thực:"
call_api "GET" "credentials/schema/github"
echo -e "\n"
echo "=== 5. API Tags ==="
# Tạo tag mới
echo "Tạo tag mới:"
call_api "POST" "tags" '{"name": "Development"}'
echo -e "\n"
# Lấy tất cả tags
echo "Lấy tất cả tags:"
call_api "GET" "tags"
echo -e "\n"
# Cập nhật tag
echo "Cập nhật tag:"
call_api "PUT" "tags/2tUt1wbLX592XDdX" '{"name": "Development-Updated"}'
echo -e "\n"
echo "=== 6. API Biến (Variables) ==="
# Tạo biến mới
echo "Tạo biến mới:"
call_api "POST" "variables" '{"key": "DATABASE_URL", "value": "postgres://user:pass@localhost:5432/db"}'
echo -e "\n"
# Lấy tất cả biến
echo "Lấy tất cả biến:"
call_api "GET" "variables"
echo -e "\n"
echo "=== 7. API Dự án (Projects) ==="
# Tạo dự án mới
echo "Tạo dự án mới:"
call_api "POST" "projects" '{"name": "Marketing Automation"}'
echo -e "\n"
# Lấy tất cả dự án
echo "Lấy tất cả dự án:"
call_api "GET" "projects"
echo -e "\n"
echo "=== 8. API Kiểm toán (Audit) ==="
# Tạo báo cáo kiểm toán bảo mật
echo "Tạo báo cáo kiểm toán bảo mật:"
call_api "POST" "audit" '{
"additionalOptions": {
"daysAbandonedWorkflow": 30,
"categories": []
}
}'
echo -e "\n"
echo "=== 9. API Kiểm soát nguồn (Source Control) ==="
# Kéo thay đổi từ kho lưu trữ từ xa
echo "Kéo thay đổi từ kho lưu trữ từ xa:"
call_api "POST" "source-control/pull" '{
"force": true,
"variables": {
"environment": "production"
}
}'
echo -e "\n"
echo "Hoàn thành các ví dụ API n8n!"