Skip to main content
This guide walks you through adding Avalex license validation to a Rust application. You will define typed structs for the request and response, generate a SHA-256 HWID from stable system identifiers, and perform an async HTTP POST using reqwest. The result is a single validate_license function you can await at startup before your application proceeds.

Cargo.toml Dependencies

Add the following entries to your Cargo.toml before writing the validation code:
  • reqwest — async HTTP client with built-in JSON serialization support.
  • serde — derive macros for serializing the request struct and deserializing the response.
  • sha2 — pure-Rust SHA-256 implementation used to hash the raw hardware identifier.
  • whoami — cross-platform access to the hostname and username without unsafe FFI.
  • tokio — async runtime required by reqwest.

Full Example

Code Walkthrough

HWID generation (get_hwid) get_hwid feeds the system hostname and username into a Sha256 hasher sequentially using hasher.update(...). Because update can be called multiple times, there is no need to allocate an intermediate concatenated string. The finalized digest is formatted as uppercase hex with {:X} and sliced to 32 characters. If whoami::fallible::hostname() fails (e.g., in a sandboxed environment), unwrap_or_default() returns an empty string rather than panicking, keeping the function infallible. Typed request and response structs ValidateRequest derives Serialize and uses #[serde(rename = "...")] to emit the camelCase field names that the Avalex API expects. ValidateResponse derives Deserialize and maps the valid boolean from the JSON response. Using lifetime parameters (<'a>) on ValidateRequest avoids unnecessary heap allocations by borrowing the string slices passed in by the caller. Async validation (validate_license) validate_license constructs a one-shot reqwest::Client, builds the typed request body, and sends it with .json(&req_body) — which sets the Content-Type: application/json header and serializes the struct automatically. The ? operator propagates any transport error up to the caller as a Box<dyn Error>. A non-success HTTP status (including 429) returns Ok(false) so the caller can apply its own grace-period logic without treating it as a fatal error.
Replace the hardcoded http://localhost:8080/licenses/validate URL with your production HTTPS endpoint. Store it in a configuration file or environment variable rather than embedding it as a string literal, which makes obfuscation easier and deployment more flexible.