Skip to main content

Overview

The Push API enables web push notifications using the Web Push Protocol with VAPID authentication. Subscriptions are tied to device sessions.

Get VAPID Public Key

curl https://ave.day/api/push/vapid-key
{
  "enabled": true,
  "publicKey": "BEL1...xyz"
}
Returns the VAPID public key for push notification subscriptions. This is a public endpoint that does not require authentication.
enabled
boolean
required
Whether push notifications are configured on the server
publicKey
string
Base64-encoded VAPID public key (P-256 ECDSA key). Required for creating push subscriptions. Only present when enabled is true.
message
string
Status message when push is not configured

Subscribe to Push

curl -X POST https://ave.day/api/push/subscribe \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "subscription": {
      "endpoint": "https://fcm.googleapis.com/fcm/send/...",
      "keys": {
        "p256dh": "BEL1...xyz",
        "auth": "abc123..."
      }
    }
  }'
{
  "success": true
}
Registers a push notification subscription for the current device. Requires authentication.
subscription
object
required
Web Push subscription object from PushManager.subscribe()
success
boolean
required
Returns true when subscription is saved
The subscription is stored on the device record associated with the current session. If no device ID is available in the session, the subscription will not be saved but the request will still succeed.

Unsubscribe from Push

curl -X POST https://ave.day/api/push/unsubscribe \
  -H "Authorization: Bearer YOUR_TOKEN"
{
  "success": true
}
Removes the push notification subscription for the current device. Requires authentication.
success
boolean
required
Returns true when subscription is removed
This endpoint only removes the subscription from the server. You should also call subscription.unsubscribe() on the browser’s PushSubscription object.

Get Push Status

curl https://ave.day/api/push/status \
  -H "Authorization: Bearer YOUR_TOKEN"
{
  "subscribed": true,
  "enabled": true
}
Returns the push notification subscription status for the current device. Requires authentication.
subscribed
boolean
required
Whether the current device has an active push subscription
enabled
boolean
required
Whether push notifications are configured on the server

Push Notification Flow

  1. Check if push is enabled: Call GET /api/push/vapid-key to verify server support
  2. Request permission: Use browser Notification.requestPermission()
  3. Get public key: Retrieve VAPID public key from vapid-key endpoint
  4. Subscribe: Create subscription with pushManager.subscribe() using the public key
  5. Register subscription: Send subscription to POST /api/push/subscribe
  6. Receive notifications: Service worker receives push events
  7. Unsubscribe (optional): Call POST /api/push/unsubscribe and subscription.unsubscribe()
// In your service worker
self.addEventListener('push', (event) => {
  const data = event.data?.json() ?? {};
  
  event.waitUntil(
    self.registration.showNotification(data.title || 'Notification', {
      body: data.body,
      icon: data.icon,
      badge: data.badge,
      data: data.data
    })
  );
});

self.addEventListener('notificationclick', (event) => {
  event.notification.close();
  
  event.waitUntil(
    clients.openWindow(event.notification.data?.url || '/')
  );
});

Build docs developers (and LLMs) love