In serverless environments (like AWS Lambda or Google Cloud Functions), you must instantiate the ewelink class on every request because the execution context is ephemeral.
To avoid performing a full login (email/password) on every single request, follow this pattern:
- First Request: Use
email, password, and region to call getCredentials(). This returns an access token (at), an apikey, and the region. - Subsequent Requests: Store the
at and region in a persistent store (like a database or cache). For future requests, instantiate ewelink using only the at and region parameters to bypass the login step.
/* first request: get access token and api key */
(async () => {
const connection = new ewelink({
email: '<your ewelink email>',
password: '<your ewelink password>',
region: '<your ewelink region>',
});
const credentials = await connection.getCredentials();
const accessToken = credentials.at;
const apiKey = credentials.user.apikey;
const region = credentials.region;
})();
/* subsequent request: use access token */
(async () => {
const newConnection = new ewelink({
at: accessToken,
region: region
});
const devices = await newConnection.getDevices();
})();