This page shows how to connect to the Weble IoT Universal Gateway peers API from a browser or a Node.js application.
The connection always goes through POST /auth, which authenticates the client and returns a fully-initialized peers object connected over WebSocket.
The gateway serves a ready-made client at /includes/js/peers/browser. Load it and call peers.auth: it posts to POST /auth and returns a connected peer object.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"><title>Weble API</title>
<script src="https://gateway.example.com/includes/js/peers/browser"></script>
</head>
<body>
<script>
peers.auth({
host : 'https://gateway.example.com',
username : 'integration',
password : 'your-password' // or: apiKey : 'wbx_...'
}, function(err, peer){
if(err) return console.error('login failed', err)
peer('supervision').require('gateways', function(err, gateways){
gateways.getLastValue('4/7/25', function(err, value){ console.log(value) })
})
})
</script>
</body>
</html>
With apiKey, the key is sent in the X-API-Key header instead of a user name and password.
The gateway exposes a POST /auth endpoint that returns the source code of the peers client and socket.io client, plus a session identifier. The browser bootstraps it with a single call and gets back a ready-to-use peer object.
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>Loading API</title></head>
<body>
<script>
function apiLogin(host, username, password, callback){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState != 4) return;
if(xhttp.status == 200){
var data = JSON.parse(xhttp.responseText);
eval(data.connect)({ host: host }, data, callback);
} else {
callback(xhttp.status + ' ' + xhttp.responseText);
}
};
xhttp.open('POST', host + '/auth', true);
xhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhttp.send(JSON.stringify({ username: username, password: password }));
}
apiLogin('https://gateway.example.com', 'admin', 'mypassword', function(err, peer){
if(err) return console.error('login failed', err);
peer('supervision').require('gateways', function(err, gateways){
gateways.writeValue('4/7/25', 1, function(err){
if(err) console.error('KNX write failed', err)
})
})
})
</script>
</body>
</html>
Send the password as it is, over HTTPS. An MD5 hash is refused.
With an API key: instead of a user name and password, you can send an API key in the X-API-Key header of POST /auth. The key is generated in Administration › Access, on the row of the account (see REST API › API key and Basic authentication). The connection then has the rights of that account.
xhttp.setRequestHeader('X-API-Key', 'wbx_...');
xhttp.send(JSON.stringify({}));
The same apiLogin flow works from any Node.js application. Call POST /auth over HTTPS, then use the returned peer object exactly as you would in a browser.
var https = require('https')
function apiLogin(host, username, password, callback){
var body = JSON.stringify({ username: username, password: password })
var req = https.request(host + '/auth', {
method : 'POST',
headers : { 'Content-Type': 'application/json', 'Content-Length': body.length }
}, function(res){
var data = ''
res.on('data', function(c){ data += c })
res.on('end', function(){
try { data = JSON.parse(data) } catch(e){ return callback(e) }
eval(data.connect)({ host: host }, data, callback)
})
})
req.on('error', callback)
req.write(body)
req.end()
}
apiLogin('https://gateway.example.com', 'admin', 'mypassword', function(err, peer){
if(err) return console.error(err)
peer('supervision').require('system', function(err, system){
system.totalmem(function(err, bytes){
console.log('total memory:', bytes)
})
})
})
Once you have a peer object, every example in the reference section applies as-is.
The main entry points you will use are:
peer('supervision').require('moduleName', callback) — load a modulemodule.on('eventName', listener) — listen to the events of a loaded module, e.g. gateways.on('newValue 4/7/25', cb)peer('supervision').isConnected() — check the link to the gatewaySee Peers protocol for the full primitive set.
The peers client reconnects automatically if the link drops, so you do not need to handle reconnection manually. To shut down explicitly:
peer.close() // closes the connection and stops the automatic reconnection
peer.removeAllListeners() // detach any handlers you registered
For long-running clients, always remove listeners you registered with module.on(...) when you no longer need them — orphaned listeners are the most common cause of memory leaks. Use peer.countListeners() to monitor.