Add license keys to a Rust app
To add license keys to a Rust program, run cargo add licers, build a LicenseClient with your public key, and call client.validate(key)?. It is blocking, needs no async runtime, and verifies each answer with ed25519-dalek in strict mode.
Updated by the Licers team
Set it up
- Create a free account and add a product.
- Copy your public key and product id from the Integration page. The public key is safe to ship inside your app.
- Issue a key: set how many devices it may use, when it expires, and any entitlements.
- Add the code below. On the first check the device is bound to the key; past the device limit a new machine is refused.
cargo add licersuse licers::LicenseClient; fn main() -> Result<(), licers::Error> { let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products .app_version("2.4.1") // shows in Analytics -> App versions .build()?; let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key let result = match client.validate(key) { Ok(result) => result, Err(e) if e.is_network() => { // Offline, or the server is down. That is not "unlicensed": try again, // or allow a few days since the last good check. eprintln!("Could not reach the license server. Please try again."); std::process::exit(1); } Err(e) => { // A forged or replayed answer, or a key for another of your products. eprintln!("License check failed: {e}"); std::process::exit(1); } }; if !result.valid { // Expired, revoked, device limit reached... the server says which. eprintln!("Not licensed: {}", result.error.unwrap_or_default()); std::process::exit(1); } println!("Licensed. Expires: {}", result.expires_at.as_deref().unwrap_or("never")); Ok(())}Errors you can match on
validate returns Ok with valid == false when the server refuses the key. Err(Error::Signature(_)) means a forged or replayed answer; Error::Network and Error::Server mean there was no answer. From async code, call it inside spawn_blocking.
| What happened | What your app should do |
|---|---|
| The key is expired, revoked, blocked or over its device limit | Show the message the server sent. You can reword every one of them on the Custom Messages page. |
| The answer has a bad signature or an old nonce | Refuse. Somebody is faking the server. |
| The server cannot be reached, or returns a 5xx error | Retry, or allow a grace period since the last good check. Being offline is not the same as being unlicensed. |
Seats, credits, variables and offline files
A FloatingSession heartbeats on a background thread and frees its seat when it is dropped.
Entitlements. Switch features on per key: true/false, numbers or text. They come from the signed answer.
use licers::LicenseClient; fn main() -> Result<(), licers::Error> { let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products .app_version("2.4.1") // shows in Analytics -> App versions .build()?; let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key let result = client.validate(key)?; if !result.valid { eprintln!("Not licensed: {}", result.error.as_deref().unwrap_or_default()); std::process::exit(1); } // Entitlements of this key, for example {"pro": true, "seats": 5, "tier": "gold"}. // They are read from the signed copy, so nobody can edit them on the way. 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(())}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.
use licers::{get_hwid, verify_offline_license, Error}; fn main() { // 1. Show this to the customer. They send it to you, and you create the file // for that device: License keys -> ... -> Offline license file. println!("Device id: {}", get_hwid()); // 2. The customer saves the file next to your app. No network is used here. let lic = match verify_offline_license( "YOUR_PUBLIC_KEY", "license.lic", None, // this device Some("YOUR_PRODUCT_ID"), // your product ) { 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}"); // expired, other device, OS or product std::process::exit(1); } }; // 3. Use it. An invalid license is always an Err. 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"); }}Usage credits. Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.
use licers::{Error, LicenseClient}; fn main() -> Result<(), licers::Error> { let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products .app_version("2.4.1") // shows in Analytics -> App versions .build()?; let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key let result = client.validate(key)?; // first: this activates the device on the key if !result.valid { eprintln!("Not licensed: {}", result.error.unwrap_or_default()); std::process::exit(1); } println!("Left: {:?}", client.get_usage(key)?.remaining()); // reads only; None = no limit match client.record_usage(key, 1) { // spent 1 credit, all or nothing 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(())}Remote variables. Values you change in the dashboard without shipping an update: a link, a message, a switch.
use licers::LicenseClient; fn main() -> Result<(), licers::Error> { let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products .app_version("2.4.1") // shows in Analytics -> App versions .build()?; let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key let result = client.validate(key)?; // first: this activates the device on the key if !result.valid { eprintln!("Not licensed: {}", result.error.unwrap_or_default()); std::process::exit(1); } // Values you set on the Remote Variables page. Signed, and only for valid keys. let url = client.get_variable(key, "download_url")?; // None when it isn't set let theme = client.get_variable(key, "theme")?.unwrap_or_else(|| "dark".to_string()); let everything = client.get_variables(key)?; // all this key may see println!("{url:?} {theme} {everything:?}"); Ok(())}Floating seats. Let only N people run the app at the same time, on any computers.
use licers::{Error, FloatingSession, LicenseClient};use std::sync::Arc; fn main() -> Result<(), licers::Error> { let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products .app_version("2.4.1") // shows in Analytics -> App versions .build()?; let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key // Takes a seat and keeps it with heartbeats on a background thread. let on_lost = Box::new(|reason: &str| eprintln!("Seat lost: {reason}")); // save the user's work match FloatingSession::start(Arc::new(client), key, Some(on_lost)) { Ok(seat) => { println!("Seat taken: {} of {}", seat.seats_used, seat.seats_max); // ... your app runs here. Call this at safe points (before a save, // between jobs). It is an Err once the seat is gone. seat.ensure_active()?; } // dropping it frees the seat Err(Error::SeatUnavailable(message)) => eprintln!("No seat: {message}"), Err(e) => return Err(e), } Ok(())}Updates. Tell the customer about a new version. Only a valid, active key is given the download link.
use licers::LicenseClient; fn main() -> Result<(), licers::Error> { let client = LicenseClient::builder("YOUR_PUBLIC_KEY") // safe to ship inside your app .product_id("YOUR_PRODUCT_ID") // your product: refuses keys for your other products .app_version("2.4.1") // shows in Analytics -> App versions .build()?; let key = "XXXX-XXXX-XXXX-XXXX"; // the customer's license key let update = client.check_update(key, "2.4.1", "stable")?; // or "beta" if update.update_available { println!("New version: {}", update.version.unwrap_or_default()); println!("What's new: {}", update.notes.unwrap_or_default()); // The answer is signed, but download_url and notes are not: check the URL is // on your own host, and verify the file against a hash you publish. println!("Download: {}", update.download_url.unwrap_or_default()); } else { println!("You have the latest version."); } Ok(())}Tauri apps
Call the crate from a Tauri command in the Rust core and return only the result to the web view, for the same reason Electron apps validate in the main process.
Questions and answers
Does it work with Tauri?
Yes. It is an ordinary crate; call it from a command on a blocking thread.
Does a compiled language make licensing safer?
It makes the check harder to find and patch than in Python or JavaScript, but not impossible. The signature is what stops fake servers.
How is the computer identified?
The device id is a SHA-256 hash of the operating system’s machine id (Windows MachineGuid, Linux /etc/machine-id, macOS IOPlatformUUID). It survives reinstalls of your app, and the Node, .NET, Kotlin, Java, Rust, Go, C and C++ SDKs compute the same value on the same machine.
What does it cost?
Nothing today. Every feature is free and unlimited: products, keys, devices, validations, offline files and floating seats. See pricing.