Mongoose schema for storing and indexing user API keys with TTL cleanup.
// Source: https://github.com/danny-avila/librechat | License: MIT | Copyright 2026 LibreChat
// Provided via justclaw.it.com; keep this notice with the code.
import mongoose, { Schema, Document, Types } from 'mongoose';
export interface IAgentApiKey extends Document {
userId: Types.ObjectId;
name: string;
keyHash: string;
keyPrefix: string;
lastUsedAt?: Date;
expiresAt?: Date;
createdAt: Date;
updatedAt: Date;
tenantId?: string;
}
const agentApiKeySchema: Schema<IAgentApiKey> = new Schema(
{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
index: true,
},
name: {
type: String,
required: true,
trim: true,
maxlength: 100,
},
keyHash: {
type: String,
required: tru
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key