Command Palette

Search for a command to run...

UnylyUnyly
Back to skills

clerk-backend-apis

FreeNo executable scriptsNot checked

Use for Clerk backend request auth. PROACTIVELY activate for Express, Fastify, Node, Go, Ruby/Rails, Python/FastAPI/Django/Flask, Java/Spring, C#/.NET…

JosiahSiegelby JosiahSiegel★ 67 GitHub stars

About 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:

  1. Receive request.
  2. Preserve auth-relevant headers and cookies through all proxies.
  3. Authenticate Clerk input using an official SDK/helper where available.
  4. Derive identity, session, organization, and token type from verified request state.
  5. Authorize the requested resource and action.
  6. Parse/validate application input.
  7. Perform data access or side effects.
  8. Return explicit 401 unauthenticated or 403/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 clerkClient server-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 Authorization header.
  • Ensure CORS allows the exact frontend origin and the Authorization header.
  • 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:

  • Authorization
  • Accept
  • Host
  • Origin
  • Referer
  • Sec-Fetch-Dest
  • User-Agent
  • X-Forwarded-Host
  • X-Forwarded-Proto or CloudFront-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

Full SKILL.md on GitHub

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
Sign up freeI already have an account

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

Compare clerk-backend-apis with