Add license keys to a Python app
To add license keys to a Python app, install pylicensify, create a LicenseClient with your public key, and call client.validate(key). It checks the key online, verifies the Ed25519 signature on the answer and binds the key to the computer. If the result is falsy, stop the program and show result.error.
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.
pip install -U "pylicensify>=1.4"from pylicensify import LicenseClient, LicenseError, NetworkError client = LicenseClient( public_key="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)key = "XXXX-XXXX-XXXX-XXXX" # the customer's license key try: result = client.validate(key)except NetworkError: # Offline, or the server is down. That is not "unlicensed": try again, or # allow a few days since the last good check. raise SystemExit("Could not reach the license server. Please try again.")except LicenseError as e: # A forged or replayed answer, or a key for another of your products. raise SystemExit("License check failed: %s" % e) if not result: # Expired, revoked, device limit reached... the server says which. raise SystemExit("Not licensed: %s" % result.error) print("Licensed. Expires:", result.expires_at or "never")Why not just compare a key in your own code?
Python source and bytecode are easy to read, so a check like if key == SECRET is found in minutes. With pylicensify there is no secret in your app: it holds only a public key, which can check a signature but cannot make one.
Each call sends a fresh random nonce. The server signs its answer with an Ed25519 private key that never leaves it, and the SDK checks that signature with your public key. A fake license server, an edited reply or a replayed old reply fails the check.
A determined attacker can still edit your code to skip the check. Combine licensing with obfuscation and server-side logic for anything valuable.
Handle every outcome
| 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. |
Trials, seats, credits and offline files
The same client covers floating seats, usage credits, remote variables, update checks and air-gapped machines.
Entitlements. Switch features on per key: true/false, numbers or text. They come from the signed answer.
from pylicensify import LicenseClient client = LicenseClient( public_key="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)key = "XXXX-XXXX-XXXX-XXXX" # the customer's license key result = client.validate(key)if not result: raise SystemExit("Not licensed: %s" % result.error) # 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") is True: print("Pro features unlocked") seats = result.feature("seats", 1) # 1 when the key has no "seats"tier = result.feature("tier", "basic")print("Seats:", seats, "Tier:", tier, "All:", result.features)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.
from pylicensify import InvalidLicense, SignatureError, get_hwid, verify_offline_license # 1. Show this to the customer. They send it to you, and you create the file# for that device: License keys -> ... -> Offline license file.print("Device id:", get_hwid()) # 2. The customer saves the file next to your app. No network is used here.try: lic = verify_offline_license( "YOUR_PUBLIC_KEY", "license.lic", expected_product_id="YOUR_PRODUCT_ID", # your product )except SignatureError: raise SystemExit("The license file is missing, damaged or was edited.")except InvalidLicense as e: raise SystemExit("License file refused: %s" % e) # expired, other device, OS or product # 3. Use it. It never returns an invalid license: it raises instead.features = lic.get("features") or {}print("Licensed to:", lic.get("customer"), "Expires (unix time):", lic.get("expires_at") or "never")if features.get("pro") is True: print("Pro features unlocked")Usage credits. Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.
from pylicensify import LicenseClient, UsageExceeded client = LicenseClient( public_key="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)key = "XXXX-XXXX-XXXX-XXXX" # the customer's license key result = client.validate(key) # first: this activates the device on the keyif not result: raise SystemExit("Not licensed: %s" % result.error) print("Left:", client.get_usage(key).remaining) # reads only; None = no limit try: usage = client.record_usage(key, amount=1) # spend 1 credit, all or nothing print("Done.", usage.remaining, "credits left")except UsageExceeded as e: print("Out of credits. Refill:", e.usage.get("resets_at") or "buy more")Remote variables. Values you change in the dashboard without shipping an update: a link, a message, a switch.
from pylicensify import LicenseClient client = LicenseClient( public_key="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)key = "XXXX-XXXX-XXXX-XXXX" # the customer's license key result = client.validate(key) # first: this activates the device on the keyif not result: raise SystemExit("Not licensed: %s" % result.error) # Values you set on the Remote Variables page. Signed, and only for valid keys.url = client.get_variable(key, "download_url") # None when it isn't settheme = client.get_variable(key, "theme", default="dark") # or your own defaulteverything = client.get_variables(key) # all this key may see (a dict) print(url, theme, everything)Floating seats. Let only N people run the app at the same time, on any computers.
from pylicensify import LicenseClient, SeatUnavailable client = LicenseClient( public_key="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)key = "XXXX-XXXX-XXXX-XXXX" # the customer's license key def seat_lost(reason): print("Seat lost:", reason) # runs on a background thread: save the user's work try: # Takes a seat, keeps it with heartbeats, and frees it when the block ends. with client.session(key, on_lost=seat_lost) as seat: print("Seat taken:", seat.seats) # {'used': 1, 'max': 5} # ... your app runs here. Call this at safe points (before a save, # between jobs). It raises SeatUnavailable once the seat is gone. seat.ensure_active()except SeatUnavailable as e: print("No seat:", e)Updates. Tell the customer about a new version. Only a valid, active key is given the download link.
from pylicensify import LicenseClient client = LicenseClient( public_key="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)key = "XXXX-XXXX-XXXX-XXXX" # the customer's license key info = client.check_update(key, current_version="2.4.1", channel="stable") # or "beta" if info["update_available"]: print("New version:", info["version"]) print("What's new:", info["notes"]) # 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. print("Download:", info["download_url"])else: print("You have the latest version.")Questions and answers
Does it work with PyInstaller or Nuitka builds?
It is an ordinary Python package, so it is bundled like any other dependency. Keep your public key and product id in the code; both are safe to ship.
How is the computer identified?
The Python SDK uses the network card’s address as the device id, or a random id it stores once when that can’t be read. It is not the same value the other Licers SDKs use: if one machine runs apps in both, pass the same hwid to each.
Can I give a 14-day trial?
Yes. Issue a key with a duration and turn on “start on first activation”. The clock runs on the server, so changing the computer’s date does not extend it.
What does it cost?
Nothing today. Every feature is free and unlimited: products, keys, devices, validations, offline files and floating seats. See pricing.