Skip to main content

Overview

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.

Method Signature

Type Definitions

Parameters

string
required
The name of the rate limit to expose. Must match a rate limit defined in your RateLimiter constructor, unless you provide an inline config.
HookOptions
Optional configuration for the hook API.

Return Value

QueryFunction
A Convex public query that returns the current rate limit value and metadata.Arguments:
Returns:
MutationFunction
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

Usage Patterns

Pattern 2: Static Key (Global Rate Limit)

Pattern 3: Client-Provided Key (Use with Caution)

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.

Complete Setup Example

Server Setup

Client Setup

Key Function Patterns

Authentication-Based

IP-Based (requires custom auth setup)

Multi-Tenant with Validation