clerk-backend-apis
FreeNo executable scriptsNot checkedUse for Clerk backend request auth. PROACTIVELY activate for Express, Fastify, Node, Go, Ruby/Rails, Python/FastAPI/Django/Flask, Java/Spring, C#/.NET…
by JosiahSiegel★ 67 GitHub starsAbout this skill
Clerk Backend APIs
Overview
Use this skill for Clerk authentication in backend services, API servers, workers, and server-to-server integrations. The key responsibilities are authenticating incoming requests, enforcing authorization on server resources, safely using the Backend API, and making cross-origin token behavior explicit.
Prefer official Clerk SDKs and middleware when available. When the target runtime lacks a first-class Clerk framework SDK, use Clerk-documented token verification and Backend API patterns rather than ad hoc JWT parsing.
Backend Integration Model
The server flow is language-independent:
- Receive request.
- Preserve auth-relevant headers and cookies through all proxies.
- Authenticate Clerk input using an official SDK/helper where available.
- Derive identity, session, organization, and token type from verified request state.
- Authorize the requested resource and action.
- Parse/validate application input.
- Perform data access or side effects.
- Return explicit
401unauthenticated or403/concealment unauthorized responses for APIs, unless using Clerk defaults intentionally.
Avoid handwritten JWT parsing unless Clerk's official docs for the target language require a lower-level path. Clerk token semantics evolve, and framework SDKs handle edge cases better than custom code.
Express Pattern
Register Clerk middleware before routes that need auth state. Use getAuth(req) in route handlers when custom JSON responses are preferred. Use requireAuth() when automatic route protection behavior fits the app.
Minimal shape:
import express from 'express'
import { clerkMiddleware, getAuth, requireAuth } from '@clerk/express'
const app = express()
app.use(clerkMiddleware())
app.get('/api/me', (req, res) => {
const { isAuthenticated, userId, orgId } = getAuth(req)
if (!isAuthenticated) return res.status(401).json({ error: 'Unauthorized' })
return res.json({ userId, orgId })
})
app.get('/api/protected', requireAuth(), (req, res) => {
res.json({ ok: true })
})
clerkMiddleware() checks request cookies and headers for a session JWT and attaches auth state to the request. getAuth(req) reads user ID, session ID, organization ID, and related auth state. clerkClient wraps Clerk's Backend API and must be instantiated according to the SDK's current docs before resource access.
Fastify Pattern
Use Clerk's Fastify plugin where available. The documented model is:
- Register
clerkPlugin()with the Fastify app before protected routes. - The plugin checks request cookies and headers for a session JWT.
- Valid auth data is attached to
request.auth. - Use
getAuth(request)to retrieve current auth state. - Use
clerkClientserver-side for Backend API calls after instantiation.
If the framework integration does not expose requireAuth(), implement route guards with getAuth(request) and explicit JSON responses.
Cross-Origin Requests
Same-origin browser requests in supported Clerk frameworks can often rely on Clerk-managed cookies. Cross-origin requests to a separate API origin must include a Bearer token retrieved from the frontend session.
Client pattern:
const token = await getToken()
await fetch('https://api.example.com/widgets', {
headers: { Authorization: `Bearer ${token}` },
})
Backend pattern:
- Accept the
Authorizationheader. - Ensure CORS allows the exact frontend origin and the
Authorizationheader. - Authenticate the token with Clerk middleware or documented backend helper.
- Reject missing, invalid, expired, wrong-environment, or wrong-token-type requests before touching application data.
Common mistakes: forgetting to await getToken(), relying on cookies across origins, stripping Authorization in a proxy, wildcard CORS with credentials, and debugging only browser navigations instead of direct API requests.
Request State and Proxy Headers
Clerk uses request headers to determine whether a request is signed in, signed out, or needs a handshake. Behind proxies, CDNs, and load balancers, preserve auth-relevant headers.
Required/important headers include:
AuthorizationAcceptHostOriginRefererSec-Fetch-DestUser-AgentX-Forwarded-HostX-Forwarded-ProtoorCloudFront-Forwarded-Proto
When deploying behind CloudFront, Nginx, Vercel rewrites, Cloudflare, custom domains, API gateways, or Kubernetes ingress, test the exact production path. Local success does not prove forwarded headers survive production.
Backend API Client Guardrails
Use Clerk's Backend API client only from trusted server environments. Typical operations include fetching users, listing users, updating metadata, reading organizations, and managing memberships. Never expose Backend API credentials, secret keys, or administrative operations to browser code.
Instantiate the client according to the SDK's current docs before accessing resources. Avoid manual fetches to Clerk's API unless
Install clerk-backend-apis in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the clerk-backend-apis skill do?
Use for Clerk backend request auth. PROACTIVELY activate for Express, Fastify, Node, Go, Ruby/Rails, Python/FastAPI/Django/Flask, Java/Spring, C#/.NET, PHP/Laravel, custom APIs, @clerk/express clerkMiddleware()/requireAuth()/getAuth(req), backend request state, cookies, Authorization Bearer tokens, cross-origin frontend-to-API calls, M2M/OAuth/API-key/session token validation, clerkClient Backend API users/orgs calls, proxies/CDNs/CORS/X-Forwarded headers, and 401/404 debugging. Provides middleware patterns, token verification flow, API client guardrails, and language-portable diagnostics.
How do I install the clerk-backend-apis skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the clerk-backend-apis skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
by Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
by Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
by Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
by Anthropic