Course contentsShow
← Web Development
Lesson 2348 of 2,98242. Progressive Web AppsFree lesson

VAPID Keys and Application Server Keys

Understanding Voluntary Application Server Identification, generating keys, and passing them to subscribe().

VAPID Keys and Application Server Keys

You'll learn what VAPID keys are, why they're essential for push notifications, and how to generate and use them with your push subscriptions.

What Are VAPID Keys?

VAPID stands for Voluntary Application Server Identification. Think of VAPID keys as a secure handshake between your server and push services—they prove your server's identity when sending push notifications.

When a user subscribes to push notifications, their browser connects to a push service (like Google's FCM or Mozilla's push service). The push service needs to know that notifications claiming to be from your app are actually from your authorized server, not an imposter. VAPID keys provide this cryptographic proof.

How VAPID Keys Work

VAPID uses a public/private key pair:

  • Public key: Shared with the browser during subscription—it's safe to expose
  • Private key: Kept secret on your server—used to sign push messages

The analogy: Your public key is like your mailing address (anyone can have it), while your private key is like your signature (only you can produce it).

Generating VAPID Keys

You generate VAPID keys once and reuse them. Libraries like web-push can create them:

// Example generation (run once, server-side)
const webpush = require('web-push');
const vapidKeys = webpush.generateVAPIDKeys();
// Store these securely - you'll need them permanently

Using Keys with subscribe()

Pass your public key as the applicationServerKey when creating a subscription:

const publicKey = 'YOUR_PUBLIC_VAPID_KEY_HERE';
const subscription = await registration.pushManager.subscribe({
  userVisibleOnly: true,
  applicationServerKey: publicKey
});

The browser embeds this key in the subscription object, linking it to your server's identity.

Key Takeaway: VAPID keys authenticate your server to push services—generate them once, keep the private key secret, and pass the public key to subscribe() as the applicationServerKey.