When building a web app with Memberstack, you will likely need details about your Memberstack app, such as its plans, custom fields and branding. The Memberstack DOM package reads this app data with getApp(), getPlans() and getPlan(), so you can use it to build customized experiences in your front-end code. None of these methods need a logged-in member.
View the full DOM Package documentation for more guidance.
Note: The Memberstack DOM Package can be accessed through the Webflow Package as well by using window.$memberstackDom.getApp() instead of memberstack.getApp().
1) Retrieving Application Data
1. Get App Information
Use the getApp method to get overall information about your Memberstack app. It resolves to an object with your app under data:
const { data: app } = await memberstack.getApp();
console.log(app.id); // your app ID
console.log(app.name); // your app name
console.log(app.mode); // "live" or "sandbox" (Live Mode or Test Mode)
The app object includes:
-
id: your app ID -
name: your app name -
mode:"live"or"sandbox" -
plans: your active plans (the same listgetPlans()returns) -
customFields: your custom fields, each with itskey,label,order,hiddenandvisibilitysettings. Fields restricted to admins are left out. These are the field settings, not a member's values. -
authProviders: the social login providers you have enabled, each withprovider,nameandicon -
contentGroups: your gated content groups -
branding: your logo (logo), modal colors (colors.lightMode) and modal style settings (style)
2. Fetch Specific Plan Details
You can also retrieve a single plan by passing its ID as planId to the getPlan method:
try {
const { data: plan } = await memberstack.getPlan({ planId: "pln_premium-abc123" });
console.log(plan.name, plan.description, plan.prices);
} catch (error) {
// An unknown plan ID throws an error with code "plan-not-found"
console.error(error.code, error.message);
}
The plan has its id, name, description (can be null), status, priority and prices. A plan ID that doesn't exist throws an error with the code plan-not-found. Unlike getPlans(), getPlan() also returns archived plans, with status "INACTIVE".
3. Get All Plans
To retrieve every active plan, use the getPlans method. The plans are an array under data. This includes free plans set to Restrict to admin, which members can't add themselves; nothing in the response marks them, so leave them out of a pricing page by ID:
const { data: plans } = await memberstack.getPlans();
plans.forEach((plan) => {
console.log(plan.id, plan.name, plan.prices.length);
});
Each plan looks like this. Free plans have an empty prices array, and so does a paid plan you haven't added a price to yet. amount is a number in the currency's main unit (19.99 is $19.99). A price's type is SUBSCRIPTION or ONETIME. For subscriptions, interval.type is DAILY, WEEKLY, MONTHLY or YEARLY; for one-time prices the interval doesn't apply and can still hold a value such as MONTHLY, so check type first:
{
"id": "pln_premium-abc123",
"name": "Premium",
"description": null,
"priority": null,
"prices": [
{
"id": "prc_premium-monthly-abc123",
"amount": 19.99,
"interval": {
"type": "MONTHLY",
"count": 1
},
"name": "Premium Monthly",
"type": "SUBSCRIPTION",
"status": "ACTIVE",
"currency": "usd"
}
]
}
2) Using the Application Data
Once you have fetched this app data from Memberstack, you can use it to build customized experiences in your web app's frontend.
For example, you could:
-
Dynamically generate a pricing page: Loop through your plans and show each one's name, description and price:
const { data: plans } = await memberstack.getPlans(); const units = { DAILY: "day", WEEKLY: "week", MONTHLY: "month", YEARLY: "year" }; plans.forEach((plan) => { // Deactivated prices stay in the list with status "INACTIVE" const price = plan.prices.find((p) => p.status === "ACTIVE"); // Every price is deactivated: nothing to buy, so skip the plan if (!price && plan.prices.length) return; let priceText = "Free"; if (price) { priceText = new Intl.NumberFormat("en-US", { style: "currency", currency: price.currency }).format(price.amount); // Only subscriptions repeat. A one-time price's interval doesn't apply, even if it has a value. if (price.type === "SUBSCRIPTION" && price.interval.type) { const count = price.interval.count || 1; priceText += count > 1 ? ` every ${count} ${units[price.interval.type]}s` : ` / ${units[price.interval.type]}`; } } const card = document.createElement("div"); card.className = "plan"; const name = document.createElement("h3"); name.textContent = plan.name; const description = document.createElement("p"); description.textContent = plan.description || ""; // description can be null const priceLine = document.createElement("p"); priceLine.textContent = priceText; card.append(name, description, priceLine); document.getElementById("plans").append(card); }); -
Show the app logo: The App image you upload under Design → Modal editor (or Settings → Application) is at
branding.logo:const { data: app } = await memberstack.getApp(); // branding.logo is your logo's URL, or null if you haven't uploaded one if (app.branding.logo) { document.getElementById("logo").src = app.branding.logo; } -
Apply branding colors: The colors you set under Design → Modal editor are in
branding.colors.lightMode, for exampleprimaryButton,textandlinks. Set them as CSS custom properties from JavaScript:const { data: app } = await memberstack.getApp(); const colors = app.branding.colors.lightMode; const root = document.documentElement; // Any color can be missing, so only set the ones that are there if (colors.primaryButton) root.style.setProperty("--primary", colors.primaryButton); if (colors.text) root.style.setProperty("--text", colors.text); if (colors.links) root.style.setProperty("--links", colors.links); -
Render custom fields:
getApp()gives you each field's label. A logged-in member's values are inmember.customFields, under each field'skey:const { data: app } = await memberstack.getApp(); const { data: member } = await memberstack.getCurrentMember(); // null when no one is logged in const container = document.getElementById("custom-fields"); app.customFields.forEach((field) => { const label = document.createElement("label"); label.textContent = field.label; const value = document.createElement("div"); value.textContent = member?.customFields?.[field.key] ?? ""; container.append(label, value); });
The key is that this data comes directly from your Memberstack configuration, so when you make changes in the dashboard, they show up the next time your page calls these methods, without you updating any values in your code.
Check out the other articles in this series:
- Getting Started with the Memberstack DOM Package (Front-end API)
- Working with Memberstack DOM Package Modals
- Managing App Data with the Memberstack DOM Package
- Member Authentication with the Memberstack DOM Package
- Member Billing and Checkout with the Memberstack DOM Package
- Member Management with the Memberstack DOM Package
- Events and Webhooks with Memberstack DOM
Comments
No comments yet. Start the conversation below.
Please sign in to leave a comment.