-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Expand file tree
/
Copy pathResilient REST API Sync with Retries, Validation, and Sanitized Errors.json
More file actions
297 lines (297 loc) · 9.92 KB
/
Copy pathResilient REST API Sync with Retries, Validation, and Sanitized Errors.json
File metadata and controls
297 lines (297 loc) · 9.92 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{
"id": "e23e4a12-4625-47bb-9a9b-5eff5a04e32d",
"name": "Resilient REST API Sync with Retries, Validation, and Sanitized Errors",
"active": false,
"versionId": "f629614a-9017-47df-83f7-bc6bded77612",
"nodes": [
{
"parameters": {
"content": "## Resilient REST API Sync\nUse this workflow when an automation must pull records from a REST API without failing silently.\n\n**What it demonstrates:** bounded retries, a 10-second timeout, required-field validation, and a credential-safe failure record.\n\nRun the included JSONPlaceholder demo first. Then follow the production checklist before connecting a real system.",
"height": 250,
"width": 480,
"color": 5
},
"id": "4b62a70f-e30f-4f72-91ef-e3aa06f88200",
"name": "Template Guide",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-760,
-380
]
},
{
"parameters": {
"content": "### 1 · Configure and run the safe demo\nClick **Run Demo**. The configuration node supplies a public test endpoint and the required fields `id,name,email`.\n\n**Expected input:** no credentials or manual data.\n**Expected result:** ten sample user records continue to the fetch step.",
"height": 210,
"width": 470,
"color": 3
},
"id": "73a6651f-42d2-4a91-991b-8a67d9750db7",
"name": "Step 1 Configure Demo",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-720,
20
]
},
{
"parameters": {
"content": "### 2 · Fetch with bounded retries\nThe HTTP request waits up to 10 seconds and retries failed calls 3 times with a 3-second delay.\n\n**Success output:** API records.\n**Error output:** routes to the sanitized failure path instead of stopping silently.",
"height": 210,
"width": 230,
"color": 6
},
"id": "e2191158-4703-460d-8b0b-b5057d884b85",
"name": "Step 2 Fetch Safely",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-230,
20
]
},
{
"parameters": {
"content": "### 3A · Normalize and validate success\nThe Code node accepts an array or individual items, normalizes names and email addresses, and checks every configured required field.\n\n**Use this output:** connect **Validated Records** to the database, spreadsheet, or app that should receive reviewed records.",
"height": 210,
"width": 500,
"color": 4
},
"id": "d2037abc-bb15-4724-bdc3-59c77667e100",
"name": "Step 3A Validate Success",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
30,
-80
]
},
{
"parameters": {
"content": "### 3B · Emit a safe failure record\nRequest errors and validation exceptions land here. The Code node removes common credential patterns, limits the message to 240 characters, and returns a retryable failure record with a next step.\n\n**Use this output:** connect **Failure Record** to a private log or alert channel.",
"height": 220,
"width": 500,
"color": 2
},
"id": "11ae143e-dfb5-4dfe-9214-e55cf3c06fc6",
"name": "Step 3B Sanitize Failure",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
30,
140
]
},
{
"parameters": {},
"id": "4d9f589c-bb4c-41e6-88d0-ad483d01dd8b",
"name": "Run Demo",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
-660,
80
]
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "4cdfa4ca-52ea-45ca-b015-256629721e21",
"name": "request_url",
"value": "https://jsonplaceholder.typicode.com/users",
"type": "string"
},
{
"id": "2ce9b783-9b16-4c42-aa74-633314ffed46",
"name": "required_fields",
"value": "id,name,email",
"type": "string"
}
]
},
"options": {}
},
"id": "8facdcd0-4b55-4693-9b15-25a6d2960d38",
"name": "Set Demo Configuration",
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-430,
80
]
},
{
"parameters": {
"url": "={{ $json.request_url }}",
"options": {
"response": {
"response": {
"responseFormat": "json"
}
},
"timeout": 10000
}
},
"id": "54588bd7-abbe-450e-a618-143c05e742ef",
"name": "Fetch API Records",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.4,
"position": [
-180,
80
],
"retryOnFail": true,
"maxTries": 3,
"waitBetweenTries": 3000,
"onError": "continueErrorOutput"
},
{
"parameters": {
"jsCode": "const incoming = $input.all();\nconst flattened = incoming.length === 1 && Array.isArray(incoming[0].json)\n ? incoming[0].json\n : incoming.map((item) => item.json);\nconst requiredFields = String(\n $('Set Demo Configuration').first().json.required_fields || 'id,name,email'\n)\n .split(',')\n .map((field) => field.trim())\n .filter(Boolean);\n\nif (flattened.length === 0) {\n throw new Error('EMPTY_RESPONSE: The upstream API returned no records.');\n}\n\nreturn flattened.map((record, index) => {\n const missing = requiredFields.filter((field) => {\n const value = record?.[field];\n return value === undefined || value === null || String(value).trim() === '';\n });\n\n return {\n json: {\n source_index: index,\n record_id: record?.id ?? null,\n name: String(record?.name ?? '').trim(),\n email: String(record?.email ?? '').trim().toLowerCase(),\n validation_status: missing.length === 0 ? 'valid' : 'invalid',\n missing_fields: missing,\n source_domain: record?.website ?? null\n }\n };\n});"
},
"id": "6db98386-50d1-4880-b374-71701fe7ec10",
"name": "Normalize and Validate Records",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
80,
-20
],
"onError": "continueErrorOutput"
},
{
"parameters": {},
"id": "11d079f9-e61e-4513-96c6-fadf07b929c8",
"name": "Validated Records",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
350,
-20
]
},
{
"parameters": {
"jsCode": "const failure = $input.first().json;\nconst raw = String(\n failure?.error?.message ||\n failure?.error?.description ||\n failure?.message ||\n 'The upstream request failed.'\n);\n\nconst sanitized = raw\n .replace(/(?:bearer|token|api[_-]?key|password|authorization)\\s*[:=]\\s*[^\\s,;]+/gi, '[credential redacted]')\n .replace(/[?&](?:key|token|api_key|access_token)=[^&\\s]+/gi, '?credential=[redacted]')\n .slice(0, 240);\n\nreturn [{\n json: {\n status: 'failed',\n error: 'upstream_request_failed',\n message: sanitized,\n retryable: true,\n failed_at: new Date().toISOString(),\n next_step: 'Verify the URL and authentication, then run the workflow again.'\n }\n}];"
},
"id": "5a4ace78-3334-4103-9d51-e59c48e5138f",
"name": "Sanitize Failure",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
80,
190
]
},
{
"parameters": {},
"id": "6f8a5ea7-4ee2-411d-83e2-0a1600e3a628",
"name": "Failure Record",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
350,
190
]
},
{
"parameters": {
"content": "### Before using real data\n1. Replace the public test URL with the production endpoint.\n2. Select an n8n credential in the HTTP Request node—never paste secrets into the URL or exported JSON.\n3. Set `required_fields` to the fields your downstream system needs.\n4. Test both outputs: a valid response and a deliberate bad URL.\n5. Connect **Validated Records** to the destination and **Failure Record** to a private log or alert.\n6. Add a separate Error Workflow for failures outside this handled API call.",
"height": 280,
"width": 520,
"color": 4
},
"id": "9848a6b2-7e5e-4c02-bd8a-e66ef79e87ed",
"name": "Production Checklist",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
40,
-390
]
}
],
"connections": {
"Run Demo": {
"main": [
[
{
"node": "Set Demo Configuration",
"type": "main",
"index": 0
}
]
]
},
"Set Demo Configuration": {
"main": [
[
{
"node": "Fetch API Records",
"type": "main",
"index": 0
}
]
]
},
"Fetch API Records": {
"main": [
[
{
"node": "Normalize and Validate Records",
"type": "main",
"index": 0
}
],
[
{
"node": "Sanitize Failure",
"type": "main",
"index": 0
}
]
]
},
"Normalize and Validate Records": {
"main": [
[
{
"node": "Validated Records",
"type": "main",
"index": 0
}
],
[
{
"node": "Sanitize Failure",
"type": "main",
"index": 0
}
]
]
},
"Sanitize Failure": {
"main": [
[
{
"node": "Failure Record",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {
"executionOrder": "v1"
},
"pinData": {},
"meta": {
"templateCredsSetupCompleted": true
},
"tags": []
}