The @litepush/sdk package
@litepush/sdk is the npm package for LitePush. One install gives you two
clients:
- a browser client (LitePushClient) for subscribing visitors to push, and
- a server client (LitePush) for sending broadcasts and managing groups/subscribers from your backend.
Prefer zero build steps? The <script> tag drop-in does the browser half without installing anything.
npm install @litepush/sdk
Client side (browser)
Construct a client with your project ID + VAPID public key, then subscribe the visitor from a user gesture (click/tap). The client is side-effect-free, so it's safe to import under SSR.
import { LitePushClient, canSubscribe } from "@litepush/sdk";
const lp = new LitePushClient({
projectId: "prj_YOUR_PROJECT_ID",
vapidKey: "YOUR_VAPID_PUBLIC_KEY",
});
document.getElementById("enable-notifs")?.addEventListener("click", async () => {
if (!canSubscribe()) return; // unsupported browser
const result = await lp.subscribe({ userId: currentUser.id });
if (result) console.log("subscribed!", result.id);
});
You still need the service worker. Host
litepush-sw.jsat your origin so it's reachable at<origin>/litepush-sw.js— installing the package doesn't provide it (a service worker must be served same-origin). Grab it from https://litepush.dev/litepush-sw.js or your dashboard. Full method list: Browser client.
Server side
Use the server client with your secret API key (never expose it in browser
code). Zero dependencies — it uses the global fetch.
import { LitePush, LitePushApiError } from "@litepush/sdk/server";
const litepush = new LitePush(process.env.LITEPUSH_API_KEY!);
try {
const { broadcast_id } = await litepush.broadcasts.create({
target: { type: "all" },
notification: {
title: "New post on the blog",
body: "We just shipped web push.",
url: "https://myblog.com/posts/web-push",
},
});
console.log("queued", broadcast_id);
} catch (err) {
if (err instanceof LitePushApiError) {
// err.code is stable (e.g. "monthly_push_limit_reached") — branch on it.
console.error(err.code, err.status, err.message);
}
}
The server client also covers groups and GDPR subscriber management — full method list on the Server client page.
Reference
- Browser client —
LitePushClient,canSubscribe. - Server client —
LitePush,LitePushApiError. - Types — shared request/response types.