{ "nodes": [ { "id": "check-twitch-stream-live-0", "name": "Document", "type": "n8n-nodes-base.set", "position": [ -600, 240 ], "parameters": { "options": {}, "assignments": { "assignments": [ { "id": "dba7b91b-17e3-4096-92aa-a6e5fe60eb55", "name": "twitch", "type": "string", "value": "YOUR-TWITCH-USERNAME" } ] } }, "typeVersion": 3.4 }, { "id": "check-twitch-stream-live-1", "name": "Twitch GraphQL", "type": "n8n-nodes-base.httpRequest", "position": [ -380, 240 ], "parameters": { "url": "=https://api.twitch.tv/helix/streams?user_login={{ encodeURIComponent($json.twitch) }}", "authentication": "genericCredentialType", "genericAuthType": "httpHeaderAuth", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Client-Id", "value": "YOUR_CLIENT_ID" } ] }, "options": {} }, "typeVersion": 4.2 }, { "id": "check-twitch-stream-live-2", "name": "Is Online", "type": "n8n-nodes-base.if", "position": [ -160, 240 ], "parameters": { "options": {}, "conditions": { "options": { "version": 2, "leftValue": "", "caseSensitive": true, "typeValidation": "strict" }, "combinator": "and", "conditions": [ { "id": "311e3b31-03e7-4763-8b4a-ebc9a18b77fd", "operator": { "type": "boolean", "operation": "true", "singleValue": true }, "leftValue": "={{ $json.data.user.stream !== null }}", "rightValue": "" } ] } }, "typeVersion": 2.2 }, { "id": "check-twitch-stream-live-3", "name": "When clicking ‘Test workflow’", "type": "n8n-nodes-base.manualTrigger", "position": [ -860, 240 ], "parameters": {}, "typeVersion": 1 }, { "id": "check-twitch-stream-live-4", "name": "Sticky Note", "type": "n8n-nodes-base.stickyNote", "position": [ -680, 40 ], "parameters": { "content": "The document node serves as sample source for `twitch` username to check\n" }, "typeVersion": 1 }, { "id": "check-twitch-stream-live-5", "name": "Sticky Note1", "type": "n8n-nodes-base.stickyNote", "position": [ -460, 420 ], "parameters": { "content": "the value of `client-id` parameter is a fixed known value used by twitch for anonymous call used in their website\n" }, "typeVersion": 1 }, { "id": "check-twitch-stream-live-6", "name": "Sticky Note2", "type": "n8n-nodes-base.stickyNote", "position": [ -240, 40 ], "parameters": { "content": "we need only to check the value of `stream` if `null` to know if the user offline. Any value will denote the user is online" }, "typeVersion": 1 }, { "id": "check-twitch-stream-live-7", "name": "Map stream response", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -280, 400 ], "parameters": { "jsCode": "return $input.all().map(item => { const data=item.json.data; if (!Array.isArray(data) || data.length > 1) throw new Error('Unexpected Twitch response'); const x=data[0]; if (x && (typeof x.id !== 'string' || typeof x.viewer_count !== 'number' || typeof x.title !== 'string' || typeof x.type !== 'string' || typeof x.game_id !== 'string')) throw new Error('Unexpected Twitch stream'); return {json:{data:{user:{stream:x ? {id:x.id,viewersCount:x.viewer_count,title:x.title,type:x.type,game:x.game_id ? {id:x.game_id}:null} : null}}}}; });" } } ], "connections": { "Document": { "main": [ [ { "node": "Twitch GraphQL", "type": "main", "index": 0 } ] ] }, "Twitch GraphQL": { "main": [ [ { "node": "Map stream response", "type": "main", "index": 0 } ] ] }, "When clicking ‘Test workflow’": { "main": [ [ { "node": "Document", "type": "main", "index": 0 } ] ] }, "Map stream response": { "main": [ [ { "node": "Is Online", "type": "main", "index": 0 } ] ] } }, "name": "Check Whether a Twitch Stream Is Live", "active": false, "pinData": {} }