Storing User Metadata

Use Keygen's API and your client-side code to store metadata on your user resources. 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 e.g. {ACCOUNT} within the code examples that will need to be replaced with your account's slug (or ID), or other resource information.


Keygen supports adding arbitrary metadata to most resources, such as the user resource. Metadata is for your use only and is not explicity shown to customers unless you display it (though note that it is included in API responses e.g. when you request a user resource).

With metadata, you can associate other information, such as the user's customer_id within your payment system, the user's shipping address, etc. Any metadata you include will be viewable within your dashboard (e.g. when looking at an individual resource).

Be aware that since the metadata attribute is simply a key-value store (hash), all write operations will overwrite the entire hash, so be sure to merge existing data on your end when performing partial updates.

The response of this request will either be an error, or the full user resource including your user's updated metadata.

const response = await fetch("https://api.keygen.sh/v1/accounts/{ACCOUNT}/users", {
  method: "PATCH",
  headers: {
    "Content-Type": "application/vnd.api+json",
    "Accept": "application/vnd.api+json",
    "Authorization": "Bearer {USER_TOKEN}"
  },
  body: JSON.stringify({
    "data": {
      "type": "users",
      "attributes": {
        "metadata": {
          "customerId": "cus_VuAsGNTGqYgz1P"
        }
      }
    }
  })
})

const { data, errors } = await response.json()

Next steps

Congrats! You've stored metadata on one of your users using Keygen. Next up, we can create a license for your user and learn how to validate it. If you have any questions about what you've learned today, be sure to reach out!