VAPID keys explained

VAPID is a key pair that identifies your server to push services. How to generate the keys, where each one goes and what happens if you change them.

Updated

VAPID keys are a public and private key pair that identify your application server to browser push services. The public key goes to the browser when a user subscribes. The private key stays on your server and signs every push request. You generate the pair once, for free, with no account at any browser vendor.

VAPID stands for Voluntary Application Server Identification and is defined in RFC 8292.

Generate a key pair

With the web-push package for Node.js:

npx web-push generate-vapid-keys

It prints a public key and a private key, both base64url-encoded strings. Store them as environment variables. Do not commit the private key.

Where each key goes

Key Lives Used for
Public In your front-end code; safe to expose Passed as applicationServerKey to pushManager.subscribe()
Private On your server only Signing the token sent with each push request

A subscription is tied to the public key it was created with. Only a server holding the matching private key can send to it.

Passing the public key to the browser

The key is a base64url string. The most widely compatible approach is to convert it to a Uint8Array before subscribing:

function urlBase64ToUint8Array(base64String) {
  const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
  const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
  const rawData = atob(base64);
  const outputArray = new Uint8Array(rawData.length);
  for (let i = 0; i < rawData.length; i += 1) {
    outputArray[i] = rawData.charCodeAt(i);
  }
  return outputArray;
}

What happens when you send

For each push, your server builds a short-lived JSON Web Token signed with the private key (ES256, on the P-256 curve) and sends it in the Authorization header alongside the public key. The token carries three claims:

  • aud: the origin of the push service endpoint
  • exp: an expiry time, no more than 24 hours ahead
  • sub: a contact for the sender, as a mailto: or https: URL

The push service checks the signature against the public key stored with the subscription. The sub contact gives the push service operator a way to reach you if your server misbehaves, so use a real address.

A library does all of this for you:

import webpush from 'web-push';

webpush.setVapidDetails(
  'mailto:you@example.com',
  process.env.VAPID_PUBLIC_KEY,
  process.env.VAPID_PRIVATE_KEY
);

await webpush.sendNotification(subscription, JSON.stringify({ title: 'Hello' }));

VAPID is not the payload encryption

VAPID proves who is sending. It does not encrypt the message. Payload encryption is separate and uses the p256dh and auth keys inside each subscription. The web-push library handles both.

Rules of thumb

  • One key pair per application, shared by all your servers and environments that send to the same subscribers.
  • Do not rotate casually. If you change the key pair, existing subscriptions stop working with the new keys and every user has to be resubscribed with the new public key.
  • Back up the private key. Losing it has the same effect as rotating it.
  • Keep it server-side. Anyone with the private key and a subscription can send notifications as you.

See how web push works for where the keys fit in the full flow.