Clerk is a developer-first auth platform that bundles user management, organizations, billing, and embeddable UI components. For a backend API, the @clerk/backend package exposes authenticateRequest(), which takes a standard Request and returns an Auth object, a perfect fit for DaloyJS's Web-standard handlers.
Clerk session-token verification
Frontend SDKClerkDaloyJS API
01asyncFrontend SDKClerkUser signs in; SDK gets a session token via getToken()
02requestFrontend SDKDaloyJS APICall API with Authorization: Bearer <session token>
03asyncDaloyJS APIClerkauthenticateRequest() verifies the tokennetworkless when jwtKey is set; otherwise calls Clerk
04noteDaloyJS APIDaloyJS APICheck authorizedParties, then read userId, orgId, orgRole
05responseDaloyJS APIFrontend SDKReturn protected data, or 401 when isAuthenticated is false
With CLERK_JWT_KEY set, authenticateRequest() verifies the token as a pure crypto check with no Clerk API call, which is ideal for edge runtimes. authorizedParties pins the origins allowed to call the API.
1. Set up your Clerk app
Create an application in the Clerk dashboard. From API Keys, copy the Publishable Key and Secret Key. Optionally copy the JWT Public Key (PEM) for networkless verification.
Your frontend (Clerk's React, Next.js, Expo, or vanilla JS SDK) obtains a session token via getToken() and sends it in the Authorization: Bearer <token> header to your DaloyJS API.
For machine-to-machine calls, create an M2M token or use Clerk's OAuth applications and accept oauth_token / m2m_token in the verifier.
2. Install
ts
pnpm add @clerk/backend
3. Environment variables
ts
# .envCLERK_PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCLERK_SECRET_KEY=sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx# Optional - enables networkless JWT verification (no Clerk API call per request)# Get it from API Keys → Show JWT public key → PEM Public KeyCLERK_JWT_KEY="-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----"
Setting authorizedParties is strongly recommended, it pins the origins allowed to make requests and protects against the subdomain-cookie-leaking attack described in Clerk's docs. Setting jwtKey turns verification into a pure crypto check (no network), which is ideal for edge runtimes.
Clerk's Auth object includes the active orgId, orgSlug, orgRole (e.g. org:admin), and orgPermissions. Add a thin helper to require a role on top of requireClerkAuth:
Set acceptsToken to "m2m_token", "oauth_token", or an array like ["session_token", "m2m_token"] to accept multiple token kinds. The returned tokenType lets you branch your business logic per caller type.
Webhooks
Clerk delivers user, organization, and session events via Svix-signed webhooks. Use clerk.verifyWebhook(request) to validate the signature before processing the payload, never trust an unverified webhook body.
Runtimes
@clerk/backend is built on the Web Request and fetch APIs, so it runs on Node 18+, Bun, Deno, AWS Lambda, Vercel (Serverless and Edge), and Cloudflare Workers. Pair it with the edge adapters.