Add license keys to a Go app
To add license keys to a Go program, run go get licers.com/go/licers, create a client with licers.New(publicKey, options), and call client.Validate(ctx, key). A nil error with Valid == false means the key was refused; a non-nil error means there is no answer you can trust.
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.
go get licers.com/go/licerspackage main import ( "context" "fmt" "log" "licers.com/go/licers") func main() { client, err := licers.New("YOUR_PUBLIC_KEY", &licers.Options{ // safe to ship inside your app ProductID: "YOUR_PRODUCT_ID", // your product: refuses keys for your other products AppVersion: "2.4.1", // shows in Analytics -> App versions }) if err != nil { log.Fatal(err) } ctx := context.Background() key := "XXXX-XXXX-XXXX-XXXX" // the customer's license key result, err := client.Validate(ctx, key) switch { case licers.IsNetwork(err): // Offline, or the server is down. That is not "unlicensed": try again, or // allow a few days since the last good check. log.Fatal("Could not reach the license server. Please try again.") case err != nil: // A forged or replayed answer, or a key for another of your products. log.Fatal("License check failed: ", err) case !result.Valid: // Expired, revoked, device limit reached... the server says which. log.Fatal("Not licensed: ", result.Error) } expires := result.ExpiresAt if expires == "" { expires = "never" } fmt.Println("Licensed. Expires:", expires)}Made for CLIs, agents and on-premises servers
- Pass a context to every call, so timeouts and cancellation work the way the rest of your program does.
- Signatures are checked with the standard library’s
crypto/ed25519. - For customers without internet access, ship a signed
.licfile and calllicers.VerifyOfflineLicense.
Handle every outcome
Use licers.IsNetwork(err) for outages and errors.Is(err, licers.ErrSignature) for forged answers.
| 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
Entitlements. Switch features on per key: true/false, numbers or text. They come from the signed answer.
package main import ( "context" "fmt" "log" "licers.com/go/licers") func main() { client, err := licers.New("YOUR_PUBLIC_KEY", &licers.Options{ // safe to ship inside your app ProductID: "YOUR_PRODUCT_ID", // your product: refuses keys for your other products AppVersion: "2.4.1", // shows in Analytics -> App versions }) if err != nil { log.Fatal(err) } ctx := context.Background() key := "XXXX-XXXX-XXXX-XXXX" // the customer's license key result, err := client.Validate(ctx, key) if err != nil { log.Fatal(err) } if !result.Valid { log.Fatal("Not licensed: ", 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.FeatureBool("pro") { fmt.Println("Pro features unlocked") } seats, ok := result.FeatureInt("seats") if !ok { seats = 1 // the key has no "seats" } tier, ok := result.FeatureString("tier") if !ok { tier = "basic" } fmt.Println("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.
package main import ( "errors" "fmt" "log" "licers.com/go/licers") func 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. fmt.Println("Device id:", licers.HWID()) // 2. The customer saves the file next to your app. No network is used here. lic, err := licers.VerifyOfflineLicense("YOUR_PUBLIC_KEY", "license.lic", &licers.OfflineOptions{ ProductID: "YOUR_PRODUCT_ID", // your product }) switch { case errors.Is(err, licers.ErrSignature): log.Fatal("The license file is missing, damaged or was edited.") case err != nil: log.Fatal("License file refused: ", err) // expired, other device, OS or product } // 3. Use it. An invalid license is always an error. fmt.Println("Licensed to:", lic.Customer, "Expires (unix time, 0 = never):", lic.ExpiresAt) if lic.FeatureBool("pro") { fmt.Println("Pro features unlocked") }}Usage credits. Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.
package main import ( "context" "errors" "fmt" "log" "licers.com/go/licers") func main() { client, err := licers.New("YOUR_PUBLIC_KEY", &licers.Options{ // safe to ship inside your app ProductID: "YOUR_PRODUCT_ID", // your product: refuses keys for your other products AppVersion: "2.4.1", // shows in Analytics -> App versions }) if err != nil { log.Fatal(err) } ctx := context.Background() key := "XXXX-XXXX-XXXX-XXXX" // the customer's license key result, err := client.Validate(ctx, key) // first: this activates the device on the key if err != nil { log.Fatal(err) } if !result.Valid { log.Fatal("Not licensed: ", result.Error) } balance, err := client.GetUsage(ctx, key) // reads only if err != nil { log.Fatal(err) } if left := balance.Remaining(); left != nil { // nil = no limit fmt.Println("Left:", *left) } usage, err := client.RecordUsage(ctx, key, 1) // spend 1 credit, all or nothing if errors.Is(err, licers.ErrUsageExceeded) { fmt.Println("Out of credits.") return } if err != nil { log.Fatal(err) } fmt.Println("Done. Used so far:", usage.Used)}Remote variables. Values you change in the dashboard without shipping an update: a link, a message, a switch.
package main import ( "context" "fmt" "log" "licers.com/go/licers") func main() { client, err := licers.New("YOUR_PUBLIC_KEY", &licers.Options{ // safe to ship inside your app ProductID: "YOUR_PRODUCT_ID", // your product: refuses keys for your other products AppVersion: "2.4.1", // shows in Analytics -> App versions }) if err != nil { log.Fatal(err) } ctx := context.Background() key := "XXXX-XXXX-XXXX-XXXX" // the customer's license key result, err := client.Validate(ctx, key) // first: this activates the device on the key if err != nil { log.Fatal(err) } if !result.Valid { log.Fatal("Not licensed: ", result.Error) } // Values you set on the Remote Variables page. Signed, and only for valid keys. url, ok, err := client.GetVariable(ctx, key, "download_url") // ok is false when it isn't set if err != nil { log.Fatal(err) } everything, err := client.GetVariables(ctx, key) // all this key may see if err != nil { log.Fatal(err) } fmt.Println(url, ok, everything)}Floating seats. Let only N people run the app at the same time, on any computers.
package main import ( "context" "errors" "fmt" "log" "licers.com/go/licers") func main() { client, err := licers.New("YOUR_PUBLIC_KEY", &licers.Options{ // safe to ship inside your app ProductID: "YOUR_PRODUCT_ID", // your product: refuses keys for your other products AppVersion: "2.4.1", // shows in Analytics -> App versions }) if err != nil { log.Fatal(err) } ctx := context.Background() key := "XXXX-XXXX-XXXX-XXXX" // the customer's license key // Takes a seat and keeps it with heartbeats in the background until Stop. seat, err := client.StartSession(ctx, key, func(reason string) { log.Print("Seat lost: ", reason) // save the user's work }) if errors.Is(err, licers.ErrSeatUnavailable) { log.Fatal("No seat: ", err) } if err != nil { log.Fatal(err) } defer seat.Stop() // frees the seat for someone else fmt.Println("Seat taken:", seat.SeatsUsed, "of", seat.SeatsMax) // ... your app runs here. Call this at safe points (before a save, between // jobs). It returns an error once the seat is gone. if err := seat.EnsureActive(); err != nil { log.Print("Seat lost: ", err) }}Updates. Tell the customer about a new version. Only a valid, active key is given the download link.
package main import ( "context" "fmt" "log" "licers.com/go/licers") func main() { client, err := licers.New("YOUR_PUBLIC_KEY", &licers.Options{ // safe to ship inside your app ProductID: "YOUR_PRODUCT_ID", // your product: refuses keys for your other products AppVersion: "2.4.1", // shows in Analytics -> App versions }) if err != nil { log.Fatal(err) } ctx := context.Background() key := "XXXX-XXXX-XXXX-XXXX" // the customer's license key update, err := client.CheckUpdate(ctx, key, "2.4.1", "stable") // or "beta" if err != nil { log.Fatal(err) } if !update.UpdateAvailable { fmt.Println("You have the latest version.") return } fmt.Println("New version:", update.Version) fmt.Println("What's new:", update.Notes) // The answer is signed, but DownloadURL and Notes are not: check the URL is on // your own host, and verify the file against a hash you publish. fmt.Println("Download:", update.DownloadURL)}Questions and answers
Can I license a self-hosted server per installation?
Yes. Give each customer one key with a device limit equal to the servers they paid for, or use floating seats.
Does it work in containers?
Containers can share or lack a machine id. Pass your own stable device id, or use floating seats, which count running instances instead.
Is it MIT licensed?
Yes.
What does it cost?
Nothing today. Every feature is free and unlimited: products, keys, devices, validations, offline files and floating seats. See pricing.