A robust Node.js client library for interacting with the WireGuard API, designed for WG-Easy servers
Powerful tools for managing your WireGuard VPN
Handles retries on 401 errors automatically
Complete implementation of WG-Easy endpoints
Supports both password and cookie-based auth
Detailed error responses for easy debugging
Comprehensive type definitions included
Get started with the WireGuard API Client
npm install wg-easy-api
node-fetch
(^3.3.2) - Included as a dependencyLearn how to use the WireGuard API Client
const WireGuardAPI = require('wg-easy-api');
async function example() {
const api = new WireGuardAPI('https', 'example.com', 51821, 'your-password');
try {
const auth = await api.initSession({ password: 'your-password' });
console.log(auth);
const clients = await api.getClients();
console.log('Clients:', JSON.stringify(clients, null, 2));
} catch (error) {
console.error('Error:', JSON.parse(error.message));
}
}
example();
try {
const clients = await api.getClients();
} catch (error) {
const err = JSON.parse(error.message);
console.error(err.error, err.statusCode);
}
Complete documentation for all methods
{ status: 'success', data: any }
{ status: 'error', error: string, statusCode: number, details: any }
Logs into the WG-Easy server with a password. Updates cookies automatically.
Parameter | Type | Required | Description |
---|---|---|---|
password | string | Yes | WG-Easy password |
await api.initSession({ password: 'myPass' }); // { status: 'success', data: { success: true } }
Checks current session status.
await api.getSession(); // { status: 'success', data: { authenticated: true } }
Creates a new session without auto-updating cookies.
Parameter | Type | Required | Description |
---|---|---|---|
password | string | Yes | WG-Easy password |
await api.createSession({ password: 'myPass' });
Logs out by deleting the session.
await api.deleteSession(); // { status: 'success', data: null }
Lists all WireGuard clients, converting timestamps to Date objects.
await api.getClients(); // { status: 'success', data: [{ id: 'abc', name: 'Client1' }] }
Adds a new client with the given name.
Parameter | Type | Required | Description |
---|---|---|---|
name | string | Yes | Client name |
await api.createClient({ name: 'MyLaptop' });
Removes a client by ID.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
await api.deleteClient({ clientId: 'abc' });
Enables a client to connect.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
await api.enableClient({ clientId: 'abc' });
Disables a client.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
await api.disableClient({ clientId: 'abc' });
Changes a client’s name.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
name | string | Yes | New name |
await api.updateClientName({ clientId: 'abc', name: 'NewName' });
Updates a client’s IP address.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
address | string | Yes | New IP address |
await api.updateClientAddress({ clientId: 'abc', address: '10.0.0.3' });
Gets a client’s .conf file as text.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
await api.getClientConfig({ clientId: 'abc' }); // { status: 'success', data: '[Interface]...' }
Gets a client’s config as an SVG QR code.
Parameter | Type | Required | Description |
---|---|---|---|
clientId | string | Yes | Client ID |
await api.getClientQRCode({ clientId: 'abc' }); // { status: 'success', data: '
Restores a server config from a string.
Parameter | Type | Required | Description |
---|---|---|---|
file | string | Yes | Configuration file content |
await api.restoreConfiguration('[Interface]...');
Gets the WG-Easy version.
await api.getRelease(); // { status: 'success', data: { version: '7' } }
Gets the UI language.
await api.getLang(); // { status: 'success', data: { lang: 'en' } }
Gets traffic stats.
await api.getUITrafficStats(); // { status: 'success', data: { bytesSent: 12345 } }
Gets the UI chart type.
await api.getChartType(); // { status: 'success', data: { type: 'line' } }
protocol
, ip
, port
in constructor.api.cookies
after initSession
.curl
to verify server responses.