Prompt

How do I send web push notifications from an API?

Technology / API Platforms1 observationLast seen Oct 14, 2026

Latest observation

Oct 14, 2026OpenAI APIWeb search: off

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

  1. 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
  2. Send subscription to your backend

    • Your API stores that subscription object for the user.
  3. 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.
  4. 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 Found by 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.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.