createMeridianApp is the server entrypoint for an embedded app. Build it once, import it everywhere, and it covers the whole server side of a Meridian integration: install wiring, shop tokens, gates, webhook registration and referral capture.
app/meridian.server.ts
MERIDIAN_APP_ID, MERIDIAN_API_KEY and MERIDIAN_API_URL from the environment, or takes { appId, apiKey, baseUrl } explicitly. See Credentials.
Two more options shape gates, in SDK 3.5.0 and later:
One more decides who can refresh your shop tokens, in SDK 3.7.0 and later:
It never throws into your app
That is what makes it safe in a loader. Whether Meridian is unconfigured, unreachable, or rejecting a rotated API key, every method returns its degraded result. Failures log aconsole.error naming the likely fix, with repeated refusals handled as described below. In SDK 3.4.0 and later, missing credentials log one [Meridian] console.warn per process at construction, including in production. It names the missing variables and links to Running without Meridian. Unconfigured calls add no per-call log.
A
401, or a 403 with a credential rejection code (UNAUTHORIZED, INVALID_API_KEY or SHOP_TOKEN_EXPIRED), keeps the credentials diagnostic. Other 403 responses log the API’s code and message without blaming MERIDIAN_API_KEY.
For 403 FEATURE_NOT_AVAILABLE_FOR_PRIVATE_APP, the log explains that the app is private and usage tracking and billing are only available for public apps. There is nothing to change in MERIDIAN_API_KEY. track and trackMany return null for this refusal.
Each 401 or 403 refusal is logged once per method and error code per process, across shops and createMeridianApp instances. Responses without a code are grouped by HTTP status. This suppresses repeated logs only: later calls still reach the API and can succeed if access changes.
A rate limit (429) opens a breaker: the refused call degrades like any other failure, and every later call on the same app instance is skipped locally, with no network, until the Retry-After deadline the API sent has passed. One console.error names the deadline when the breaker opens; the skipped calls are silent. For that window gates come back with status: "rate_limited" and every feature off (unless failOpen, or the shop’s entitlements are still cached), and shop tokens are null for every shop, which is the trade for never blocking a loader on a retry. See Rate limits.
The lower-level
createMeridianServerClient and createMeridianAdminClient do throw MeridianApiError. Reach for them when you want to handle failures yourself. See Errors.afterAuth
One call in your ShopifyafterAuth hook does five things:
- Registers your Shopify webhooks. Meridian resolves the topic set itself (the managed billing topics plus whatever you routed in the dashboard), so you never declare topics. If the configuration is unreachable it falls back to the managed set, and it always unions those in, so billing webhooks can never be dropped by an outage. The configuration also names the Pub/Sub topic to publish to. When it is unreachable or names none, the SDK keeps the topic Meridian last served this app instance. Without one, an app on the production API falls back to the production topic, and an app on any other Meridian API (staging, dev, local) creates or moves no subscription and reports it in
errorsunderregistration, since that environment never reads the production topic. The nextafterAuthorregisterWebhookstries again. - Reports the authentication, which fires your install and reinstall automations. On an install or a reinstall it also drops the shop token this instance cached for the shop, because the uninstall before it revoked that token.
- Sends the shop’s profile with that report (SDK 2.10.0 and later): the email, primary domain, Shopify plan and country, read with the session’s access token in one Admin API query that needs no extra scope. From SDK 3.3.0 the same query reads the shop’s name, and the SDK also reads the shop’s primary locale in a second query, alongside the first, when the session’s
scopeincludesread_locales,write_locales,read_markets_homeorwrite_markets_home. Passing thesessionShopify hands your hook is enough, since it carriesscope. For an app hosted elsewhere, this is how the store’s name, address, domain, plan and language reach your CRM and your variables, and how a Send Email in an install automation finds its recipient with no Wait. The report waits for the reads at most 1.5 seconds and goes without them otherwise. A locale read that fails or runs out of time costs the language only: the rest of the profile still goes. Meridian keeps the profile as Shopify’s answer, so anemailyour app sends toidentifyalways wins, and the shop’s name is stored apart from thenameyour app sends there. - Reports your Shopify subscriptions. If you bill through the Shopify Billing API yourself, the SDK reads the shop’s active subscriptions with the session’s access token and sends their name, status, trial length and creation date to Meridian. That is what powers the Trial status on the Stores screen, the Trials report and the trial automations for apps Meridian does not bill. At an install this step runs before the merchant has had the chance to subscribe, so a trial they start afterwards needs one more call. See Reporting a trial the merchant just started and Free trial.
- Attributes an affiliate referral, when you passed the OAuth callback
request. See Referral attribution.
registered, updated and unchanged list topics: created on this call, rewritten because the existing subscription differed, or already correct and not touched. On a re-authentication every topic is normally unchanged, and the SDK makes no Shopify write for those. When the same process already reconciled that shop cleanly against the same topic set within the last 24 hours, a plain re-authentication makes no Shopify call at all; an install or reinstall always reconciles. The topic set itself is remembered for five minutes between authentications. errors is per topic, so one failing topic never blocks the rest. Inspect it to log or alert on failures. A rejected API key is also logged as a [Meridian] error, once per method and error code per process, so an invalid or rotated MERIDIAN_API_KEY is visible in your logs even if you never read errors. subscriptionsSynced is how many subscriptions were reported, or null when that step was skipped or failed (the failure is then in errors under the topic subscriptions). A profile read that failed or ran out of time is in errors under the topic shop-profile; the authentication was still reported, without it. A failed locale read is there too, under the same topic, with a message that names the locale.
Syncing subscriptions on demand
afterAuth runs on every authentication, and a merchant opening the app is usually not one. Shopify’s app libraries only authenticate again when a shop has no stored session, its access token expires or its scopes change, so with non-expiring offline tokens a store that installed before you upgraded the SDK stays unknown to Meridian until it reinstalls. To pick those stores up, the same step is available as its own call:
[Meridian] syncSubscriptions failed for <shop>: Shopify Admin API responded 401. and is skipped. Going through your framework’s unauthenticated admin client uses the shop’s stored refresh token and hands back a live access token. Expiring offline tokens are already mandatory for public apps created after 1 April 2026 and become mandatory for every public app on 1 January 2027, so write the backfill this way even if your app still holds non-expiring tokens.
Every call is idempotent: Meridian never fires an automation for a status it already knew, and never overwrites a trial end it already had. Each call is one Shopify Admin API request plus one Meridian request, so pace a large backfill rather than firing it all at once.
Reporting a trial the merchant just started
Approving a charge on Shopify’s confirmation screen is not an authentication, soafterAuth never sees the subscription the merchant just took. Shopify’s APP_SUBSCRIPTIONS_UPDATE webhook is all Meridian gets at that moment, and it carries no trial length, so until the shop authenticates again the store reads as active with no trial and the Trials report stays empty.
When that next authentication happens is up to your offline tokens. With expiring ones Shopify’s app libraries authenticate again on the first embedded request after the 60-minute token has lapsed, so the trial reaches Meridian within about an hour. With non-expiring ones the shop never authenticates again, and the trial never reaches Meridian by this path at all.
Sync on the route the merchant lands on after approving the charge and the trial is reported straight away, on either kind of token:
Minting shop tokens
identify handshake: it upserts the shop as an install and returns the per-shop token your browser code uses. Call it in the loader for your app shell. It is idempotent and returns the shop’s current token.
It also takes name, email, contacts, mref and mrefClickedAt. See Identify and contacts.
Cached per shop and payload
Calling this in a loader costs oneidentify per shop per token lifetime, not one per request. The app instance caches the handshake for exactly as long as Meridian says the token lives, then replaces it, and concurrent calls for the same shop share one request instead of each starting their own.
Two rules shape the cache, and both exist to protect you:
- A payload that changes always reaches Meridian.
identifyis not just a token read: it upserts contacts, attributes affiliate referrals and tags the developer database. The cache only short-circuits a call identical to one it has already sent for that shop, so nothing you send can be silently swallowed. - A live token is never rotated. The cache serves the token for its full reported lifetime and only replaces it once it has actually expired. Refreshing early would rotate it server-side, and every copy you persisted or handed to the browser would then have to refresh too.
401) is dropped on the spot. gatesForShop, track, trackMany and sendEmail then identify again and replay the call once, so a token that expired, was rotated out from under the cache or was revoked by an uninstall costs one extra request, not a degraded call. That is also how the other instances of a multi-instance app catch up with a reinstall, since only the one that ran afterAuth hears of it. A token you handed to the browser recovers there: the provider asks your server for a new one when you pass it getShopToken, and refreshes it itself otherwise.
A token the browser was refused
AMeridianProvider given getShopToken asks your server for a new token when Meridian refuses the one it holds. Answer it from a route authenticated with App Bridge’s session token, here POST /app/meridian/shop-token as in the Shopify app template for Meridian, and pass the refused token back to mintShopToken as refusedShopToken:
app/routes/app.meridian.shop-token.tsx
Cache-Control: no-store). A 503 answers a null mint, when Meridian is unconfigured or unreachable, and the provider then shows Meridian’s original refusal. The SDK ships no helper for this route, because how a request is authenticated stays your app’s decision.
When this instance has the refused token cached, it drops it and identifies the shop again, as it does for a token refused on the server, so it never hands back the token Meridian just refused. That is what keeps an app with several instances working: another instance may have replaced the token (a refresh, or an identify that renewed its chain within a day of its 7-day limit) while this one still caches it.
- A different cached token is newer than the refused one, and is served from the cache as usual.
- A token refused within 30 seconds of arriving from Meridian is served too. The rate-limit breaker is per instance and shared by every shop on it, so one session asking again and again must not turn every request into an
identify. - Concurrent requests share one
identify.refusedShopTokenis never sent to Meridian and is not part of the cache key.
mintShopTokenDetailed takes refusedShopToken too. If you persist the token yourself, never answer this route with a stored copy equal to refusedShopToken: mint with mintShopTokenDetailed({ shop, refusedShopToken }) and store what it returns. SDK 3.7.0 and later.
Once a cached token has expired, the instance replaces it through POST /auth/refresh, and calls identify instead if the refresh is refused. In SDK 3.7.0 and later, that refresh sends your API key in an X-Meridian-Api-Key header, beside the shop token in Authorization, so it keeps working when browserTokenRefresh is off.
Persisting the token yourself
The cache is per process, so on serverless or multi-instance hosting every cold start pays for a fresh handshake. If you already persist Shopify sessions, persist the shop token with them and skip the call while it is live.mintShopTokenDetailed is the same call, same cache, same best-effort contract, but returns the expiry too:
shopTokenExpiresAt is the expiry Meridian reported, verbatim, and absent when it reported none. Re-mint shortly before it, or whenever a call comes back with an expired-token error.
Resolving gates without a token
For a loader, a webhook handler or a job that just needs to know what the shop is entitled to:gatesForShop mints a token and resolves entitlements in one call. On any failure it returns gates with every feature off, unless you fail open. In SDK 3.5.0 and later, gates.status tells the cases apart:
These are the statuses of
trackDetailed, so one branch can handle both. serverClientForShop(shop) gives you the full server client instead, or null.
Caching entitlements
Every entitlements read counts as one SDK request against your app’s monthly allowance (see the plan comparison), and the first call for a shop also spends one on identify. An app that checks access on every render of a loader or a customer account block would spend its whole monthly allowance on reads that mostly return the same snapshot. So in SDK 3.5.0 and later,gatesForShop keeps each shop’s entitlements in memory for 30 seconds:
- Only a snapshot Meridian answered is kept. A failure is never cached, so the next call tries again.
- Concurrent calls for the same shop share one read.
- A snapshot still within its 30 seconds is served while the breaker is open, so a short rate limit does not turn a paying shop’s features off.
- These clear the shop’s snapshot, so the next call reads again: a recorded
track,trackMany,trackDetailedortrackManyDetailed, a successfulsyncSubscriptions, everyafterAuth, and everysubscribe,cancelSubscription,undoScheduledChange,updateUsageCap,trackortrackManymade through the clientserverClientForShopreturns, even one that failed. gatesForShop(shop, { fresh: true })skips the cache and keeps what it read. Use it where a change must show at once, such as the route the merchant returns to after approving a plan.- Up to 250 shops are kept per instance, least recently used out.
entitlementsCacheTtlMs to change the 30 seconds, or to 0 to read from Meridian on every call (the behaviour before 3.5.0):
- Writes it does not see. A plan change made in the browser (the pricing page calls Meridian directly) or by another instance of your app does not clear this cache. The server then sees the new plan within the TTL, at most 30 seconds by default. Use
{ fresh: true }where that matters. - Serverless and multi-instance hosting. Each instance has its own cache, cold after every cold start, and a write clears it only on the instance that made it.
Tracking usage events
Record one event withtrack, or send up to 50 events together with trackMany:
timestamp (with an idempotencyKey, which a timestamp requires): a delayed sync, a replay after an incident. See Dated events for what that does to billing and automations. An invalid Date is a failure like any other and resolves null.
Both methods resolve the shop’s token through the identify cache and return the API result unchanged on success. They are best-effort: you receive null when Meridian is unconfigured, identify fails, tracking fails, or the rate-limit breaker is open. A failed call logs one console.error, except repeated 401 or 403 refusals with the same method and code in this process. Missing credentials warn once at construction in SDK 3.4.0 and later. Unconfigured calls add no per-call log, and calls skipped by the breaker add no log. shop selects the token and is not sent in the event payload.
Knowing whether an event was recorded
In SDK 3.4.0 and later,trackDetailed and trackManyDetailed take the same inputs as track and trackMany and return MeridianTrackOutcome<TrackEventResult> or MeridianTrackOutcome<TrackManyResult>. The type is exported from @the-meridian/sdk/server:
recorded carries the API result unchanged. unconfigured makes no network call. rate_limited covers both an API 429 and a call skipped locally, with the breaker’s ISO deadline in retryAt. failed carries the original MeridianApiError when the API answered, including its status and code, or an Error for other failures. These methods never throw and use the same cache, retries, breaker and logging as the original methods, with the name of the method you called.
status: "recorded". The original track and trackMany keep their result-or-null contract.
The underlying client retries 429 and 5xx only when track has an idempotencyKey, or every event in trackMany has one. The facade adds one retry of its own, for a refused shop token: a 401 on the cached token, or a SHOP_TOKEN_REVOKED, drops that token, identifies again and replays the call once. The replay is safe because a 401 is answered before the request does anything. A token the facade has just minted is not retried. A final 429 opens the shared breaker.
Use the server client’s track when you need to catch errors yourself: it still throws on failure. See Events for event fields and Sending events in batch for batch semantics.
Re-registering webhooks
afterAuth only runs when a shop has no session, its offline token is expiring or its scopes changed. A topic you add in the dashboard is therefore not registered on an already-installed shop until it next authenticates, and with non-expiring offline tokens, that may never happen.
registerWebhooks reconciles a shop immediately, with the same topic set and no re-install:
afterAuth remembered.
Capturing referrals
captureReferral(request, options?) reads an affiliate code off a landing-page request and returns the cookies to set. It never throws, so it is safe to await on a page whose only job is to render. See Referral capture.
What it exposes
lifecycle.notifyAuthenticated({ shopDomain }) reports an install or re-auth on its own, if you want that step without the rest of afterAuth. Uninstall does not go through it. That reaches Meridian as the APP_UNINSTALLED webhook the SDK registered.