licers
License-key verification for Rust apps, against Licers.
Every answer from the license server is Ed25519-signed with your account key and checked here (ed25519-dalek, strict verification), with a fresh random nonce per call — so a cracked "fake license server", an edited response or a replayed one is refused.
cargo add licers
Your public key and product id are on the dashboard’s Integration page. The public key is safe to ship inside your app. Each example below is a complete file.
Check a key
Run this when your app starts. The first check binds the device to the key; past the key’s device limit a new device is refused.
In the dashboard: create a product, then a license key for it.
use licers::LicenseClient;
fn main() -> Result<(), licers::Error> {
let client = LicenseClient::builder("YOUR_PUBLIC_KEY") .product_id("YOUR_PRODUCT_ID") .app_version("2.4.1") .build()?;
let key = "XXXX-XXXX-XXXX-XXXX";
let result = match client.validate(key) {
Ok(result) => result,
Err(e) if e.is_network() => {
eprintln!("Could not reach the license server. Please try again.");
std::process::exit(1);
}
Err(e) => {
eprintln!("License check failed: {e}");
std::process::exit(1);
}
};
if !result.valid {
eprintln!("Not licensed: {}", result.error.unwrap_or_default());
std::process::exit(1);
}
println!("Licensed. Expires: {}", result.expires_at.as_deref().unwrap_or("never"));
Ok(())
}
- A refused key is a normal answer with a message. An unreachable server is an error: never treat it as “unlicensed”.
validate returns Ok with valid == false when the server refuses the key, and Err for a forged answer or an unreachable server. err.is_network() tells them apart. The API is blocking and needs no async runtime; from async code, call it in spawn_blocking.
Entitlements
Switch features on per key: true/false, numbers or text. They come from the signed answer.
In the dashboard: set Entitlements (JSON) on the key or on its plan, for example {"pro": true, "seats": 5}.
use licers::LicenseClient;
fn main() -> Result<(), licers::Error> {
let client = LicenseClient::builder("YOUR_PUBLIC_KEY") .product_id("YOUR_PRODUCT_ID") .app_version("2.4.1") .build()?;
let key = "XXXX-XXXX-XXXX-XXXX";
let result = client.validate(key)?;
if !result.valid {
eprintln!("Not licensed: {}", result.error.as_deref().unwrap_or_default());
std::process::exit(1);
}
if result.feature("pro").and_then(|v| v.as_bool()) == Some(true) {
println!("Pro features unlocked");
}
let seats = result.feature("seats").and_then(|v| v.as_i64()).unwrap_or(1);
let tier = result.feature("tier").and_then(|v| v.as_str()).unwrap_or("basic");
println!("Seats: {seats} Tier: {tier} All: {:?}", result.features);
Ok(())
}
- Compare with true exactly. The text "false" is not the same as false.
Offline file
Check a signed .lic file with no internet at all, for air-gapped machines. Signature, product, expiry, device and operating system are all checked on the computer.
- Your app shows its device id, and the customer sends it to you.
- License keys → ⋯ → Offline license file: paste the device id, set how many days it lasts, download the .lic file.
- Send it to the customer. Your app reads it from disk.
use licers::{get_hwid, verify_offline_license, Error};
fn main() {
println!("Device id: {}", get_hwid());
let lic = match verify_offline_license(
"YOUR_PUBLIC_KEY",
"license.lic",
None, Some("YOUR_PRODUCT_ID"), ) {
Ok(lic) => lic,
Err(Error::Signature(_)) => {
eprintln!("The license file is missing, damaged or was edited.");
std::process::exit(1);
}
Err(e) => {
eprintln!("License file refused: {e}"); std::process::exit(1);
}
};
let customer = lic.get("customer").and_then(|v| v.as_str()).unwrap_or("-");
match lic.get("expires_at").and_then(|v| v.as_i64()) {
Some(t) => println!("Licensed to: {customer} Expires (unix time): {t}"),
None => println!("Licensed to: {customer} Expires: never"),
}
if lic.get("features").and_then(|f| f.get("pro")).and_then(|v| v.as_bool()) == Some(true) {
println!("Pro features unlocked");
}
}
- Revoking a key can’t reach a file you already sent, so give files a short life.
- The expiry uses the computer’s own clock.
- A blank device id makes a file that works on any computer.
Usage credits
Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.
In the dashboard: give the key a credit limit, and a daily or monthly refill if you want one.
use licers::{Error, LicenseClient};
fn main() -> Result<(), licers::Error> {
let client = LicenseClient::builder("YOUR_PUBLIC_KEY") .product_id("YOUR_PRODUCT_ID") .app_version("2.4.1") .build()?;
let key = "XXXX-XXXX-XXXX-XXXX";
let result = client.validate(key)?; if !result.valid {
eprintln!("Not licensed: {}", result.error.unwrap_or_default());
std::process::exit(1);
}
println!("Left: {:?}", client.get_usage(key)?.remaining());
match client.record_usage(key, 1) {
Ok(usage) => println!("Done. {:?} credits left", usage.remaining()),
Err(Error::UsageExceeded { usage, .. }) => {
println!("Out of credits. Refill: {}", usage["resets_at"].as_str().unwrap_or("buy more"));
}
Err(e) => return Err(e),
}
Ok(())
}
- Check the key on this device first.
- After a network error the credits may already be spent: read the balance before trying again.
Remote variables
Values you change in the dashboard without shipping an update: a link, a message, a switch.
In the dashboard: add them on the Remote Variables page.
use licers::LicenseClient;
fn main() -> Result<(), licers::Error> {
let client = LicenseClient::builder("YOUR_PUBLIC_KEY") .product_id("YOUR_PRODUCT_ID") .app_version("2.4.1") .build()?;
let key = "XXXX-XXXX-XXXX-XXXX";
let result = client.validate(key)?; if !result.valid {
eprintln!("Not licensed: {}", result.error.unwrap_or_default());
std::process::exit(1);
}
let url = client.get_variable(key, "download_url")?; let theme = client.get_variable(key, "theme")?.unwrap_or_else(|| "dark".to_string());
let everything = client.get_variables(key)?;
println!("{url:?} {theme} {everything:?}");
Ok(())
}
- Check the key on this device first.
- Values are text. A product’s value wins over one for all products.
Floating seats
Let only N people run the app at the same time, on any computers.
In the dashboard: set Max concurrent sessions on the key.
use licers::{Error, FloatingSession, LicenseClient};
use std::sync::Arc;
fn main() -> Result<(), licers::Error> {
let client = LicenseClient::builder("YOUR_PUBLIC_KEY") .product_id("YOUR_PRODUCT_ID") .app_version("2.4.1") .build()?;
let key = "XXXX-XXXX-XXXX-XXXX";
let on_lost = Box::new(|reason: &str| eprintln!("Seat lost: {reason}")); match FloatingSession::start(Arc::new(client), key, Some(on_lost)) {
Ok(seat) => {
println!("Seat taken: {} of {}", seat.seats_used, seat.seats_max);
seat.ensure_active()?;
} Err(Error::SeatUnavailable(message)) => eprintln!("No seat: {message}"),
Err(e) => return Err(e),
}
Ok(())
}
- If the server can’t be reached, the seat is kept until its last signed expiry and then given up.
- A crashed app frees its seat by itself after the session timeout.
Updates
Tell the customer about a new version. Only a valid, active key is given the download link.
In the dashboard: add the version and its download link on the Releases page.
use licers::LicenseClient;
fn main() -> Result<(), licers::Error> {
let client = LicenseClient::builder("YOUR_PUBLIC_KEY") .product_id("YOUR_PRODUCT_ID") .app_version("2.4.1") .build()?;
let key = "XXXX-XXXX-XXXX-XXXX";
let update = client.check_update(key, "2.4.1", "stable")?;
if update.update_available {
println!("New version: {}", update.version.unwrap_or_default());
println!("What's new: {}", update.notes.unwrap_or_default());
println!("Download: {}", update.download_url.unwrap_or_default());
} else {
println!("You have the latest version.");
}
Ok(())
}
- The answer is signed, including “no update”. The download link is not: check its host and the file’s hash.
Errors
| Error |
Meaning |
Error::Network |
Server unreachable: retry, or apply a grace period |
Error::Server |
The server answered 5xx: an outage, not a verdict on the key |
Error::Signature |
Signature or nonce check failed: forged, edited or replayed (or a damaged license file) |
Error::ProductMismatch |
Correctly signed, but for another of your products |
Error::InvalidLicense |
The server (or an offline file) refused the key |
Error::UsageExceeded |
Not enough credits; nothing was spent |
Error::SeatUnavailable |
No free floating seat, or a held seat was lost |
Error::Protocol |
Not a usable reply (a proxy or captive portal; redirects are never followed) |
err.is_network() covers the first two, err.is_refusal() the refusals.
Device id
get_hwid() is SHA-256 of "licers:" + the OS machine id (Windows MachineGuid, Linux /etc/machine-id, macOS IOPlatformUUID) — the same value the Node and .NET SDKs produce on that machine. Where none is readable, a random id is stored once in the user's data folder (LICERS_HWID_DIR overrides). Use .hwid("...") on the builder to supply your own.
License
MIT