Creating Licenses
Use Keygen's API and your code to create new licenses for your product. If you need help after reading this, can reach out to us anytime at [email protected].
This tutorial is written in JavaScript, but you can follow along in any
other language that you're comfortable in. Throughout this tutorial, you
will see placeholders such as <account>
within the code examples that
will need to be replaced with an ID for that particular resource type.
Before getting started, you will either need to log into your Dashboard and generate a product token, or generate an authentication token via the API. To create a token directly through the API, check out the code examples in the API reference or check out our guide on authenticating users.
If you are using product tokens, the code for this tutorial should not be written client-side. If you are wanting to allow users to create licenses for themselves client-side, then you should authenticate them and use an authentication token which belongs to them; otherwise, all code should be server-side.
Your admin, environment, and product tokens carry many privileges and should always be kept secret. Do not share your secret API tokens in publicly accessible areas such GitHub, embed them in client-facing code e.g. inside of your product, and so forth. Learn more.
Once you've created a user, you can create a license for them. This can either be done client- or server-side, and depending on which one of those you choose, the tokens you use to make the request will be different i.e. product tokens vs. user tokens, for server-side vs. client-side license creation, respectively.
metadata
to the resource if needed to associate
the license with user records on your end. Simply exclude the user
relationship when creating a license.
When creating licenses, we first need to determine what policy we want the license to implement. Policies are like blueprints for licenses—determining how a license is validated, how long it lasts, how many machines it can be used on, and more generally in relation to your product, what it's for. If you haven't created a policy yet, head over to your dashboard and create one for your product.
Once you've decided on a policy to use, we can make our request. Below
you will see that we're using an authentication token which belongs to
one of our user's, as well as including the user within the license's
relationships
object. This will make sure that our license is associated
with the correct user resource so that we can query it later when we
want to validate it.
You may be asking yourself about the license's key. You can manually specify a key by including it inside of the license resource's attributes object. When the license key is not manually specified, one will automatically be generated and included within the response body.
The response of this request will either be an error (e.g. if a required attribute is missing, or the key has already been used), or a license resource containing your new license's information (including the key).
const response = await fetch("https://api.keygen.sh/v1/accounts/<account>/licenses", { method: "POST", headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/vnd.api+json", "Authorization": "Bearer {CURRENT_USER_TOKEN}" }, body: JSON.stringify({ "data": { "type": "licenses", "relationships": { "policy": { "data": { "type": "policies", "id": "<policy>" } }, "owner": { "data": { "type": "users", "id": "{CURRENT_USER}" } } } } })}) const { data: license, errors } = await response.json()if (errors) { // … handle errors} console.log(`Our license's key is: ${license.attributes.key}`)
The license's key attribute will be the product key you will want to deliver to the end-user. Depending on your licensing model, you may also want to generate a license token for the license.
Next steps
Congrats! You've created your first license using Keygen. Next up, we can learn how to validate it. If you have any questions about what you've learned today, be sure to reach out!