On this page

Guide

How to Add License Keys to a Python App (Offline-Ready)

If you distribute a commercial Python desktop application (PyQt, PySide, Tkinter) or a paid local CLI tool, hardcoding a plain HTTP check against /verify?key=... can be bypassed in under a minute by editing /etc/hosts or running a local proxy.

Using asymmetric Ed25519 signatures, your Python client embeds only the public key, validates activation responses mathematically, and supports verified offline launches when you enable a lease window. For the full API reference see the Keyfern documentation.

Step 1: Install the Keyfern Python SDK

The Keyfern Python package is a lightweight single-dependency wrapper (relying only on cryptography, which pip installs for you):

pip install keyfern

No access to PyPI? pip install https://keyfern.com/dl/keyfern-1.0.0-py3-none-any.whl

Step 2: Configure offline leases (optional)

In Keyfern, offline validation is disabled by default (lease_days = 0), meaning the app checks online on startup. To allow paying customers to launch your Python app offline (for example on flights or isolated workstations), open your Product settings in the Keyfern dashboard and set a lease duration between 1 and 3,650 days (supported on every plan, including Free).

Step 3: Activate and verify licenses

Follow our full working repository on GitHub: velopre/python-license-key-example.

from keyfern import Keyfern, KeyfernError

kf = Keyfern(
    product="prod_xxx",
    public_key="<public key from the dashboard>",   # safe to embed in your app
    cache_path="license.cache",
)

try:
    lic = kf.activate_or_cached(user_key)   # online check, verified cache when offline
except KeyfernError as e:                   # blocked, expired, invalid_key, machine_limit, lease_expired...
    print("License problem:", e.code)
else:
    if lic.has(1):                          # feature flag F1
        unlock_pro()

Using the SDK’s activate_or_cached flow:

  1. On every start the SDK calls POST /v1/activate with the user’s license key, a machine fingerprint hash, and a random anti-replay nonce.
  2. The server returns a signed payload ({payload, sig}).
  3. The client checks the Ed25519 signature using your embedded Public Key, checks the nonce, and saves the signed answer locally.
  4. When the server cannot be reached, the app unlocks from that cached answer, but only if its signature is valid, it belongs to this machine, the lease (lease_until) is still valid and no system clock rollback of more than 1 hour is detected.

Why Keyfern for Python builders

Getting started

Get started in 5 minutes in the Keyfern Quickstart or view our Cryptolens & Devolens comparison. To create your keys and your first product for free, create an account.