VAPID key generator

Create a public and private VAPID key pair for web push. The keys are generated by your browser’s Web Crypto API on this page. Nothing is sent to our server or stored anywhere.

Your key pair (P-256, URL-safe Base64)

.env format
Paste into your environment file

    
JSON (web-push format)
Same shape as npx web-push generate-vapid-keys --json

    
Private key as JWK
For libraries that take a JSON Web Key

    

How to use your VAPID keys

In the browser, pass the public key when you subscribe:

const toBytes = (b64) => Uint8Array.from(atob(b64.replace(/-/g, '+').replace(/_/g, '/')), (c) => c.charCodeAt(0));

const reg = await navigator.serviceWorker.register('/sw.js');
const sub = await reg.pushManager.subscribe({
  userVisibleOnly: true,
  applicationServerKey: toBytes('YOUR_PUBLIC_KEY')
});
await fetch('/api/push/subscribe', { method: 'POST', body: JSON.stringify(sub) });

On the server (Node.js), sign each push with the private key:

import webpush from 'web-push';
webpush.setVapidDetails('mailto:[email protected]', process.env.VAPID_PUBLIC_KEY, process.env.VAPID_PRIVATE_KEY);
await webpush.sendNotification(subscription, JSON.stringify({ title: 'Hello', body: 'It works' }));

On the server (PHP), with minishlink/web-push:

$webPush = new Minishlink\WebPush\WebPush(['VAPID' => [
    'subject' => 'mailto:[email protected]',
    'publicKey' => getenv('VAPID_PUBLIC_KEY'),
    'privateKey' => getenv('VAPID_PRIVATE_KEY'),
]]);

Generate keys once per site and keep them. Every subscription is bound to the public key it was created with. If you replace the key pair, existing subscribers stop receiving your notifications and have to subscribe again.

Want the background? Read VAPID keys explained, or send a test push to your browser.

Questions

Are these VAPID keys safe to use in production?

Yes. They are generated by your browser’s Web Crypto API (ECDSA on the P-256 curve), the same algorithm the web-push libraries use. The private key never leaves this page: there is no network request. Close the tab and they are gone.

Which key goes where?

The public key goes in your front-end code as the applicationServerKey for pushManager.subscribe(). The private key goes only on your server, in an environment variable or secret store, and is used to sign each push request.

What format are the keys in?

URL-safe Base64 without padding, the format used by web-push (Node), minishlink/web-push (PHP), pywebpush and most push services. The public key is the 65-byte uncompressed point; the private key is the 32-byte scalar (the "d" value of the JWK).

Can I generate new keys for an existing site?

You can, but existing subscribers were created with the old public key and will stop receiving pushes signed with the new private key. Generate keys once per site and keep them.

How do I generate VAPID keys on the command line?

With Node: npx web-push generate-vapid-keys. With PHP: Minishlink\WebPush\VAPID::createVapidKeys(). With OpenSSL: openssl ecparam -name prime256v1 -genkey -noout -out vapid.pem, then derive the public key and convert both to URL-safe Base64.

Rather not run your own push server?

SuperWicket creates and stores a key pair for each website, or imports the one you already use, so you keep your subscribers.