Skip to content

About

EntitleHub SDK — one entitlement API across App Store, Google Play, Stripe & web (browsers, React Native, Expo, Node). Zero-dep, MIT.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

@entitlehub/sdk

npm version types included zero dependencies license MIT

One entitlement API across App Store, Google Play, Stripe, and web, for browsers, React Native, Expo, and Node. Stop checking raw product IDs; ask "what is this user entitled to right now?"

Docs: entitlehub.com/docs · REST API · Migrating from RevenueCat

npm install @entitlehub/sdk

Zero dependencies. Uses the global fetch (Node 18+, all modern browsers, React Native, Expo).

Client (in your app) (publishable key)

Reads only. Safe to ship in your app. Never put a secret (sk_) key here.

import { EntitleHub } from "@entitlehub/sdk";

const eh = new EntitleHub({
  apiKey: "pk_live_…",     // your publishable key
  appUserId: user.id,      // your own stable user id
});

const info = await eh.getCustomerInfo();
if (info.isActive("pro")) {
  unlockProFeatures();
}

// full detail when you need it
const pro = info.active["pro"];
// → { entitlement: "pro", status: "active", store: "app_store", expires_at, will_renew, … }

React / React Native

const eh = new EntitleHub({ apiKey: "pk_live_…", appUserId: user.id });

useEffect(() => {
  const unsub = eh.addCustomerInfoUpdateListener((info) => setPro(info.isActive("pro")));
  eh.getCustomerInfo();               // triggers the listener
  return unsub;
}, []);

After your own login / logout

await eh.logIn(newUserId);   // switches user, refreshes, clears cache
eh.logOut();                 // forget cached info

API (client)

Method Returns Notes
getCustomerInfo({ fetchPolicy }) CustomerInfo cache-first (default) or network-only. Cached cacheTtlMs (5 min default).
isEntitled(id) boolean Convenience over cached info.
checkEntitlement(id) CheckResult Always hits the server; includes a reason when inactive.
getOfferings() Offerings Entitlements + products, for building a paywall.
addCustomerInfoUpdateListener(fn) () => void Returns an unsubscribe.

Server (in your backend) (secret key)

Report purchases and grant entitlements. Server only.

import { EntitleHubServer } from "@entitlehub/sdk";

const eh = new EntitleHubServer({ apiKey: process.env.ENTITLEHUB_SECRET_KEY! });

// After the store purchase is confirmed on-device, report it from your server.
// Google Play: validated (EntitleHub verifies the token with your Play service account):
const info = await eh.reportPurchase(userId, {
  store: "play", storeProductId: "pro_monthly",
  purchaseToken: playToken, isSubscription: true,
});
info.isActive("pro"); // → true

// Apple StoreKit 2: validated (product / environment read from the signed JWS):
await eh.reportPurchase(userId, { store: "app_store", storeProductId: "", signedTransaction: jws });

// Trusted server-report (no store validation, only if you validated the receipt elsewhere):
await eh.reportPurchase(userId, { store: "app_store", storeProductId: "pro_monthly" });

// Grant directly (promo / comp / support), no purchase:
await eh.grantEntitlement(userId, "pro", { durationDays: 30 });
Method Returns
reportPurchase(userId, { store, storeProductId, purchaseToken?, isSubscription?, signedTransaction?, isSandbox?, transactionId? }) CustomerInfo
grantEntitlement(userId, id, { durationDays?, isSandbox? }) CustomerInfo
getCustomerInfo(userId) / check(userId, id) CustomerInfo / CheckResult

Catalog: entitlements, products, offerings

Set up and change your catalog from code. Refs take the id or the key / SKU / identifier you already have. Every create has an ensure twin that returns the existing row instead of failing, so a setup script can run on every deploy.

await eh.entitlements.ensure({ key: "pro", name: "Pro" });

await eh.products.ensure({
  store: "stripe", storeProductId: "price_1Q...", type: "subscription", duration: "P1M",
  priceMicros: 9_990_000, entitlements: ["pro"],
});

// Remap (future purchases only; access already granted is untouched):
await eh.products.update({ store: "play", storeProductId: "pro_monthly" }, { entitlements: ["pro", "team"] });
await eh.entitlements.attachProducts("team", ["price_1Q..."]);

// Retire a SKU you no longer sell. Existing subscribers keep access and renewals:
await eh.products.archive("price_old");

await eh.offerings.create({
  identifier: "default", isCurrent: true, metadata: { headline: "Go Pro" },
  packages: [{ identifier: "monthly", product: "price_1Q..." }],
});
Namespace Methods
eh.entitlements list get create ensure update delete products attachProducts detachProducts archive unarchive
eh.products list get create ensure update delete attachEntitlements detachEntitlements archive unarchive
eh.offerings list get create update delete archive unarchive packages addPackage updatePackage deletePackage

Full reference: entitlehub.com/docs/catalog-api.

Already have an IAP setup?

Keep making the actual store purchase with your existing IAP flow (StoreKit / Play Billing / expo-iap / react-native-iap), then reportPurchase(...) from your server and read entitlements with getCustomerInfo(). See the full guide at entitlehub.com/docs.

Errors

Every failure throws an EntitleHubError with .status and .code (auth | http | network | config). When the API sends its own machine-readable code it is on .apiCode (e.g. product_exists, ambiguous_product, rate_limited), with the full payload on .body.

import { EntitleHubError } from "@entitlehub/sdk";
try { await eh.getCustomerInfo(); }
catch (e) { if (e instanceof EntitleHubError && e.code === "auth") relogin(); }

Self-hosting

Point the SDK at your own EntitleHub with baseUrl:

new EntitleHub({ apiKey, appUserId, baseUrl: "https://entitlements.yourco.com/v1" });

About

EntitleHub SDK — one entitlement API across App Store, Google Play, Stripe & web (browsers, React Native, Expo, Node). Zero-dep, MIT.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages