Skip to content

SDK Reference

Browser package: @gesher/sdk

The SDK never holds your Gesher API key. It requests notification permission, registers your service worker, creates a PushSubscription, and POSTs the result to your backend URLs.

Installation

bash
npm install @gesher/sdk
typescript
import { GesherClient } from '@gesher/sdk'

GesherClient constructor

typescript
new GesherClient(config: GesherConfig)
ParameterTypeRequiredDefaultDescription
subscribeEndpointstringYesURL of your backend proxy for subscribe
unsubscribeEndpointstringYesURL of your backend proxy for unsubscribe
vapidPublicKeystringNo(see below)VAPID public key for PushManager.subscribe
vapidKeyEndpointstringNohttps://api.gesher.pro/vapid-keyURL used to fetch the key when vapidPublicKey is omitted
serviceWorkerPathstringNo/gesher-sw.jsScript URL passed to navigator.serviceWorker.register
serviceWorkerScopestringNo/Registration scope

If vapidPublicKey is omitted, the client fetches the key from vapidKeyEndpoint. The Gesher API returns { "publicKey": "..." }. If you rely on auto-fetch, ensure your deployed API and SDK versions agree on the JSON field name; setting vapidPublicKey explicitly (copy the value from GET /vapid-key) is the most reliable approach.

Methods

MethodReturnsDescription
subscribe(userId)Promise<GesherResult>Permission → service worker → push subscription → POST to subscribeEndpoint
unsubscribe(userId?)Promise<GesherResult>Unsubscribe in the browser → POST to unsubscribeEndpoint with { userId, endpoint }
getState()Promise<GesherSubscriptionState>'subscribed' | 'unsubscribed' | 'denied' | 'unsupported'
isSubscribed()Promise<boolean>true if getState() is 'subscribed'

unsubscribe(userId?) uses the last userId passed to subscribe when userId is omitted.

Types

typescript
interface GesherResult {
  ok: boolean
  error?: string
}

type GesherSubscriptionState = 'subscribed' | 'unsubscribed' | 'denied' | 'unsupported'

interface GesherSubscriptionPayload {
  userId: string
  subscription: { endpoint: string; keys: { p256dh: string; auth: string } }
}

interface GesherUnsubscribePayload {
  userId: string
  endpoint: string
}

GesherNotificationPayload exists in the package for typing payloads you send server-side to /notify (not used by the browser client).

Service worker file

Ship the template from @gesher/sdk/sw (file gesher-sw.js) on your origin. See Service Worker.

Gesher — managed web push by Otomator