Learn how to use Unkey to ratelimit public pages, like newsletter sign up forms or login attempts.
Author:
James Perkins
Consider a typical authentication form. Without protection, someone could:
Submit thousands of fake email addresses, polluting your database
Attempt to guess valid email addresses through enumeration
Overload your server with excessive requests
Abuse authentication endpoints by trying random email/password combinations
Traditional rate limiting based on IP addresses has limitations. Users can bypass IP-based restrictions using VPNs, proxy servers, or botnets. This is where device fingerprinting shines.
Device Fingerprinting: A Better Approach
Device fingerprinting creates a unique identifier for each device based on browser characteristics, hardware information, and other attributes. Unlike IP addresses, fingerprints are much harder to spoof and provide a more reliable way to identify individual devices.
The thumbmark.js library generates comprehensive fingerprints using:
This creates a robust identifier that persists across sessions and IP changes.
Implementing Rate Limiting with Unkey
Let's walk through a complete implementation using Next.js, Unkey for rate limiting, and thumbmark.js for device fingerprinting. You can see the full example in the unkey-fingerprint repository.
Step 1: Setting Up Unkey
First, create an Unkey account and generate a root key with rate limiting permissions:
# Add to your .env.localUNKEY_ROOT_KEY=your_unkey_root_key_here
Sometimes legitimate users might trigger rate limits (family sharing devices, public computers). Consider:
Providing a way for users to request limit increases
Implementing progressive rate limiting (stricter limits for new users)
Adding CAPTCHA challenges for suspicious activity
Privacy Implications
Device fingerprinting raises privacy concerns. Be transparent about:
What data you're collecting
How you're using it
How users can opt out if needed
Monitoring and Analytics
Track rate limiting effectiveness:
// Log rate limit violations for monitoringif (!success) { console.warn(`Rate limit exceeded: ${fingerprintData.thumbmark}`); // Send to monitoring service}
Conclusion
Device fingerprinting combined with Unkey's rate limiting provides a robust solution for protecting public endpoints. This approach is more reliable than IP-based restrictions and offers better protection against automated abuse.
The complete example demonstrates how to implement this pattern securely, with proper validation, error handling, and user experience considerations. By following these practices, you can protect your public endpoints while maintaining a smooth experience for legitimate users.
For the full implementation, check out the unkey-fingerprint repository on GitHub.