
# Use unjwt from a CDN

> Load one module of unjwt into a plain HTML page from esm.sh, pinned to an exact version, and verify a token in the browser.

Sometimes a page is one HTML file with no build step: a debugging tool for your team, a demo, a quick look at what a token carries. The browser can import the package straight from a CDN. Each module has its own door, so asking for `unjwt/jws` downloads signing and verifying, not the encryption or session code.

## Before you start

- A secure context: a page served over HTTPS or from `localhost`. Signing, verifying, encrypting and importing keys all need the browser's Web Crypto, which it hides on other pages.
- Pin an exact version. Without one, esm.sh serves the newest release, and your page changes without a deploy. A pinned page also stays on that release: update the version in the URL when you upgrade.
- A pinned URL still trusts esm.sh to serve what npm published. For a page that handles anything sensitive, install the package and bundle it instead, as [Install unjwt](/getting-started/installation) describes.
- The issuer's public key as a JWK that names its `alg`, the way `generateJWK` and most JWK Set endpoints write it.

## Verify a token in the browser

This page takes a token and the public JWK of whoever signed it, checks the signature, and shows the claims and when the token expires. Nothing leaves the browser.

<!-- automd:region src="../../examples/recipes/cdn-page.html" lang="html" title="inspect-token.html" -->

```html [inspect-token.html]
<!doctype html>
<meta charset="utf-8" />
<title>Token inspector</title>

<p>A check in this page protects nothing on a server: verify the token there too.</p>
<form>
  <label>Token <textarea name="token" rows="4" required></textarea></label>
  <label>Issuer's public JWK <textarea name="jwk" rows="4" required></textarea></label>
  <button>Verify</button>
</form>
<output></output>

<script type="module">
  import { verify } from "https://esm.sh/unjwt@0.9.1/jws";

  const form = document.querySelector("form");
  const output = document.querySelector("output");

  form.addEventListener("submit", async (event) => {
    event.preventDefault();
    try {
      const key = JSON.parse(form.elements.jwk.value);
      const { payload } = await verify(form.elements.token.value.trim(), key);
      const expires = payload.exp ? new Date(payload.exp * 1000).toLocaleString() : "never";
      output.textContent = `Valid, expires ${expires}\n${JSON.stringify(payload, null, 2)}`;
    } catch (error) {
      output.textContent = `Not valid: ${error.code ?? error.message}`;
    }
  });
</script>
```

<!-- /automd -->

What makes it work without a build step:

- **Subpath and version**: the URL names a release and `/jws`, so the browser fetches that version of that module and nothing else. The pin on this page follows the library's current release.
- **Module script**: `import` only works inside `<script type="module">`, so a plain `<script>` won't do.
- **The key decides the algorithm**: `verify` accepts only the `alg` the JWK names, so a token signed any other way fails even before the signature is checked.
- **Expiry is checked too**: an expired token fails with `ERR_JWT_EXPIRED` instead of showing its claims; `exp` is in seconds, so the page multiplies by 1000 for `Date`.
- **Codes, not messages**: every failure from `verify` carries a `code`, which stays the same across releases. A `SyntaxError` from a pasted JWK that isn't JSON has none, so the page falls back to its message.

::warning
The user controls everything their browser runs, so a check in this page protects nothing on your server. Use it to look at tokens, and verify again on the server before you trust one.
::

## When it fails

You'll see each of these on the page, or in the browser console:

| Situation                                              | What happens                                                                                                                                                 |
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Page on plain HTTP, from a host other than `localhost` | The import works, but `verify` throws `ERR_JWK_INVALID`: the browser hides Web Crypto, so the key can't be imported. Its `cause` is the browser's own error. |
| Token signed by another key                            | `ERR_JWS_SIGNATURE_INVALID`.                                                                                                                                 |
| JWK for another algorithm than the token's             | `ERR_JWS_ALG_NOT_ALLOWED`.                                                                                                                                   |
| JWK without `alg`, for a key type that allows several  | `ERR_JWS_ALG_NOT_ALLOWED`: pass `{ algorithms: ["RS256"] }` as the third argument to say which one you accept. See [Verify a token](/jws/verify).            |
| A private JWK pasted as the key                        | `ERR_JWK_INVALID`. Paste the public half; a private key has no place in a browser.                                                                           |
| Token past its `exp`                                   | `ERR_JWT_EXPIRED`. See [Handle errors](/safety/errors).                                                                                                      |
| Version that does not exist, or a typo in the URL      | The import fails, and nothing in that module script runs.                                                                                                    |

## Going further

- **Import maps**: keep the version in one place with `<script type="importmap">` mapping `"unjwt/"` to `"https://esm.sh/unjwt@<version>/"`. Then `import { verify } from "unjwt/jws"` works in every module script on the page, and an upgrade changes one line.
- **A JWK Set**: pass the issuer's whole set, `{ "keys": [...] }`, instead of one key, and `verify` picks the key by the token's `kid`. See [Use a JWK Set](/jwk/sets).
- **Claims you expect**: add `{ issuer, audience }` to the options to refuse tokens meant for someone else. See [Validate claims](/safety/claims).
- **Bundlers**: `import { verify } from "unjwt/jws"` from an installed package gives the same module. The subpath matters most for imports over the network.
