Electron Builder auto-updater for private applications

Learn how to distribute your commercial Electron application with Electron Builder.

Requirements

When you create a release on GitHub you will need to upload the build artifacts (binaries). Make sure you don't modify the filenames of your binaries as incorrect filenames may result in your updates being rejected. For example, the following filenames are valid:

  • Example-1.0.10-arm64-mac.zip
  • Example-1.0.10-mac.zip
  • Example-1.0.10.AppImage
  • Example.Setup.1.0.10.exe

Electron License Package

You can license and add auto-update support your Electron application in just a matter of minutes with Unlock's plug-and-play licensing package. Scroll down to the 'Manual configuration' section if you want to manually integrate auto-updates.

Requirements

  • You will need your product ID.
  • A license key with the permissions: license:validate and license:activate.

Installation

To get started, you will need to install Unlock's NPM package:

npm install @unlocksh/electron-license

Open your main process, like main.js, and initiate Unlock with the following parameters:

1const { BrowserWindow, autoUpdater } = require('electron');
2const Unlock = new (require('@unlocksh/electron-license'))(
3 {
4 api: {
5 key: 'YOUR-API-KEY',
6 productId: 'YOUR-PRODUCT-ID',
7 },
8 license: {
9 encryptionKey: 'UNIQUE-KEY',
10 }
11 },
12 autoUpdater
13);

The API key and product ID parameters are mandatory. Ensure your API key only has the license:activate and license:validate permission scopes. You can omit the autoUpdater if you don't use Unlock for distribution.

You can enter any random string as the encryption key to encrypt the data on a user's device. The only reason why you would change this key in the future is when the key was breached. If you change the encryption key, all your users will be prompted to enter their license again.

Finally, you will need to provide Unlock with your primary application window. When a valid license is available, Unlock will open your primary window. If no valid license is on your user's device, Unlock will prompt the user to enter their license information.

1const createWindow = () => {
2 
3 const mainWindow = new BrowserWindow({
4 width: 800,
5 height: 600,
6 show: false // Important to set this to false
7 });
8 
9 mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
10 
11 Unlock.ifAuthorized(mainWindow);
12};

Make sure you set show to false for your main window. The main window will "unlock" when a device is authorized.

That's all it takes. Your application will now prompt your users for their license and auto-update their applications.

Set your environment variable

Set NODE_ENV to development during development.
Set NODE_ENV to production when building a production release.

Production

When you are ready to build a production ready release you will need to make sure you Electron application can access the necessary files needed for the license prompt window. This can be achieved by adding the required directory to the extraResources or extraResource config option:

1// Electron Builder
2extraResources: [
3 {
4 from: "./node_modules/@unlocksh/electron-license/license",
5 to: "license",
6 filter: "**/*"
7 }
8]
9 
10// Electron Native (e.g. Forge)
11packagerConfig: {
12 extraResource: './node_modules/@unlocksh/electron-license/license'
13}

Additional options

The following options allow you to tweak a couple of configuration options and provide your logo, and copy.

1{
2 "api":{
3 "key":"YOUR-API-KEY",
4 "productId":"YOUR-PRODUCT-ID"
5 },
6 "license":{
7 "requireEmail":false,
8 "checkIn":{
9 "value":24,
10 "unit":"hours"
11 },
12 "encryptionKey":"UNIQUE-KEY"
13 },
14 "prompt":{
15 "title":"Unlock",
16 "subtitle":"Activate your license to get started",
17 "logo":"https://unlock.sh/img/unlock-logo-grey.svg",
18 "email":"Email address",
19 "licenseKey":"License key",
20 "activateLicense":"Activate license",
21 "errors":{
22 "NOT_FOUND": "Your license information did not match our records.",
23 "SUSPENDED": "Your license has been suspended.",
24 "EXPIRED": "Your license has been expired.",
25 "FINGERPRINT_INVALID": "No license was found for this device.",
26 "FINGERPRINT_ALREADY_EXISTS": "An active license already exists for this device.",
27 "MAX_USAGE_REACHED": "Your license has reached its activation limit.",
28 "RELEASE_CONSTRAINT": "Your license has no access to this version."
29 }
30 },
31 "confirmation":{
32 "title":"You are awesome!",
33 "subtitle":"Thank you for activating your product license."
34 }
35}

If you use Unlock's licensees option together with licensing and want to make sure the license key matches with the licensee, you can set license.requireEmail to true. Setting this value to true will prompt users to enter their email.

By default, Unlock will do a license validity check every 24 hours. You can change this by setting the value and unit of the checkIn config.

Manual configuration

You can also choose to manually add auto update support to your Electron application.

Add the following code to your main.js.

1import {app} from 'electron'
2import {autoUpdater} from "electron-updater"
3 
4const server = 'https://dist.unlock.sh/v1/electron'
5const productId = ''
6 
7// If you require a valid license, you will need to pass the user's license key.
8// You will need to store the user's license key somewhere.
9const key = 'a3105b99-e849-412b-ac1c-6f9d6aade946';
10const url = `${server}/${productId}/releases?key=${key}`
11 
12// If you don't require a license to download new releases you can omit the key from the url.
13const url = `${server}/${productId}/releases`
14 
15autoUpdater.setFeedURL({
16 url: url,
17 serverType: 'json',
18 provider: "generic",
19 useMultipleRangeRequest: false
20})
21 
22app.on('ready', function() {
23 autoUpdater.checkForUpdatesAndNotify()
24})