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.subtle exists only on HTTPS pages and on localhost. On a plain HTTP page, nothing that signs, verifies, encrypts or generates keys works.
  • TypeScript: 5.7 or later, with "DOM" or "WebWorker" in compilerOptions.lib, because the types name CryptoKey, Request and Headers.
  • ES modules only: the package has no CommonJS build.
  • No dependencies: installing unjwt adds 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 fromWhat it holds
unjwtEverything from unjwt/jws, unjwt/jwe, unjwt/jwk, unjwt/utils and unjwt/error, plus every type
unjwt/jwssign, verify and the multi-signature functions
unjwt/jweencrypt, decrypt and the multi-recipient functions
unjwt/jwkGenerating, importing, exporting, wrapping and deriving keys, and JWK Sets
unjwt/utilsThe base64url codec, type guards, claim checks and random bytes
unjwt/errorJWTError, isJWTError and the error code types
unjwt/sessiondefineJWSSession, defineJWESession, the driver API, the Web driver, cookie helpers and the token transport
unjwt/dpopcreateDPoPProof 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:

  • JWK and JWKSet: 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 as verify<MyClaims>(…).
  • JWTError and KnownJWTErrorCode: the one error class, and the union of every code it can carry. Import both from unjwt/error; Handle errors explains each code.
  • SessionConfigJWS and SessionConfigJWE: the config of a signed or an encrypted session, from unjwt/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:

SituationWhat happens
An import names a path the package doesn't export, such as unjwt/adapters/h3Node.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 HTTPGenerating 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.7tsc 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.