{"_id":"55a670a5aaf9cf1900115069","__v":25,"project":"55a47338e75ca60d00a3932f","category":{"_id":"55a6703b80c8a30d00b32685","version":"55a47338e75ca60d00a39332","project":"55a47338e75ca60d00a3932f","__v":6,"pages":["55a670a5aaf9cf1900115069","55a67fefaaf9cf19001150ce","55a6804aaaf9cf19001150d1","55a6818480c8a30d00b326d7","55a681e5aaf9cf19001150d9","55a682ae51457325000e4de0"],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-15T14:37:47.055Z","from_sync":false,"order":2,"slug":"javascript-api","title":"Javascript API"},"user":"55a4f13d02b09d1900d00a9f","version":{"_id":"55a47338e75ca60d00a39332","project":"55a47338e75ca60d00a3932f","__v":8,"createdAt":"2015-07-14T02:26:00.724Z","releaseDate":"2015-07-14T02:26:00.724Z","categories":["55a47339e75ca60d00a39333","55a4fb7e1a5f991700a940f0","55a6702b80c8a30d00b32684","55a6703b80c8a30d00b32685","55a6ea5516d20b1900ed8f53","55b23ad2329f363900fbc92f","55b23b0a329f363900fbc932","55b2426a0e11590d008f7950"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-07-15T14:39:33.181Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"If you haven't read [Getting Started](/docs/getting-started), we'd suggest reading that first.\n\nWant to control your Lono with Javascript? The easiest way is probably with our Lono module on npm!\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// In Node:\\n// npm install lono\\nLonoClient = require(\\\"lono\\\").LonoClient\\n\\n// Client-side javascript:\\n// download it from https://github.com/lono-devices/lono-api-js/blob/master/dist/index.js\\nLonoClient\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nSingle User Application Example\n---\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"lc = new LonoClient({\\n\\n  // oauth client id, secret, and scope\\n  client_id: \\\"client id\\\",\\n  client_secret: \\\"client secret\\\",\\n  scope: [\\\"write\\\"],\\n\\n  // Specify the auth token here. This is shown on the Dev Settings page\\n  // right under client secret for a single-user application.\\n  auth_token: \\\"auth token\\\",\\n\\n  on_token: function(token) {\\n    console.log(token, \\\"(token) has been emitted!\\\");\\n    \\n    device = lc.get_device(\\\"device id\\\")\\n    console.log(\\\"Turn on zone:\\\", device.set_zone(0, true))\\n  }\\n\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nOn start, you'll see that your token has been emitted (`on_token` will fire).\n\nThen, start issuing requests!\n\nMulti User Application Example\n---\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"lc = new LonoClient({\\n\\n  // oauth client id, secret, and scope\\n  client_id: \\\"Client ID\\\",\\n  client_secret: \\\"Client Secret\\\",\\n  scope: [\\\"write\\\"],\\n\\n  // this is the url the app will redirect to on a\\n  // successful login.\\n  redirect_on_success: \\\"http://127.0.0.1:3001\\\",\\n\\n  // oauth2 callback address. This is where access token requests will\\n  // be sent. If you also pass in an express app, the route will be created\\n  // for you. Otherwise, call lc.callback(req, res) when a callback\\n  // hits your app. Take note that for the oauth callback to work your app has\\n  // to be publically accessible on the internet.\\n  callback_url: \\\"/callback\\\",\\n  express_app: app,\\n\\n  // though these callback's aren't required, they are useful\\n  // for diagnostic purposes.\\n  on_auth_code: function(authCode) {\\n    console.log(authCode, \\\"(auth code) has been emitted!\\\");\\n  },\\n\\n  on_token: function(token) {\\n    console.log(token, \\\"(token) has been emitted!\\\");\\n    \\n    device = lc.get_device(\\\"device id\\\")\\n    console.log(\\\"Turn on zone:\\\", device.set_zone(0, true))\\n  }\\n\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nAdditionally, pass `lc.authorize` as a route to your initial \"authorization\"\npage.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.get(\\\"/auth\\\", lc.authorize);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n(If you aren't using express, just pass `req` and `res`: `lc.authorize(req, res);`)\n\nAnd then, direct your user to that route (in our case, /auth). If you've set\neverything up correctly, the logged-in user will be prompted to allow access to\nyour app. If they click yes, both `on_auth_code` and `on_token` will fire with their\nrespective tokens. Then, you can start issuing requests!","excerpt":"","slug":"getting-started-js","type":"basic","title":"Getting Started in Javascript"}

Getting Started in Javascript


If you haven't read [Getting Started](/docs/getting-started), we'd suggest reading that first. Want to control your Lono with Javascript? The easiest way is probably with our Lono module on npm! [block:code] { "codes": [ { "code": "// In Node:\n// npm install lono\nLonoClient = require(\"lono\").LonoClient\n\n// Client-side javascript:\n// download it from https://github.com/lono-devices/lono-api-js/blob/master/dist/index.js\nLonoClient", "language": "javascript" } ] } [/block] Single User Application Example --- [block:code] { "codes": [ { "code": "lc = new LonoClient({\n\n // oauth client id, secret, and scope\n client_id: \"client id\",\n client_secret: \"client secret\",\n scope: [\"write\"],\n\n // Specify the auth token here. This is shown on the Dev Settings page\n // right under client secret for a single-user application.\n auth_token: \"auth token\",\n\n on_token: function(token) {\n console.log(token, \"(token) has been emitted!\");\n \n device = lc.get_device(\"device id\")\n console.log(\"Turn on zone:\", device.set_zone(0, true))\n }\n\n});", "language": "javascript" } ] } [/block] On start, you'll see that your token has been emitted (`on_token` will fire). Then, start issuing requests! Multi User Application Example --- [block:code] { "codes": [ { "code": "lc = new LonoClient({\n\n // oauth client id, secret, and scope\n client_id: \"Client ID\",\n client_secret: \"Client Secret\",\n scope: [\"write\"],\n\n // this is the url the app will redirect to on a\n // successful login.\n redirect_on_success: \"http://127.0.0.1:3001\",\n\n // oauth2 callback address. This is where access token requests will\n // be sent. If you also pass in an express app, the route will be created\n // for you. Otherwise, call lc.callback(req, res) when a callback\n // hits your app. Take note that for the oauth callback to work your app has\n // to be publically accessible on the internet.\n callback_url: \"/callback\",\n express_app: app,\n\n // though these callback's aren't required, they are useful\n // for diagnostic purposes.\n on_auth_code: function(authCode) {\n console.log(authCode, \"(auth code) has been emitted!\");\n },\n\n on_token: function(token) {\n console.log(token, \"(token) has been emitted!\");\n \n device = lc.get_device(\"device id\")\n console.log(\"Turn on zone:\", device.set_zone(0, true))\n }\n\n});", "language": "javascript" } ] } [/block] Additionally, pass `lc.authorize` as a route to your initial "authorization" page. [block:code] { "codes": [ { "code": "app.get(\"/auth\", lc.authorize);", "language": "javascript" } ] } [/block] (If you aren't using express, just pass `req` and `res`: `lc.authorize(req, res);`) And then, direct your user to that route (in our case, /auth). If you've set everything up correctly, the logged-in user will be prompted to allow access to your app. If they click yes, both `on_auth_code` and `on_token` will fire with their respective tokens. Then, you can start issuing requests!