Example: streamlabs api
Streamlabs api example
This flow is an example of authorization for the streamlabs api with node-red.
Start
First of all, you will have to create a streamlabs (or test) application in settings/api-settings -> my apps
remember to whitelist the correct user and use the correct redirect_uri
then you will have the necessary data to use the api
[{"id":"80225fdace93e1c1","type":"tab","label":"streamlabs api","disabled":false,"info":"","env":[]},{"id":"ff1721f0615e6b34","type":"http response","z":"80225fdace93e1c1","name":"","statusCode":"","headers":{},"x":590,"y":200,"wires":[]},{"id":"bd6f8ed8363e6f0c","type":"function","z":"80225fdace93e1c1","name":"","func":"const STREAMLABS_API_BASE = 'https://www.streamlabs.com/api/v1.0'\nconst client = flow.get(\"streamlabs.client\")\nlet params = {\n \"client_id\":client.clientId,\n \"redirect_uri\":client.redirectUrl,\n 'response_type': 'code',\n 'scope': client.scope,\n }\n\nmsg.url = `${STREAMLABS_API_BASE}/authorize?`\nmsg.url += Object.keys(params).map(k => `${k}=${params[k]}`).join('&')\n\nmsg.payload = `<a href=\"${msg.url}\">Authorize with Streamlabs!</a>`\nmsg.req.headers ={\n \"Accept\": \"application/json\"\n}\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":200,"wires":[["ff1721f0615e6b34"]]},{"id":"823ccfef93e90f4d","type":"http in","z":"80225fdace93e1c1","name":"","url":"streamlabs","method":"get","upload":false,"swaggerDoc":"","x":160,"y":200,"wires":[["bd6f8ed8363e6f0c"]]},{"id":"c179e99cb875e61f","type":"comment","z":"80225fdace93e1c1","name":"auth to get code","info":"","x":160,"y":160,"wires":[]},{"id":"1fd2ad474ea34f5b","type":"inject","z":"80225fdace93e1c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3600","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":80,"wires":[["aac46cabaa41e1da"]]},{"id":"aac46cabaa41e1da","type":"function","z":"80225fdace93e1c1","name":"Streamlabs Configuration","func":"let client = {\n clientId: 'your clientId',\n clientSecret: 'your clientSecret',\n redirectUrl: 'http://localhost:1880/streamlabs/auth',\n //important to specify or u can get code 400\n 'scope': 'donations.read+donations.create+alerts.create',\n}\n\nflow.set(\"streamlabs.client\",client)\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":80,"wires":[["f87348b7ba031920"]],"icon":"font-awesome/fa-drivers-license"},{"id":"f87348b7ba031920","type":"debug","z":"80225fdace93e1c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":80,"wires":[]},{"id":"67563c301a3fc466","type":"comment","z":"80225fdace93e1c1","name":"https://streamlabs.readme.io/v1.0/docs/alerts","info":"","x":250,"y":120,"wires":[]},{"id":"85e708be7bd57351","type":"http in","z":"80225fdace93e1c1","name":"","url":"streamlabs/auth","method":"get","upload":false,"swaggerDoc":"","x":180,"y":300,"wires":[["a1895ddf9e56f2a3"]]},{"id":"250c5e46bacde799","type":"http response","z":"80225fdace93e1c1","name":"","statusCode":"","headers":{},"x":890,"y":300,"wires":[]},{"id":"a57b717de5b593e3","type":"debug","z":"80225fdace93e1c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":669,"y":261,"wires":[]},{"id":"a1895ddf9e56f2a3","type":"function","z":"80225fdace93e1c1","name":"get token ","func":"\nflow.set(\"streamlabs.code\",msg.payload.code||\"\")\n\nconst client = flow.get(\"streamlabs.client\")\n \n var querystring = {\n \"grant_type\":\"authorization_code\",\n \"client_id\":client.clientId,\n \"client_secret\":client.clientSecret,\n \"redirect_uri\":client.redirectUrl,\n \"code\":msg.payload.code\n}\n\nmsg.url = \"https://streamlabs.com/api/v1.0/token\"\nmsg.method = \"POST\"\n\n msg.payload = querystring\n// flow.set(\"streamlabs.token\",msg.socketToken||\"\")\n\n\n\n\n return msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":300,"wires":[["77e8f3563f2b8e6a"]]},{"id":"77e8f3563f2b8e6a","type":"http request","z":"80225fdace93e1c1","name":"","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":550,"y":300,"wires":[["a57b717de5b593e3","da6e013174aa4937"]]},{"id":"da6e013174aa4937","type":"function","z":"80225fdace93e1c1","name":"save token ","func":"\n \nflow.set(\"streamlabs.token\",msg.payload||\"\")\n \nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":300,"wires":[["250c5e46bacde799"]]},{"id":"f51453d3d7c03fb8","type":"comment","z":"80225fdace93e1c1","name":"get token ((Same url as \"redirect_uri\"))","info":"","x":230,"y":260,"wires":[]},{"id":"c53d79419b8fe674","type":"inject","z":"80225fdace93e1c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":440,"wires":[["8c3a194c32a2d58d"]]},{"id":"7f37a695e2c5a3cc","type":"debug","z":"80225fdace93e1c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":440,"wires":[]},{"id":"080683ddcc1de5c0","type":"http request","z":"80225fdace93e1c1","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":570,"y":440,"wires":[["7f37a695e2c5a3cc"]]},{"id":"8c3a194c32a2d58d","type":"function","z":"80225fdace93e1c1","name":"Streamlabs user","func":" const token = flow.get(\"streamlabs.token\")\nconst STREAMLABS_API_BASE = 'https://www.streamlabs.com/api/v1.0'\n \nvar querystring = {\n \"access_token\":token.access_token,\n}\n\nmsg.url = `${STREAMLABS_API_BASE}/user?`\nmsg.url += Object.keys(querystring).map(k => `${k}=${querystring[k]}`).join('&')\nmsg.headers ={\n Accept: 'application/json'\n}\nmsg.rejectUnauthorized=false\nreturn msg\n \n \n \n \n ","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":440,"wires":[["080683ddcc1de5c0"]],"icon":"font-awesome/fa-drivers-license"},{"id":"926e533465461b72","type":"inject","z":"80225fdace93e1c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":480,"wires":[["ccf1a62244c6a4e4"]]},{"id":"f283e60ba8403465","type":"debug","z":"80225fdace93e1c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":480,"wires":[]},{"id":"b95e99847d5bbe9e","type":"http request","z":"80225fdace93e1c1","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":570,"y":480,"wires":[["f283e60ba8403465"]]},{"id":"ccf1a62244c6a4e4","type":"function","z":"80225fdace93e1c1","name":"Streamlabs Custom alert","func":" const token = flow.get(\"streamlabs.token\")\nconst STREAMLABS_API_BASE = 'https://www.streamlabs.com/api/v1.0'\n \nvar querystring = {\n \"access_token\":token.access_token,\n \"type\":\"donation\",\n \"message\":\"ALERT\",\n \"user_message\":\"fool who reads it\",\n \"image_href\":\"https://i.imgur.com/csmEY6z.gif\"\n}\n\nmsg.url = `${STREAMLABS_API_BASE}/alerts`\nmsg.payload = querystring\n// msg.url += Object.keys(querystring).map(k => `${k}=${querystring[k]}`).join('&')\nmsg.headers ={\n Accept: 'application/json'\n}\nmsg.rejectUnauthorized=false\n return msg\n \n \n \n \n ","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":480,"wires":[["b95e99847d5bbe9e"]],"icon":"font-awesome/fa-drivers-license"},{"id":"36807c352f3a8b7c","type":"comment","z":"80225fdace93e1c1","name":"Do whatever u want","info":"","x":170,"y":400,"wires":[]},{"id":"be4da956bd80938c","type":"comment","z":"80225fdace93e1c1","name":"docs v1.0","info":"https://streamlabs.readme.io/v1.0/docs/alerts\nor \nhttps://dev.streamlabs.com/reference/alerts","x":340,"y":400,"wires":[]}]