Back to Templates

License keys for selfhosting

Protect your Next.js routes with license keys at runtime

Written by
Protect your Next.js routes with license keys at runtime

Unkey for license keys

Licensing parts of your apps for selfhosting
Video demo

This is an example of protecting parts of your Next.js application with license keys.

The idea is to offer your app as a self-hosted solution, where users can download the app and run it on their own servers. To access the app or parts of it, users need to buy a license from you.

In this example. we have a simple Next.js app with a single page (/ee) that requires a valid license key to access. The license key is stored in a .env file and is checked at runtime using Next.js middleware.

Getting Started

First, run the development server:

1npm run dev
2# or
3yarn dev
4# or
5pnpm dev
6# or
7bun dev

Open http://localhost:3000/ee with your browser to see a page blocking access to users without a valid license key.

Add the following license key to your .env file:


Now restart your development server and try to access the page again. You should now see the content of the page.

License key management

To manage license keys, you can use the Unkey Dashboard or API and optionally add a white-label proxy in between, like in this example. (See white label for more information.)

Protect your API.
Start today.

2500 verifications and 100K successful rate‑limited requests per month. No CC required.