Skip to content
How to load the UniPDF license key?

How to load the UniPDF license key?

The key has to be loaded before the first call into the library, so init is the place to do it. Anything that runs earlier will fail with a license error.

A metered API key and an offline key are loaded by different calls, both in github.com/unidoc/unipdf/v5/common/license. The major version in that import path has to match the version of the library you are calling, since each keeps its own key.

Metered API key

A metered key is the API key from UniCloud. Pass it to SetMeteredKey and nothing else is needed.

package main

import (
    "os"

    "github.com/unidoc/unipdf/v5/common/license"
)

func init() {
    if err := license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY")); err != nil {
        panic(err)
    }
}

Usage is reported back to the license server as documents are processed. By default the counts are cached on disk first, under UNIDOC_LICENSE_DIR if that is set and the home directory otherwise, which is worth pointing somewhere writable in a container. license.SetMeteredKeyPersistentCache(false) reports immediately instead and needs no storage at all. license.GetMeteredState() returns the documents used and the credits left.

Offline key

An offline key is a signed block of text, verified from its own signature with no network connection. It is loaded together with the customer name, which has to match the name embedded in the key exactly.

package main

import (
    "fmt"

    "github.com/unidoc/unipdf/v5/common/license"
)

const offlineLicenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
contents here.
-----END UNIDOC LICENSE KEY-----
`

func init() {
    if err := license.SetLicenseKey(offlineLicenseKey, `My Company`); err != nil {
        panic(err)
    }
}

func main() {
    lk := license.GetLicenseKey()
    if lk == nil {
        fmt.Println("no license key loaded")
        return
    }
    fmt.Println(lk.ToString())
}

Keep the BEGIN and END lines and the line breaks as they came. A key pasted onto a single line, or with the header stripped, does not parse.

GetLicenseKey returns nil when nothing was loaded. For an offline key ToString prints the license id, customer, tier and expiry date, which is the quickest way to confirm that the key running in production is the one you think it is. For a metered key it prints only Metered subscription; use GetMeteredState for the numbers.

Environment variables

The offline key can also come from the environment. Set both of these and the license package loads the key during its own package initialization:

  • UNIPDF_CUSTOMER_NAME - the customer name.
  • UNIPDF_LICENSE_PATH - path to a file containing the license key.

On Linux and macOS:

export UNIPDF_CUSTOMER_NAME=UniDoc
export UNIPDF_LICENSE_PATH=/path/to/licenses/UniDoc.txt

On Windows:

set UNIPDF_CUSTOMER_NAME=UniDoc
set UNIPDF_LICENSE_PATH="C:\path\to\license.txt"

Both variables are required; setting one has no effect. Failures here are quiet. An unreadable file or a customer name that does not match the key is logged at error level and then ignored, so the symptom is a license error on the first operation rather than anything at startup. GetLicenseKey() == nil tells you the load did not happen.

There is no environment variable for the metered key. Reading one yourself and passing it to SetMeteredKey, as in the example above, is the equivalent.

How to load the UniOffice license key?

Last updated on