Overview
The Clerk Utilities API provides backend functions for managing Clerk user metadata and authentication-related operations. These functions run in Node.js environment and require Clerk backend SDK access. Location:convex/utils/clerk.ts
User Metadata
Manage user public metadata for role-based access and preferences
Backend Integration
Server-side Clerk operations with full SDK access
Update User Public Metadata
Update user’s public metadata completely (replaces existing metadata).Clerk user ID
New metadata object to replace existing metadata
Get User Public Metadata
Retrieve user’s complete public metadata.Clerk user ID
Merge User Public Metadata
Merge new metadata with existing metadata (preserves existing values).Clerk user ID
New metadata to merge with existing metadata
This function merges new metadata with existing metadata, preserving existing values that aren’t explicitly overridden.
Remove User Public Metadata Keys
Remove specific keys from user’s public metadata.Clerk user ID
Array of keys to remove from metadata
Set User Public Metadata Key
Set a specific key-value pair in user’s public metadata.Clerk user ID
Metadata key to set
Value to set for the key
Get User Public Metadata Key
Retrieve a specific key from user’s public metadata.Clerk user ID
Metadata key to retrieve
Has User Public Metadata Key
Check if user has a specific key in their public metadata.Clerk user ID
Metadata key to check for
Clear User Public Metadata
Clear all user’s public metadata (sets to empty object).Clerk user ID
Complete User Management Example
Here’s a complete example of user role management using Clerk utilities:Environment Setup
To use Clerk utilities, configure the following environment variable:Clerk Secret Key
Clerk Secret Key
Required environment variable:Setup steps:
- Go to Clerk Dashboard → API Keys
- Copy the “Secret key” (starts with
sk_test_orsk_live_) - Add to your Convex environment variables
- Restart your Convex deployment
Error Handling
Invalid User ID
Invalid User ID
Status Code:
400Missing Secret Key
Missing Secret Key
Status Code:
500Clerk API Error
Clerk API Error
Status Code:
500Best Practices
Metadata Structure
- Use consistent key naming conventions
- Store only necessary data in public metadata
- Consider data size limits (2KB total)
Error Handling
- Always wrap calls in try-catch blocks
- Provide meaningful error messages
- Handle rate limiting gracefully
Performance
- Cache metadata when possible
- Batch operations when updating multiple keys
- Use specific key operations instead of full metadata updates
Security
- Never store sensitive data in public metadata
- Validate user permissions before operations
- Use private metadata for sensitive information
All Clerk utility functions require Node.js environment and must be called from Convex actions. They cannot be used in queries or mutations due to the “use node” directive.
