Skip to content

JavaScript / Node

Works in any environment with fetch — modern browsers, Node.js 18+, Deno, Bun.

class VoltaClient {
constructor(apiUrl, apiKey, layoutId) {
this.apiUrl = apiUrl.replace(/\/$/, '');
this.apiKey = apiKey;
this.layoutId = layoutId;
this.token = null;
}
async getLayoutInfo() {
const res = await fetch(`${this.apiUrl}/layout/${this.layoutId}`, {
headers: { 'x-api-key': this.apiKey },
});
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
return res.json();
}
async connect() {
const res = await fetch(`${this.apiUrl}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-api-key': this.apiKey },
body: JSON.stringify({ layoutId: this.layoutId }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
this.token = (await res.json()).token;
}
async sendAction(lid, type, value) {
const res = await fetch(`${this.apiUrl}/action`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.token}`,
},
body: JSON.stringify({ layoutId: this.layoutId, lid, type, value }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
return res.json();
}
sendButton(lid) { return this.sendAction(lid, 'Button'); }
sendSlider(lid, value) { return this.sendAction(lid, 'Slider', value); }
sendToggle(lid, value) { return this.sendAction(lid, 'Toggle', value); }
sendIndex(lid, value) { return this.sendAction(lid, 'Index', value); }
}
<button id="cheer">Cheer!</button>
<input id="energy" type="range" min="0" max="1" step="0.01" />
<script>
const client = new VoltaClient(API_URL, API_KEY, LAYOUT_ID);
(async () => {
await client.connect();
const layout = await client.getLayoutInfo();
console.log('Controls:', layout.controls);
})();
document.getElementById('cheer').onclick = () => client.sendButton(1);
document.getElementById('energy').oninput = (e) =>
client.sendSlider(2, parseFloat(e.target.value));
</script>
const client = new VoltaClient(
'https://your-api-url.execute-api.eu-west-2.amazonaws.com',
'your-api-key',
'your-layout-id'
);
await client.connect();
const layout = await client.getLayoutInfo();
for (const control of layout.controls) {
console.log(`${control.type} lid:${control.lid}${control.props?.label}`);
}
await client.sendButton(1);
await client.sendSlider(2, 0.5);