api-authentication
Backend API authentication patterns with Clerk JWT middleware and route protection. Use when building REST APIs, GraphQL APIs, protecting backend routes, implementing JWT validation, setting up Express middleware, or when user mentions API authentication, backend security, JWT tokens, or protected endpoints.
What this skill does
# api-authentication
Backend API authentication skill for Clerk integration. Provides JWT middleware, route protection patterns, and API client generation for REST and GraphQL backends.
## Instructions
### Phase 1: Understand Requirements
1. Identify backend framework (Express, Fastify, Next.js API routes, etc.)
2. Determine authentication strategy (JWT validation, session tokens)
3. Check for existing Clerk configuration
4. Identify API endpoints to protect
### Phase 2: Setup API Authentication
Run the setup script to configure backend authentication:
```bash
bash scripts/setup-api-auth.sh <framework> <project-path>
```
**Supported Frameworks:**
- `express` - Express.js middleware
- `fastify` - Fastify decorators
- `nextjs` - Next.js API route helpers
- `fastapi` - FastAPI dependencies (Python)
**What it does:**
- Installs required Clerk SDK packages
- Creates middleware files from templates
- Configures environment variables
- Sets up JWT verification utilities
- Creates route protection helpers
### Phase 3: Implement Route Protection
**For Express/Node.js backends:**
Use the `api-middleware.ts` template:
```typescript
import { requireAuth } from './middleware/clerk-auth'
// Protect individual routes
app.get('/api/protected', requireAuth, (req, res) => {
const userId = req.auth.userId
res.json({ message: 'Protected data', userId })
})
// Protect route groups
app.use('/api/admin', requireAuth, adminRouter)
```
**For Next.js API routes:**
Use the `api-routes.ts` template:
```typescript
import { withAuth } from '@/lib/clerk-middleware'
export default withAuth(async (req, res) => {
const { userId } = req.auth
// Protected route logic
})
```
**For GraphQL:**
Use the `graphql-clerk.ts` example:
```typescript
import { ClerkExpressRequireAuth } from '@clerk/clerk-sdk-node'
const server = new ApolloServer({
context: ({ req }) => ({
userId: req.auth?.userId,
user: req.auth?.user
})
})
app.use('/graphql', ClerkExpressRequireAuth(), apolloMiddleware)
```
### Phase 4: Generate API Client
Create type-safe API clients with authentication headers:
```bash
bash scripts/generate-api-client.sh <api-type> <output-path>
```
**API Types:**
- `rest` - REST API client with fetch
- `graphql` - GraphQL client with Apollo
- `axios` - Axios-based REST client
- `trpc` - tRPC client with auth context
**Generated Client Features:**
- Automatic JWT token attachment
- Token refresh handling
- Type-safe request methods
- Error handling for auth failures
### Phase 5: Test Authentication
Run comprehensive authentication tests:
```bash
bash scripts/test-api-auth.sh <project-path>
```
**Test Coverage:**
- ✅ Unauthenticated requests rejected (401)
- ✅ Valid JWT tokens accepted
- ✅ Expired tokens refreshed
- ✅ Invalid tokens rejected
- ✅ Protected routes accessible with auth
- ✅ User context available in handlers
## Common Patterns
### Pattern 1: Express Middleware
```typescript
// middleware/clerk-auth.ts
import { ClerkExpressRequireAuth } from '@clerk/clerk-sdk-node'
export const requireAuth = ClerkExpressRequireAuth({
onError: (error) => {
console.error('Auth error:', error)
return { status: 401, message: 'Unauthorized' }
}
})
// Optional auth (allows both authenticated and anonymous)
export const optionalAuth = ClerkExpressWithAuth()
```
### Pattern 2: Custom JWT Validation
```typescript
// lib/jwt-verify.ts
import { verifyToken } from '@clerk/backend'
export async function validateJWT(token: string) {
try {
const payload = await verifyToken(token, {
secretKey: process.env.CLERK_SECRET_KEY
})
return { valid: true, userId: payload.sub }
} catch (error) {
return { valid: false, error: error.message }
}
}
```
### Pattern 3: Role-Based Access Control
```typescript
// middleware/rbac.ts
export function requireRole(role: string) {
return async (req, res, next) => {
const { userId } = req.auth
const user = await clerkClient.users.getUser(userId)
if (user.publicMetadata.role !== role) {
return res.status(403).json({ error: 'Forbidden' })
}
next()
}
}
// Usage
app.get('/api/admin', requireAuth, requireRole('admin'), handler)
```
### Pattern 4: GraphQL Context Integration
```typescript
// graphql/context.ts
import { ClerkExpressRequireAuth } from '@clerk/clerk-sdk-node'
export const context = async ({ req }) => {
const userId = req.auth?.userId
if (!userId) {
throw new AuthenticationError('Must be authenticated')
}
const user = await clerkClient.users.getUser(userId)
return {
userId,
user,
isAdmin: user.publicMetadata.role === 'admin'
}
}
```
## Environment Variables
Required environment variables (always use placeholders in committed files):
```bash
# .env.example
CLERK_PUBLISHABLE_KEY=your_clerk_publishable_key_here
CLERK_SECRET_KEY=your_clerk_secret_key_here
# Optional: For webhook verification
CLERK_WEBHOOK_SECRET=your_webhook_secret_here
# Optional: For custom JWT configuration
CLERK_JWT_KEY=your_jwt_key_here
```
## Security Best Practices
1. **Always validate tokens server-side** - Never trust client-side validation alone
2. **Use HTTPS in production** - JWT tokens must be transmitted securely
3. **Implement rate limiting** - Prevent brute force attacks on protected endpoints
4. **Sanitize user inputs** - Validate all data even from authenticated users
5. **Log authentication events** - Track failed auth attempts and suspicious activity
6. **Rotate secrets regularly** - Update webhook and JWT secrets periodically
7. **Use environment variables** - Never hardcode API keys or secrets
## Troubleshooting
**Issue: "Invalid token" errors**
- Verify `CLERK_SECRET_KEY` is correct
- Check token expiration settings in Clerk dashboard
- Ensure clock sync between client and server
**Issue: CORS errors on API requests**
- Configure CORS middleware before Clerk middleware
- Whitelist your frontend domain in CORS config
- Include credentials in fetch requests
**Issue: "Missing userId" in request context**
- Verify middleware is applied to route
- Check that token is sent in Authorization header
- Ensure middleware order is correct
**Issue: GraphQL authentication not working**
- Apply Clerk middleware before GraphQL middleware
- Extract auth from request in context function
- Check that Apollo Server receives request object
## Requirements
- Clerk account with secret key
- Backend framework (Express, Fastify, Next.js, etc.)
- Node.js 16+ or Python 3.8+ (for FastAPI)
- Environment variables configured
- HTTPS enabled in production
## Templates Reference
- `templates/api-middleware.ts` - Express/Node.js middleware
- `templates/api-routes.ts` - Next.js API route helpers
- `templates/backend-sdk-setup.ts` - Backend SDK initialization
- `templates/fastapi-middleware.py` - FastAPI authentication dependencies
## Examples Reference
- `examples/rest-api.md` - Complete REST API with authentication
- `examples/graphql-api.md` - GraphQL server with Clerk context
- `examples/webhooks.md` - Webhook event handling and processing
## Scripts Reference
- `scripts/setup-api-auth.sh` - Configure backend authentication
- `scripts/generate-api-client.sh` - Create authenticated API clients
- `scripts/test-api-auth.sh` - Test authentication flows
---
## Security Compliance
This skill follows strict security rules:
- All code examples use placeholder values only
- No real API keys, passwords, or secrets
- Environment variable references in all code
- `.gitignore` protection documented
**Reference:** `@docs/security/SECURITY-RULES.md`
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.