eWeLink API Client for Node.js
The ewelink-api library provides a programmatic interface for interacting with eWeLink-compatible smart home devices, such as Sonoff products, from Node.js environments. It supports both cloud-based interaction (requiring internet access) and ZeroConf (LAN mode) for local control without an internet connection. The current stable version is 3.1.1, with releases occurring on a moderate, irregular cadence, focusing on bug fixes, dependency updates, and feature enhancements like improved error handling and new device control methods. Key differentiators include its multi-environment compatibility (Node.js, browsers, serverless), robust device state management, and power consumption monitoring for compatible devices.
Common errors
-
Error: Account authentication failed. Please check your login credentials and region.
cause Incorrect email, phone number, password, or an invalid region provided during `EwelinkApi` class initialization.fixDouble-check your `email` (or `phoneNumber`), `password`, and `region` parameters. Ensure the region corresponds to your eWeLink account server (e.g., 'us', 'eu', 'as'). -
TypeError: EwelinkApi is not a constructor
cause Attempting to import `EwelinkApi` as a named export (`import { EwelinkApi } from 'ewelink-api';`) when it is exported as a default.fixChange your import statement to `import EwelinkApi from 'ewelink-api';` for ES Modules or `const EwelinkApi = require('ewelink-api');` for CommonJS. -
Error: Device not found or unreachable.
cause The provided device ID does not correspond to an existing device, or the device is offline/not registered to the authenticated account, or there's a network issue preventing connection (especially in LAN mode).fixVerify the `DEVICE_ID` is correct. Ensure the device is powered on and connected to Wi-Fi. If using LAN mode, confirm your network setup allows local communication with the device.
Warnings
- breaking Version 3.0.0 introduced significant changes to the authentication process, primarily switching to phone number and password login. If you were previously logging in with email, your authentication method will need to be updated.
- breaking The default APP_ID and APP_SECRET used by the library were updated in version 2.0.1. If you were relying on hardcoded or previous default values, these may no longer function correctly, leading to authentication failures.
- gotcha The underlying HTTP client was changed from `requests` to `node-fetch` in version 3.0.0. While this primarily impacts internal implementation, it might subtly alter error structures or network request behavior in edge cases.
- gotcha For optimal performance and real-time control, particularly for power state changes, use the `setWSDevicePowerState` method (available since v3.1.0) which leverages WebSockets. The older `setDevicePowerState` method uses HTTP, which can be slower and less responsive.
Install
-
npm install ewelink-api -
yarn add ewelink-api -
pnpm add ewelink-api
Imports
- EwelinkApi
import { EwelinkApi } from 'ewelink-api';import EwelinkApi from 'ewelink-api';
- EwelinkApi (CommonJS)
import EwelinkApi from 'ewelink-api';
const EwelinkApi = require('ewelink-api'); - Device types (TypeScript)
import { Device, DeviceStatus } from 'ewelink-api';import type { Device, DeviceStatus } from 'ewelink-api/dist/ts/interface';
Quickstart
import EwelinkApi from 'ewelink-api';
const EMAIL = process.env.EWELINK_EMAIL ?? '';
const PASSWORD = process.env.EWELINK_PASSWORD ?? '';
const REGION = process.env.EWELINK_REGION ?? 'us'; // e.g., 'us', 'eu', 'as'
const DEVICE_ID = process.env.EWELINK_DEVICE_ID ?? '';
async function controlDevice() {
if (!EMAIL || !PASSWORD || !DEVICE_ID) {
console.error('Please set EWELINK_EMAIL, EWELINK_PASSWORD, and EWELINK_DEVICE_ID environment variables.');
process.exit(1);
}
try {
const connection = new EwelinkApi({ email: EMAIL, password: PASSWORD, region: REGION });
await connection.get and set things
const devices = await connection.getDevices();
const targetDevice = devices.find(d => d.deviceid === DEVICE_ID);
if (targetDevice) {
console.log(`Found device: ${targetDevice.name} (ID: ${targetDevice.deviceid})`);
// Toggle power state using the newer WebSocket method (v3.1.0+)
console.log('Toggling device power state via WebSocket...');
const currentState = targetDevice.params.switch === 'on' ? 'off' : 'on';
await connection.setWSDevicePowerState(DEVICE_ID, currentState);
console.log(`Device state toggled to ${currentState}.`);
// Or using the standard HTTP method
// console.log('Toggling device power state via HTTP...');
// await connection.setDevicePowerState(DEVICE_ID, currentState);
// console.log(`Device state toggled to ${currentState}.`);
const status = await connection.getDevicePowerState(DEVICE_ID);
console.log(`Device new power state: ${status.state}`);
} else {
console.log(`Device with ID ${DEVICE_ID} not found.`);
}
} catch (error) {
console.error('An error occurred:', error.message);
}
}
controlDevice();