C, C++ and other languages
The SDK for Windows, macOS and Linux is a C library with seven functions. Call it directly from C or C++, or from any language that can call a C library. Before you start, get your account approved and create an SDK key (steps 1 and 2 of the overview).
| 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.
The Windows download also has idlepay.def, for linking with MSVC (step 2).
The functions
| Function | What it does |
|---|---|
char *IdlePayStartKey(const char *key, const char *data_dir, int consent) | Starts sharing in the background: your ipsk_ key, a private folder of your app, and non-zero once the user agreed. Returns NULL, or an error message. |
char *IdlePayStart(const char *options) | The same, with every option as JSON (below). Only needed for the optional ones. |
void IdlePayStop(void) | Stops sharing. Waits a few seconds at most. |
void IdlePaySetPaused(int paused) | Pauses (non-zero) or resumes sharing. |
void IdlePayReconnect(void) | Reconnects now. Optional: call it when the network changed. |
char *IdlePayStatus(void) | The status as JSON: running, state, message, public_ip, device_id. |
void IdlePayFree(char *s) | Frees a string returned by IdlePayStartKey, IdlePayStart or IdlePayStatus. Call it on every one. |
Strings are UTF-8. The JSON options of IdlePayStart are key, data_dir and consent (true or false), plus the optional max_daily_mb, name and device_id. See Options. The functions can be called from any thread, and the library runs on its own threads.
1. Call it
#include <stdio.h>
#include "idlepay.h"
/* Call once the user has agreed to share (see the consent guide). */
int start_sharing(void) {
char *err = IdlePayStartKey("ipsk_...", "/home/me/.local/share/myapp/idlepay", 1);
if (err) {
fprintf(stderr, "IdlePay: %s\n", err);
IdlePayFree(err);
return -1;
}
return 0;
}
void print_status(void) {
char *s = IdlePayStatus();
printf("%s\n", s); /* {"running":true,"state":"online",...} */
IdlePayFree(s);
}
/* When your app exits: */
/* IdlePayStop(); */
The same code works in C++. For an optional setting, pass JSON to IdlePayStart instead. In C++ a raw string keeps it readable; if the JSON holds a Windows path, build it with a JSON library so the backslashes are escaped.
char *err = IdlePayStart(R"({"key": "ipsk_...", "data_dir": "/home/me/.local/share/myapp/idlepay", "consent": true, "max_daily_mb": 500})");
Ask the user before calling IdlePayStartKey, save their answer, and start it on later launches only if they agreed: see Asking for consent.
2. Build and link
Linux (ship libidlepay.so next to your executable):
gcc app.c -o app -L. -lidlepay -Wl,-rpath,'$ORIGIN'
macOS (next to the executable, or in Contents/Frameworks of an app bundle with -Wl,-rpath,@executable_path/../Frameworks):
clang app.c -o app -L. -lidlepay -Wl,-rpath,@executable_path
Windows with MinGW links against the DLL directly:
gcc app.c -o app.exe -L. -lidlepay
Windows with MSVC needs an import library. Make it once from idlepay.def in a Developer Command Prompt, then link with it:
lib /def:idlepay.def /machine:x64 /out:idlepay.lib cl app.c idlepay.lib
Ship idlepay.dll next to app.exe. Build for x64 only. With CMake, add idlepay.lib (or the .so / .dylib) as an imported library and copy the shared library to the output folder.
3. Check that it works
On a computer on a home connection, state in the status JSON 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.
4. Ship it
- macOS: sign
libidlepay.dylibwith your Developer ID along with your app, before notarizing. A library downloaded with a browser is quarantined: while testing, clear it withxattr -d com.apple.quarantine libidlepay.dylib. - Windows: if you sign your app, you can sign
idlepay.dllwith the same certificate.
Other languages
Any language that can call C functions from a shared library can use the SDK. Declare the functions from idlepay.h and follow the same rules: pass UTF-8 strings, and free every returned string with IdlePayFree.
- Rust:
extern "C"declarations and-l idlepayin a build script, or thelibloadingcrate. - Go: cgo with
#cgo LDFLAGS: -lidlepayand#include "idlepay.h". - Java and Kotlin on desktop: JNA or the Foreign Function API.
- Dart and Flutter desktop:
dart:ffi. - Delphi, Lazarus, Swift, Ruby, PHP and others: their usual C interface.
The .NET, Node.js and Python packages are small examples of this: see IdlePay.cs, index.js and idlepay.py in the bindings download.
Updating the SDK
Replace the library and idlepay.h with the new version's and release your app. Devices keep their link to your account.