Install unjwt
Add unjwt to a project, import each module from its own path, and know which types to reach for.
unjwt lives on npm as unjwt. It ships as ES modules with TypeScript types included, and each module has its own import path, so you load only what you use.
#Before you start
A quick check of your setup saves a confusing error later:
- Node.js: Node.js 22 or later.
- Other runtimes: Bun, Deno, Cloudflare Workers and browsers work when they expose the Web Crypto API as the global
crypto. - Browsers: Web Crypto's
crypto.subtleexists only on HTTPS pages and onlocalhost. On a plain HTTP page, nothing that signs, verifies, encrypts or generates keys works. - TypeScript: 5.7 or later, with
"DOM"or"WebWorker"incompilerOptions.lib, because the types nameCryptoKey,RequestandHeaders. - ES modules only: the package has no CommonJS build.
- No dependencies: installing
unjwtadds one package to your lockfile. It has no runtime dependencies and no peer dependencies.
#Install the package
Pick the line for the package manager your project already uses:
# ✨ Auto-detect
npx nypm install unjwt
# npm
npm install unjwt
# yarn
yarn add unjwt
# pnpm
pnpm add unjwt
# bun
bun install unjwt
# deno
deno install npm:unjwt#Import a module
Import each function from the path of its module, so every import line says which part of the library it needs:
import { sign, verify } from "unjwt/jws";
import { generateJWK } from "unjwt/jwk";
import { isJWTError } from "unjwt/error";The package exports these paths:
| Import from | What it holds |
|---|---|
unjwt | Everything from unjwt/jws, unjwt/jwe, unjwt/jwk, unjwt/utils and unjwt/error, plus every type |
unjwt/jws | sign, verify and the multi-signature functions |
unjwt/jwe | encrypt, decrypt and the multi-recipient functions |
unjwt/jwk | Generating, importing, exporting, wrapping and deriving keys, and JWK Sets |
unjwt/utils | The base64url codec, type guards, claim checks and random bytes |
unjwt/error | JWTError, isJWTError and the error code types |
unjwt/session | defineJWSSession, defineJWESession, the driver API, the Web driver, cookie helpers and the token transport |
unjwt/dpop | createDPoPProof and verifyDPoPProof |
A bundler trims an import from unjwt down to what you call, so the root works fine in an app you build. Without a build step, each path loads only its own module; Use unjwt from a CDN shows a page that does that.
#Use the types
The types come in the box, so there's no @types/unjwt to install. Every type is exported from unjwt, and a few of them show up all over the API:
JWKandJWKSet: a key as JSON, and{ keys: JWK[] }. See Keys.JWTClaims: the registered claims (iss,sub,aud,exp,nbf,iat,jti) plus any member of your own. Extend it for your claims and pass it asverify<MyClaims>(…).JWTErrorandKnownJWTErrorCode: the one error class, and the union of everycodeit can carry. Import both fromunjwt/error; Handle errors explains each code.SessionConfigJWSandSessionConfigJWE: the config of a signed or an encrypted session, fromunjwt/session. See Configure a session.
import type { JWK, JWKSet, JWTClaims } from "unjwt";
import type { KnownJWTErrorCode } from "unjwt/error";
import type { SessionConfigJWS } from "unjwt/session";#When it fails
These setup mistakes show up as errors rather than as wrong results:
| Situation | What happens |
|---|---|
An import names a path the package doesn't export, such as unjwt/adapters/h3 | Node.js throws ERR_PACKAGE_PATH_NOT_EXPORTED and bundlers fail to resolve the import. The table above lists every path; sessions on every framework come from unjwt/session. |
| A browser page served over plain HTTP | Generating keys throws a TypeError, and signing or verifying throws a JWTError, because crypto.subtle is missing. Serve the page over HTTPS or from localhost. |
| TypeScript older than 5.7 | tsc reports Type 'Uint8Array' is not generic inside the package's types. Upgrade TypeScript. |
compilerOptions.lib without "DOM" or "WebWorker" | tsc reports Cannot find name 'Headers' or 'CryptoKey' inside the package's types. Add one of the two. |