Electron and Node.js
Add the SDK to an Electron desktop app, or to a Node.js program. In Electron, the SDK runs in the main process. Before you start, get your account approved and create an SDK key (steps 1 and 2 of the overview).
| Downloads | Language bindings (for the node/ package) plus the library for each system below, from your SDK page (approved accounts only) |
| Package | @idlepay/sdk, installed from the folder (it isn't on npm). It depends on koffi, which npm installs. |
| Requires | Node.js 18 or later, or any current Electron |
| Your app runs on | Package | Library file |
|---|---|---|
| Windows (64-bit) | windows-amd64 | idlepay.dll |
| macOS (Apple silicon and Intel) | macos | libidlepay.dylib |
| Linux x86-64 | linux-amd64 | libidlepay.so |
| Linux ARM64 | linux-arm64 | libidlepay.so |
| Linux ARMv7 (32-bit) | linux-armv7 | libidlepay.so |
Download the package for each OS from the Downloads tab on your SDK page (approved accounts only). Every download also has idlepay.h. On Windows the library is 64-bit x86 only, so your app must run as a 64-bit x86 process. The macOS library works on both Apple silicon and Intel.
1. Add the package to your project
- Copy the
node/folder from the bindings zip into your project, for example asvendor/idlepay-sdk/. - Put the libraries in that folder. Windows, macOS and Linux x86-64 go in the folder itself. Linux ARM libraries go in subfolders,
linux-arm64/andlinux-arm/, since they have the same file name. - Install it.
--install-linkscopies the files instead of linking the folder, so packagers pick them up:
npm install --install-links ./vendor/idlepay-sdk
The folder then looks like this:
vendor/idlepay-sdk/ index.js package.json idlepay.dll Windows libidlepay.dylib macOS libidlepay.so Linux x86-64 linux-arm64/libidlepay.so linux-arm/libidlepay.so
2. Load the SDK in the main process
Packaged Electron apps keep their files in app.asar, and a native library can't be loaded from inside it. Tell the SDK where the unpacked copy is before you require it:
const path = require('path')
function idlepayLibrary() {
const dir = path.dirname(require.resolve('@idlepay/sdk'))
.replace('app.asar' + path.sep, 'app.asar.unpacked' + path.sep)
if (process.platform === 'win32') return path.join(dir, 'idlepay.dll')
if (process.platform === 'darwin') return path.join(dir, 'libidlepay.dylib')
if (process.arch === 'x64') return path.join(dir, 'libidlepay.so')
return path.join(dir, 'linux-' + process.arch, 'libidlepay.so') // linux-arm64, linux-arm
}
process.env.IDLEPAY_LIB = idlepayLibrary()
const idlepay = require('@idlepay/sdk')
While developing (not packaged) there's no app.asar, and the same code finds the files in the folder.
3. Ask the user, and remember the answer
The package has a ready-made consent dialog for Electron's main process. It shows IdlePay's standard wording with your app's name, saves the answer in the SDK's data folder, and counts closing it as "No thanks". Ask once, and start the SDK if the user agrees:
const { app } = require('electron')
const SDK_KEY = 'ipsk_...' // from the SDK page of your dashboard
const dataDir = () => path.join(app.getPath('userData'), 'idlepay')
function startSharing() {
idlepay.start({ key: SDK_KEY, dataDir: dataDir(), consent: true })
}
app.whenReady().then(async () => {
if (idlepay.consent(dataDir()) === null) { // never asked yet
await idlepay.askConsent({ appName: 'My App', dataDir: dataDir(), reward: 'you use My App without ads' })
}
if (idlepay.consent(dataDir())) startSharing()
// ... create your windows as usual
})
app.on('will-quit', () => idlepay.stop())
reward is optional: it finishes the sentence "In return, …". Pass window to attach the dialog to one of your windows.
The dialog's wording is in English. To translate it or use your own words, pass title, message and the button labels allow, decline and learnMore. Your text must still cover what the consent screen must say:
await idlepay.askConsent({
appName: 'My App', dataDir: dataDir(),
title: t('sharing.title'), message: t('sharing.message'),
allow: t('sharing.allow'), decline: t('sharing.decline'), learnMore: t('sharing.learnMore'),
})
Add a setting that turns sharing off with idlepay.setConsent(dataDir(), false), which also stops sharing, and on again with askConsent and startSharing(). Your renderer can ask the main process through IPC.
Your own consent screen instead? That's fine if it follows the consent rules. Save its answer with idlepay.setConsent(dataDir(), agreed), and use idlepay.consent the same way.
startreturns at once; the SDK runs on its own threads. It throws with the reason if it can't start, for example "already started".stopwaits a few seconds at most.- Sharing runs while your app runs. An app that stays in the tray or starts at login earns more hours.
4. Check that it works
console.log(idlepay.status())
// { running: true, state: 'online', message: '', public_ip: '203.0.113.7', device_id: 123456 }
On a computer on a home connection, state should be online within about a minute, and the device appears on your SDK page. Anything else comes with a message that says why: see the messages. An error loading the library at require means the path in IDLEPAY_LIB is wrong, or the file is for another system or CPU.
5. Pause and reconnect
idlepay.setPaused(true) // your own reason: on battery, a metered connection... idlepay.setPaused(false) idlepay.reconnect() // optional: the network changed, reconnect now
To cap traffic per device, add maxDailyMB to the start options. Sharing then pauses until midnight UTC once the device has shared that much.
6. Package the app
Keep the SDK's folder out of app.asar. With electron-builder, in package.json:
"build": {
"asarUnpack": ["node_modules/@idlepay/sdk/**"]
}
With Electron Forge, in forge.config.js, and add @electron-forge/plugin-auto-unpack-natives so koffi is unpacked too:
packagerConfig: {
asar: { unpack: '**/node_modules/@idlepay/sdk/**' },
},
plugins: [{ name: '@electron-forge/plugin-auto-unpack-natives', config: {} }],
- macOS: electron-builder and Forge sign every binary in the app, the library included, when you sign the app. A library downloaded with a browser is quarantined: while testing, clear it with
xattr -d com.apple.quarantine libidlepay.dylib. - Windows: only 64-bit x86 builds of your app can load the library, not ARM64 builds.
Plain Node.js
Outside Electron there's no app.asar: install the folder the same way, then require('@idlepay/sdk'). On Linux ARM, still set IDLEPAY_LIB to the library in the linux-arm64/ or linux-arm/ subfolder first (the idlepayLibrary() function above does this). Use a data folder your program owns, for example path.join(os.homedir(), '.myapp', 'idlepay'), and call idlepay.stop() before the process exits. Your program still has to ask its user for consent first; the ready-made dialog needs Electron, so save the answer with idlepay.setConsent.
Updating the SDK
Replace the files in vendor/idlepay-sdk/ with the new version's, run the install command again and release your app. Devices keep their link to your account.