Bryan Mayland
TVWBB Hall of Fame
Spent today getting together the framework of an actual REST API for interacting with LinkMeter from external applications. Documentation is filling out as I add functions on the wiki Accessing Raw Data Remotely.
The gist is that there is now a collection of URIs /cgi-bin/luci/lm/api/* which are designed for better interaction with 3rd party apps, specifically when it comes to being able to change parameters and do all the things the webui does when a user is logged in. Most important is the change from a username / password and sysauth and cookies and tokens and all that authentication to an API key-based authorization parameter. LinkMeter generates an API key on install and adding that to a POST request to any of the API functions will allow changes to be made. Quick example:
Right now all I have is current status, configuration, and version. I plan on adding archive features (list, stash, restore, reset current database), reboot avr, alarm test, possibly outputting history data in JSON? And other wizardry I do behind the scenes that maybe someone wants exposed to use in their app as well, just ask me about it! The old API is still there, but I believe people are having problems with doing SET commands on v14 snapshots and it would be pretty hacky for me to fix what is going wrong in a backward compatible way so this is the future.
Application developers are encouraged to query /cgi-bin/luci/lm/api/version to determine support. 404 Not found response? Old v13 and below API. 403 Forbidden response? User has disabled the external API. The API version number is returned in a JSON object on a 200 (success) return code. Other API requests return 403 if the supplied API key isn't valid or 405 Method Not Allowed if attempting to do a write operation and you did not use a POST HTTP method or 401 if supplied arguments are invalid.
The gist is that there is now a collection of URIs /cgi-bin/luci/lm/api/* which are designed for better interaction with 3rd party apps, specifically when it comes to being able to change parameters and do all the things the webui does when a user is logged in. Most important is the change from a username / password and sysauth and cookies and tokens and all that authentication to an API key-based authorization parameter. LinkMeter generates an API key on install and adding that to a POST request to any of the API functions will allow changes to be made. Quick example:
Code:
curl --data "sp=250&apikey=XXXX" http://heatermeter.local/cgi-bin/luci/lm/set
or
curl --data "sp=250" http://heatermeter.local/cgi-bin/luci/lm/set?apikey=XXXX
or
curl --data "value=250" http://heatermeter.local/cgi-bin/luci/lm/set/sp?apikey=XXXX
Right now all I have is current status, configuration, and version. I plan on adding archive features (list, stash, restore, reset current database), reboot avr, alarm test, possibly outputting history data in JSON? And other wizardry I do behind the scenes that maybe someone wants exposed to use in their app as well, just ask me about it! The old API is still there, but I believe people are having problems with doing SET commands on v14 snapshots and it would be pretty hacky for me to fix what is going wrong in a backward compatible way so this is the future.
Application developers are encouraged to query /cgi-bin/luci/lm/api/version to determine support. 404 Not found response? Old v13 and below API. 403 Forbidden response? User has disabled the external API. The API version number is returned in a JSON object on a 200 (success) return code. Other API requests return 403 if the supplied API key isn't valid or 405 Method Not Allowed if attempting to do a write operation and you did not use a POST HTTP method or 401 if supplied arguments are invalid.