---
title: "Nonce Authentication"
description: "Authenticate the Landing Page Editor with short-lived nonces that your backend signs with the Secret key, instead of the API key alone."
url: https://docs.topol.io/landing-page-editor/guide/nonce-authentication.html
---

# Nonce Authentication

**Pass a `getNonce` callback and the editor authenticates against Topol with short-lived nonces that your backend signs with the Secret key.** The API key stays in the page for backwards compatibility, but what actually authorizes the session is a nonce bound to the current user. It lives as long as the expiry your backend sets (an hour at most is recommended) and stops working as soon as you rotate the Secret key or revoke the API token.

**Nonce authentication is the setup we recommend for every integration.** The API key alone keeps working and is enough to try the editor, but it sits in your page source and never expires. There is no separate option to switch on: the presence of `getNonce` is what enables nonce authentication. Nothing else changes: templates, autosaves, images, comments and sections keep going through your own `api.*` endpoints and callbacks exactly as before.

```js
const TOPOL_OPTIONS = {
  authorize: {
    apiKey: "YOUR_API_KEY", // your API key, unchanged
    entityId: "42",
  },
};

const LPE = LandingPageEditor({
  config: TOPOL_OPTIONS,
  async getNonce() {
    // Your backend signs the nonce with the Secret key (see below).
    const res = await fetch("/api/topol/nonce", { method: "POST" });
    const { nonce } = await res.json();
    return nonce;
  },
});
LPE.render("#landing-page-editor");
```

## How it works

1.  **Your backend signs a nonce.** It signs an `nc_…` nonce for the user opening the editor with the [Secret key](https://docs.topol.io/email-editor/guide/how-to-get-secret-key.html). It doesn't call Topol.
2.  **Your page hands it over.** The editor calls the `getNonce` callback whenever it needs a nonce: at boot, and again before the current one expires. The callback runs on your page, first-party, so your session cookies work and there is no CORS to set up. Never expose the Secret key to the browser: `getNonce` should call _your_ endpoint, which holds the key server-side.
3.  **The editor authorizes with it.** Boot goes through `GET /auth/me` with the nonce instead of the legacy authorize call. Every later request to Topol's API carries the current nonce as `Authorization: Bearer nc_…`; requests to your own `api.*` endpoints never see it and keep using [`apiAuthorizationHeader`](./authorization.html#apiauthorizationheader).

### Signing nonces

The Landing Page Editor accepts the same nonces as the Email Editor, so one signing endpoint on your backend serves both. A nonce is a JSON payload and its HMAC-SHA256 signature:

```
nc_<base64url(payload)>.<base64url(HMAC-SHA256(base64url(payload), key))>
```

| Field | Type | Value |
| --- | --- | --- |
| `credPk` | string | Your API key, exactly as you pass it in `authorize.apiKey` |
| `entityId` | string | The account the editor works for. Use the same value as `authorize.entityId`; every user-scoped action is attributed to it. Must be a JSON string: convert numeric ids with `String()` |
| `exp` | integer | Expiry in Unix seconds. An hour from now at most is recommended |

`userId` is still accepted in the payload as a deprecated alias of `entityId` until the next major version; if both are present, `entityId` wins.

The signing `key` is the SHA-256 hex digest of your Secret key: `sha256_hex("sk_…")`. If your Secret key doesn't start with `sk_` (keys issued before the prefix existed), add `sk_` in front of it before hashing. Base64url means URL-safe Base64 without `=` padding. See [Sign nonces on your backend](https://docs.topol.io/email-editor/guide/nonce-authentication.html#step-1-sign-nonces-on-your-backend) for complete Node.js and PHP examples.

> **WARNING**
>
> Never send the Secret key to the browser, and never sign nonces in the browser. Your endpoint should sign nonces only for the user who is signed in.

## Lifecycle and failure handling

-   **Proactive refresh.** The editor decodes the nonce's expiry and asks `getNonce` for a new one at roughly 80 % of its lifetime, so an open editor is never interrupted.
-   **One retry on 401.** A rejected nonce (revoked, rotated Secret key) triggers exactly one forced refresh and retry of the failed request.
-   **Timeout.** A `getNonce` that does not settle within 15 seconds is treated as failed; the editor keeps using the previous nonce while it is still valid.
-   **Errors reach `onEditorError`.** A first nonce that cannot be obtained fails the boot with `source: "authorize"`. A later refresh failure is reported with `source: "nonceAuth"` and the editor keeps working on the last valid nonce until it expires. See [`onEditorError`](https://docs.topol.io/landing-page-editor/reference/topol-options.html#oneditorerror).

## Revocation

Nonces are not stored on Topol's side; they are signed with your token's Secret key. Rotating the Secret key in your [Topol Account](https://app.topol.io) invalidates every nonce signed with the old one, and revoking the token rejects nonces immediately regardless of their remaining lifetime.
