Tied-agent walkthrough (Next.js + BFF)
Building a Next.js app as a tied-agent tenant: an OIDC login, a server-side session holding the tokens, and a tenant-gated proxy to the Marketplace API.
Build a Next.js app that embeds Assetera's catalog for your customers, as a tied-agent tenant. Your app ships a thin Backend-for-Frontend (BFF): a server layer, under your control, that holds all tokens and calls Assetera on the browser's behalf.
One rule above all: the browser never receives a general Assetera API access or refresh token. Those tokens live server-side; the browser holds only an opaque, HttpOnly session cookie. A purpose-restricted, short-lived SDK token is the explicit exception described below.
Under a tied-agent arrangement, Assetera runs KYC/AML and you operate under its licence.
Before you start
| Read first | For |
|---|---|
| Embed the marketplace in your app | whether this pattern is the right one, before any code |
| Tenancy and responsibility | reliance against tied agent, and who carries what |
| Authentication | the OIDC grants and the token contract, which this page links to rather than repeating |
The complete tied-agent flow
Before you integrate, your organization signs up with Assetera. Assetera provisions a tenant-scoped OIDC client, MetaKYC SDK credentials, allowed redirect origins, and, if you offer it, the Assetera Wallet URL. These are organization credentials, not end-user credentials.
The Assetera Identity sub is the join key. Do not create a separate random KYC user ID. Use the
signed-in customer's sub as MetaKYC's externalRefId.
To offer the Assetera Wallet, add NEXT_PUBLIC_ASSETERA_WALLET_URL (the wallet URL from Assetera) and follow
Assetera Wallet. The wallet opens in its own pop-up. Your BFF handles no
wallet token.
Never accept a user ID for KYC from an untrusted browser body. Read sub from the validated server-side
session. This prevents one customer from opening or resuming another customer's onboarding workflow.
Runtime shape
For ordinary Marketplace API calls there are three parties: the browser, your BFF, and Assetera Identity plus the Marketplace API. The browser talks to your BFF and does not receive the Marketplace API token. The onboarding SDK uses its own restricted browser-token boundary, as shown in the complete flow above.
The example project
A minimal App Router layout: a page, three auth route handlers, one catch-all proxy, and two lib modules.
Snippets below are correct in shape, not a complete app. They use the standard openid-client and
iron-session libraries; swap in your own equivalents. Environment hosts are never hardcoded; read them
from env (ASSETERA_ISSUER, etc.).
Walkthrough
Register your OIDC client
During onboarding, Assetera provisions you a confidential OIDC client bound to your tenant. You
register your redirect URI (/api/auth/callback) and receive a client ID plus a credential: a secret, or
better, a key pair for private_key_jwt (Assetera stores only your public key, so no shared secret crosses
the boundary). See Partner onboarding.
Your BFF discovers the issuer's endpoints from OIDC discovery:
// lib/oidc.ts
import * as client from "openid-client";
// e.g. https://auth.<base_domain>/realms/assetera
const issuerUrl = new URL(process.env.ASSETERA_ISSUER!);
export async function getConfig() {
return client.discovery(issuerUrl, process.env.ASSETERA_CLIENT_ID!, {
client_secret: process.env.ASSETERA_CLIENT_SECRET!,
});
}Login: redirect to Assetera (Auth Code + PKCE)
The login route generates a PKCE verifier, stashes it in the session, and redirects the browser to Assetera's hosted login. Assetera renders the sign-in methods (password, 2FA, passkey, SSO); you render nothing.
Callback: exchange the code server-side, store tokens in the session
Assetera redirects back with a short-lived code. Your BFF exchanges it server-to-server for an
access token and a refresh token, stores them in the server-side session, and sets an opaque
HttpOnly cookie. That cookie is all the browser ever gets. The tenant claim rides inside the token,
not the cookie.
Proxy: attach the bearer token and call the Marketplace API
Every data call from the browser hits your BFF (cookie only). The proxy looks up the session, attaches
the access token, and forwards to the Marketplace API. The API validates the token locally and scopes the
response to your tenant claim, so your customer sees only your catalog. Refresh happens here too, fully
server-side.
Gate on KYC before the wallet and trading
Before showing "add wallet" or trade controls, read the user's status from your same-origin BFF route backed by the Compliance API. The order is fixed: KYC, then the wallet, then trading. The Compliance API refuses to register a wallet until KYC is complete (see Register a client's wallet). The SDK runs the onboarding workflow; it is not the source you should use to authorize a trade. The UI gate is UX only, and the API re-enforces eligibility.
const response = await fetch("/api/kyc/status", { cache: "no-store" });
const status = await response.json();
if (!status.verified) {
// redirect to KYC, and hide "add wallet" and trade controls
}Trade
With a verified user and a valid session, load the tenant-scoped market and fee data through the BFF. The
execution route then depends on the market: an on-chain trade is prepared and signed through the user's
wallet, while a configured bank-transfer primary sale uses the offchain-purchases API journey. Do not
invent a generic order-write endpoint. Use the live Marketplace API contract
and the off-chain sale guide for the operation enabled for your tenant.
Show the portfolio
Your BFF calls GET /me/holdings with the customer's token and receives the complete portfolio, with a
flag on each position for the assets your tenant offers. The fields, the filter, and the partial-failure
rules are in the Portfolio API.
Route handlers
// app/api/auth/login/route.ts
import * as client from "openid-client";
import { getConfig } from "@/lib/oidc";
import { getSession } from "@/lib/session";
export async function GET() {
const config = await getConfig();
const verifier = client.randomPKCECodeVerifier();
const challenge = await client.calculatePKCECodeChallenge(verifier);
const session = await getSession();
session.pkceVerifier = verifier;
await session.save();
const url = client.buildAuthorizationUrl(config, {
redirect_uri: process.env.ASSETERA_REDIRECT_URI!,
scope: "openid profile marketplace",
code_challenge: challenge,
code_challenge_method: "S256",
});
return Response.redirect(url.href);
}The catch-all proxy shown here forwards a broad path space for brevity. In production, allowlist the routes you expose and add refresh-on-401 so an expired access token is renewed server-side before retry.
Rules that keep it safe
- No general API token in the browser. No Marketplace or Compliance API access/refresh token in
localStorage, query strings, or non-HttpOnly cookies. The app cookie is opaque and HttpOnly, Secure, SameSite. The MetaKYC SDK token is a short-lived, audience-restricted exception and must not be reused as an API bearer token. - Authorization is the API's job. Your UI may hide or show controls for UX, but the Marketplace API and Assetera Identity enforce the real rules, re-checked on every call. Never move authorization into the browser.
- Tenant comes from the token, not the request. You cannot select or spoof a tenant on the wire; you get the tenant your client was issued for. (See Tenancy.)
Backend-only integration (no user browser)
If you have no interactive frontend (a server syncing catalog or acting on behalf of your book), skip the
BFF and use the Client Credentials grant: your backend authenticates as its own service client, receives
a token carrying your tenant claim, and calls the API directly. No user session, no cookie. See
Authentication.
Assetera maintains a working first-party BFF implementation using this pattern. A partner starter is supplied during technical onboarding with the endpoints and environment values enabled for that tenant.
Authentication
The OIDC grants, local token validation, and the tenant claim in detail.
KYC SDK
Check and gate on a user's KYC/AML status before enabling trade.
Marketplace API
The endpoints your BFF proxy calls: catalog, orders, settlement.
Portfolio API
Show the customer's holdings, all assets or only the assets you offer.
Partner onboarding
Getting your OIDC client provisioned and going live.
Embed the marketplace in your app
A worked B2B2C example. Your customers buy tokenized securities inside your own app, on your brand, while Assetera holds the licence and runs onboarding and settlement.
KYC integration (MetaKYC SDK)
Embed Assetera's KYC flow with the @asseteragmbh/metakyc React SDK. Get npm access, mint a session token on your server, then render the workflow.