# Add memberships to an EmDash site

EmDash Memberships is an independent MIT-licensed plugin by Simple Things Ltd. The directory guide explains setup. The npm package supplies access rules, reader sessions, Stripe Checkout and billing. This walkthrough uses the published package 0.5.0, EmDash 1.6.0 and Astro 6.

[Illustrated tutorial and film](/tutorial) · [Source of the working theme routes](/examples.md) · [Directory guide](https://plugins.emdashcms.com/plugins/@emdashmemberships.bsky.social/emdash-memberships) · [Package on npm](https://www.npmjs.com/package/@simplethingsltd/emdash-memberships/v/0.5.0)

## Start with the publisher's project

Inspect the package manager, Astro configuration, EmDash collections, theme routes, hosting adapter, database, search and email provider. Use what the publisher has already told you about their readers. If the content or intended offer is unclear, ask one focused question. Work locally through owner review.

On an existing site, protect the chosen material and preserve its editor workflow. Use a clearly identified sample article on a new site. If you need to scaffold EmDash first, follow the core instructions. [EmDash start guide](https://emdashcms.com/start.md)

## Install and register

Use Node 22.16 or newer, Astro 6 with server rendering, EmDash 1.4 or newer within major version 1, and React 18 or 19. Use the project's package manager.

```sh
pnpm add @simplethingsltd/emdash-memberships@0.5.0
```

Import `emCashPlugin` from the package in `astro.config.mjs`, then append `emCashPlugin()` to the existing `emdash({ plugins: [...] })` array. Keep the site's database, storage and adapter. The runtime plugin ID is `em-cash`.

## Wire the theme

Fetch the companion Markdown at `/examples.md` on the same origin as this guide. It contains the exact code used by the working tutorial. Adapt its layout, collection and paths to the publisher’s site.

- **Membership page**. Handle the checkout return and reader cookies before rendering. List plans with `listMembershipPlans` and use `EmCashSubscribe`. With `verificationMode="redirect"`, the account route must handle purchase verification.
- **Account page**. Implement existing-reader sign-in, first-time purchase verification, sign-out and billing management. Preserve `purchase=1` on the form POST and call `requestSignInLink` with `purpose: "purchase"` for a first-time buyer. Confirm an emailed token on a same-site POST. Use the plugin's cookie instructions and return-path helper. A publisher grant and a paid subscription both convey access. Use billing memberships for the Stripe portal controls.
- **Article page**. Fetch published content through EmDash. Pass the verified CMS preview state into `resolveProtectedPostRequest`, then use `selectPostResponseBlocks` before rendering Portable Text. Save the plugin's cookies and honor its headers and redirects. Render `EmCashPaywall` when a reader needs access. Use the same boundary for metadata and other public representations. Reader-specific pages and redirects need `Cache-Control: private, no-store`.
- **Editable copy**. Keep the title, public excerpt and full body in EmDash. Use the database entry ID for API calls and the slug for reader links. Pass public content queries’ cache hints to Astro. Disable shared caching on pages whose response depends on the reader.

## Configure sandbox billing and email

Set `STRIPE_SECRET_KEY` to a sandbox key and `PUBLIC_SITE_URL` to the actual local origin. Set `EM_CASH_CURRENCY` for the site's currency. Start a Stripe CLI listener for the same sandbox. For a site running on port 4321, use the command below and take the listener's signing secret as `STRIPE_WEBHOOK_SECRET`. Restart the site after setting the environment. Keep secrets in the server environment.

```sh
stripe listen --forward-to http://localhost:4321/_emdash/api/plugins/em-cash/stripe-webhook
```

For a registered Stripe webhook, select checkout.session.completed, checkout.session.async_payment_succeeded, customer.subscription.created, customer.subscription.updated, customer.subscription.deleted, customer.subscription.paused, customer.subscription.resumed, invoice.paid, invoice.payment_failed and charge.refunded. Check the product README for the Stripe API version and billing portal settings. The development email provider prints local sign-in links. Production needs an email provider and a verified sender.

## Set up the offer and search

Open EmDash Admin → Plugins → em-cash. For a monthly collection membership, choose the collection, set its target slug, the Membership model, the price, the preview word count and the plan name. A collection rule covers the entire collection. Put a single sample in its own collection if that's the access boundary the publisher wants.

On EmDash 1.6.0, the 0.5.0 admin form accepts £5.01 but rejects £5.00 because of its numeric input step. The filmed example uses £5.01. For a round price with that release, set a stable ID in the supported `EM_CASH_SEED_PLANS` environment value before first start. Use your own collection and currency, and choose either seeded or manually created plans for that offer.

```json
[{"id":"field-notes-monthly","name":"Field Notes","scopeType":"collection","scopeKey":"posts","price":5,"interval":"month"}]
```

Keep paid text out of search indexes, listings and feeds. Create `em-cash-search.json` in the project root, listing each gated collection and the public fields that may be indexed.

```json
{"collections":[{"collection":"posts","searchable":["title","excerpt"],"neverSearchable":["content"]}]}
```

Install the optional peer `better-sqlite3` in a local SQLite project. Run `pnpm exec em-cash-protected-search local` after seeding, with `EM_CASH_DATABASE_URL` exported if the database is elsewhere. For Cloudflare D1, also configure the Wrangler file and D1 binding from the package README, and use the corresponding staging or production target at deployment.

## Test two readers and hand back the local site

1. As a visitor, read the public excerpt. Verify that a distinctive passage from the paid body is absent from signed-out HTML, metadata, public search snippets and feeds.
2. In the plugin's Members page, give the first address access to the plan. Request its email link from the account page, confirm sign-in and reopen the article. Verify the full body and the zero-revenue grant.
3. With a different address in a fresh browser session, choose Subscribe, verify the email, then subscribe through Stripe's hosted sandbox Checkout. Use the published 4242 test Visa, a future expiry and a test CVC. Confirm that the signed webhook was acknowledged, the subscription is active, and the correct reader can read the paid body. To isolate the webhook in a rehearsal, intercept the checkout return in the test browser and revisit the article directly. [Stripe test cards](https://docs.stripe.com/testing)
4. Verify that another signed-out session still gets only the excerpt. Show the publisher the local site, editable content, access rule and results. Get their review before deployment or opening live billing.
