If you haven't read 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!
// In Node:
// npm install lono
LonoClient = require("lono").LonoClient
// Client-side javascript:
// download it from https://github.com/lono-devices/lono-api-js/blob/master/dist/index.js
LonoClient
Single User Application Example
lc = new LonoClient({
// oauth client id, secret, and scope
client_id: "client id",
client_secret: "client secret",
scope: ["write"],
// Specify the auth token here. This is shown on the Dev Settings page
// right under client secret for a single-user application.
auth_token: "auth token",
on_token: function(token) {
console.log(token, "(token) has been emitted!");
device = lc.get_device("device id")
console.log("Turn on zone:", device.set_zone(0, true))
}
});
On start, you'll see that your token has been emitted (on_token
will fire).
Then, start issuing requests!
Multi User Application Example
lc = new LonoClient({
// oauth client id, secret, and scope
client_id: "Client ID",
client_secret: "Client Secret",
scope: ["write"],
// this is the url the app will redirect to on a
// successful login.
redirect_on_success: "http://127.0.0.1:3001",
// oauth2 callback address. This is where access token requests will
// be sent. If you also pass in an express app, the route will be created
// for you. Otherwise, call lc.callback(req, res) when a callback
// hits your app. Take note that for the oauth callback to work your app has
// to be publically accessible on the internet.
callback_url: "/callback",
express_app: app,
// though these callback's aren't required, they are useful
// for diagnostic purposes.
on_auth_code: function(authCode) {
console.log(authCode, "(auth code) has been emitted!");
},
on_token: function(token) {
console.log(token, "(token) has been emitted!");
device = lc.get_device("device id")
console.log("Turn on zone:", device.set_zone(0, true))
}
});
Additionally, pass lc.authorize
as a route to your initial "authorization"
page.
app.get("/auth", lc.authorize);
(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!