The hookAPI() method creates a public query and mutation that can be used with the useRateLimit React hook. It returns two functions: getRateLimit for fetching current rate limit values, and getServerTime for client/server time synchronization.
A Convex public query that returns the current rate limit value and metadata.Arguments:
{ name?: string; // Override the rate limit name key?: string; // Client-provided key (only if allowed) sampleShards?: number; // Override sampleShards config?: RateLimitConfig; // Inline config}
Returns:
{ value: number; // Current token value ts: number; // Server timestamp shard: number; // Shard number used config: RateLimitConfig; // Rate limit configuration}
A Convex public mutation that returns the current server time (Date.now()). Used by useRateLimit to synchronize client and server clocks.Arguments: NoneReturns: number - Server timestamp in milliseconds
// convex/multiTenant.tsexport const { getRateLimit, getServerTime } = rateLimiter.hookAPI( "organizationAPI", { key: async (ctx, keyFromClient) => { // IMPORTANT: Validate the client can access this key const userId = await getUserId(ctx); const userOrgs = await ctx.db .query("memberships") .withIndex("by_user", (q) => q.eq("userId", userId)) .collect(); if (!userOrgs.some(m => m.orgId === keyFromClient)) { throw new Error("Access denied to this organization"); } return keyFromClient; }, });
// React componentfunction OrganizationDashboard({ orgId }) { const { status } = useRateLimit(api.multiTenant.getRateLimit, { key: orgId, // Client provides the org ID getServerTimeMutation: api.multiTenant.getServerTime, }); // ...}
Security Consideration: When using client-provided keys, always validate that the authenticated user has permission to access that key’s rate limit. Without validation, malicious clients could check rate limits for other users or organizations.