HowTo: Using the JSON-RPC API
LuCI provides some of its libraries to external applications through a JSON-RPC API. This Howto shows how to use it and provides information about available functions.
LuCI comes with an efficient JSON De-/Encoder together with a JSON-RPC-Server which implements the JSON-RPC 1.0 and 2.0 (partly) specifications. The LuCI JSON-RPC server offers several independent APIs. Therefore you have to use different URLs for every exported library. Assuming your LuCI-Installation can be reached through /cgi-bin/luci any exported library can be reached via /cgi-bin/luci/rpc/LIBRARY.
Most exported libraries will require a valid authentication to be called with. If you get an HTTP 403 Forbidden status code you are probably missing a valid authentication token. To get such a token you have to call the function login of the RPC-Library auth. Following our example from above this login function would be provided at /cgi-bin/luci/rpc/auth. The function accepts 2 parameters: username and password (of a valid user account on the host system) and returns an authentication token.
If you want to call any exported library which requires an authentication token you have to append it as an URL parameter auth to the RPC-Server URL. So instead of calling /cgi-bin/luci/rpc/LIBRARY you have to call /cgi-bin/luci/rpc/LIBRARY?auth=TOKEN.
If your JSON-RPC client is Cookie-aware (like most browsers are) you will receive the authentication token also with a session cookie and probably don't have to append it to the RPC-Server URL.
The UCI-Library /rpc/uci offers functionality to interact with the Universal Configuration Interface.
- (string) add(config, type)
- (integer) apply(config)
- (object) changes([config)]
- (boolean) commit(config)
- (boolean) delete(config, section[, option)]
- (boolean) delete_all(config[, type)]
- (array) foreach(config[, type)]
- (mixed) get(config, section[, option)]
- (object) get_all(config[, section)]
- (mixed) get_state(config, section[, option)]
- (boolean) revert(config)
- (name) section(config, type, name, values)
- (boolean) set(config, section, option, value)
- (boolean) tset(config, section, values)
The Filesystem library /rpc/fs offers functionality to interact with the filesystem on the host machine.
Note: All functions are exported as they are except for readfile which encodes its return value in base64 and writefile which only accepts base64 encoded data as second argument. Note that both functions will only be available when the luasocket packet is installed on the hostsystem.
The System library /rpc/sys offers functionality to interact with the operating system on the host machine.
- Complete luci.sys library
- Complete luci.sys.group library with prefix group.
- Complete luci.sys.net library with prefix net.
- Complete luci.sys.process library with prefix process.
- Complete luci.sys.user library with prefix user.
- Complete luci.sys.wifi library with prefix wifi.
The IPKG library /rpc/ipkg offers functionality to interact with the package manager (IPKG or OPKG) on the host machine.