subscriptions.create in the
@mesub/node README.pro.
The slug is how your code names the plan everywhere below. You can read it in
the plan's Implement tab, where the code on this page is already filled in
with it.SUB_ and is shown
once: more on the key. Put it in your server's environment:1MESUB_API_KEY=SUB_...
1npm install @mesub/node
pnpm add @mesub/node, yarn add @mesub/node and bun add @mesub/node work
the same. It goes in dependencies: your server needs it in production.1234567891011121314151617181920212223import { Mesub } from '@mesub/node'; import { mesubRoutes } from '@mesub/node/express'; const mesub = new Mesub(); // reads MESUB_API_KEY // What the widget calls. Mount it after your own login. app.use( '/api/mesub', yourLogin, mesubRoutes({ customer: (req) => req.user ? { external_id: req.user.id } : null, }), ); // A paid route. app.get('/api/analytics', yourLogin, async (req, res) => { if (!(await mesub.hasAccess({ external_id: req.user.id }, 'pro'))) { res.status(402).json({ error: 'The Pro plan is needed.' }); return; } res.json(buildAnalytics(req.user)); });
customer must come from a session you verified, never from what the
browser sends. When it returns null, the routes answer 401. The Next.js and
NestJS examples use the SDK's guards, which answer 401 and 402 themselves;
Express has one too, requirePlan.1npm install @mesub/react
react and react-dom 18 or later. @mesub/react 0.1.x works with
the routes of @mesub/node 0.1.x: keep the two at the same minor.123456import { MesubProvider } from '@mesub/react'; import '@mesub/react/styles.css'; export function App({ children }) { return <MesubProvider endpoint="/api/mesub">{children}</MesubProvider>; }
chain="solana:mainnet".123import { SubscribeButton } from '@mesub/react'; <SubscribeButton plan="pro">Subscribe to Pro</SubscribeButton>;
/api/analytics answers for that customer.