Prompt
How do I send web push notifications from an API?
Latest observation
To send web push notifications from an API, you generally use the Web Push protocol with a push service like Firebase Cloud Messaging, Mozilla Autopush, or the browser’s native push service.
High-level flow
-
Client subscribes
- Your web app asks the user for notification permission.
- It registers a service worker.
- It creates a PushSubscription containing:
- an endpoint URL
- public keys / encryption info
-
Send subscription to your backend
- Your API stores that subscription object for the user.
-
Your backend sends a push request
- When you want to notify the user, your API makes an authenticated HTTP request to the subscription’s endpoint.
- The payload is encrypted.
- The browser/vendor push service delivers it to the service worker.
-
Service worker displays the notification
- The browser receives the push event and shows the notification.
What you need
- A browser app with:
- service worker
- notification permission handling
- push subscription code
- A backend/API that:
- stores subscriptions
- sends push requests
- Web Push encryption keys:
- VAPID public/private key pair
Typical implementation steps
1) Generate VAPID keys
Use a library or tool for your stack.
Example with Node.js:
npx web-push generate-vapid-keys
You’ll get:
- public key
- private key
2) Subscribe the browser client
In the browser:
const registration = await navigator.serviceWorker.register('/sw.js');
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY),
});
Send subscription to your API and store it.
3) Send push from your API
Your backend sends a POST request to the subscription’s endpoint with the encrypted payload and VAPID authentication.
Example in Node.js using web-push:
import webpush from 'web-push';
webpush.setVapidDetails(
'mailto:you@example.com',
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
const payload = JSON.stringify({
title: 'Hello',
body: 'You have a new message',
});
await webpush.sendNotification(subscription, payload);
4) Handle it in the service worker
sw.js:
self.addEventListener('push', event => {
const data = event.data?.json() || {};
const title = data.title || 'Notification';
const options = {
body: data.body || '',
};
event.waitUntil(self.registration.showNotification(title, options));
});
If you want to do it as a pure API service
If your goal is “I want my backend API to send notifications whenever it receives some event,” then your API simply:
- receives the event
- looks up the user’s saved push subscription
- sends the notification request to the push endpoint
You do not send directly to the user’s browser. You send to the push service endpoint from the subscription.
Important notes
- You can only send notifications to users who opted in.
- Push payloads must be encrypted.
- Subscriptions can expire or become invalid; handle
410 Gone/404 Not Foundby removing them. - Use HTTPS in production.
- The notification must be shown by the service worker in response to the push event.
Recommended libraries
- Node.js:
web-push - Python:
pywebpush - Go: web push libraries available, but fewer mainstream options
- Java:
nl.martijndwars:web-push
Minimal architecture
Browser -> subscribes -> sends subscription to API
API -> stores subscription
API -> sends push request to subscription endpoint
Push service -> browser service worker
Service worker -> showNotification()
If you tell me your backend language/framework, I can give you a working end-to-end example.