How to Make Craft CMS Plugin Licenses Using the Craftnet API

Ryan Masuga Sep 5, 2018 Read time: 3 minute read
Generate License Key with Craftnet API

Need to give away a free license for a commercial plugin in the Craft CMS Plugin Store? There is no GUI at id.craftcms.com to do this, so use the Craftnet API to easily generate license keys without a purchase.

We developed Link Vault for Craft 3 and had been using it on projects for a few months before it was approved and accepted into the Craft Plugin store as a commercial plugin.

Plugins that are in the Craft Plugin Store communicate with sites on which they’re installed, so now that Link Vault was in this official plugin list (and commercial), we were presented with license key “nag notices” in our Craft control panels.

Craft Plugin License Key Nag Notice

We needed to be able to gift ourselves a few licenses to cover the sites it was in use on (we could just pay ourselves, but that feels a little Inception-y, and I don’t want to rip a hole in the space-time continuum). Further, we will want to grandfather in all the users who had bought Link Vault for Craft 2 and may want to upgrade those sites. There is currently no way to do this when logged into id.craftcms.com, so you have to use the Craftnet API to create a plugin license, which is easy to do.

New Craft ID API Token

To generate a license key without requiring a purchase, you need your Craft ID username, and an API token from your account. Under Developer Settings, click “Generate New API Token” and copy that somewhere, because when you return to the page, it will be all asterisks.

With those two pieces of information, you have enough to generate a license key. Assuming my login is masugadesign and my API Token is uurzawyghj3ebb7mp8r762h7dn8htstx8xsl7ou7, I would open a terminal window and enter:

$ curl https://api.craftcms.com/v1/plugin-licenses \
   -u masugadesign:uurzawyghj3ebb7mp8r762h7dn8htstx8xsl7ou7 \
   -d $'{
          "edition": "standard",
          "plugin": "linkvault",
          "email": "plugins@gomasuga.com"
        }'

This will generate a standard Link Vault license for the user associated with the ”plugins@gomasuga.com” email address.

The response that comes back looks like this:

{  
   "license":{  
      "id":1234,
      "pluginId":55555,
      "editionId":66666,
      "ownerId":123,
      "cmsLicenseId":null,
      "pluginHandle":"linkvault",
      "edition":"standard",
      "expirable":false,
      "expired":false,
      "autoRenew":false,
      "email":"plugins@gomasuga.com",
      "key":"IC7D12345678901234567890",
      "notes":null,
      "lastVersion":null,
      "lastAllowedVersion":null,
      "lastActivityOn":null,
      "lastRenewedOn":null,
      "expiresOn":null,
      "dateCreated":null,
      "dateUpdated":null,
      "uid":null
   }
}

If you (or the person to whom you assigned the license) look under Plugins in your Craft ID account, you will see the new license there, under Licenses > Plugins.

New License Listed Under Plugins

Enter the full license key in your Craft control panel, and the green check-mark will indicate that you're all set.

Valid Craft Plugin

Alternatives

Just yesterday, Studio Espresso released Craftnet CP plugin for Craft CMS 3.x, which is a Craft plugin that allows you to "Generate one or more licenses for a plugin." You could try this as well, but note that the author says "Note that I made this mostly for myself and that future development will take place if and when I need it. Issues and PR's will be handled on a best-effort basis."

Update: Fred Carlsen of Superbig pointed out that he also has a plugin called Craftnet CLI, which should make this process a bit nicer via the command line.

Subscribe to our newsletter!

A few times a year we send out a newsletter with tips and info related to Craft CMS, technical SEO, and things we find interesting. No spam. Unsubscribe any time.

You Might Also Like

Blog hero google analytics

Google Analytics Alternatives For Better Privacy

Ryan Masuga Apr 30, 2021  ·  4 minute read

User privacy is a growing concern on the web. Websites using Google Analytics to track user behavior contribute to the privacy problem, but there are many privacy-focused web analytics alternatives to use instead.