Developing Capybara
After validating the idea of a privacy-first CAPTCHA, the next challenge was execution.
This meant translating the Proof of Work concept into a real, production-ready system that could scale globally without traditional servers.
Design Goals
I set out with three non-negotiable technical goals:
Run entirely on Cloudflare Workers
No cookies, no tracking, KV only
Difficulty, duration, limits, redirect
Architecture
The system combines three core pieces:
- Cloudflare Worker → Acts as the API layer (
/api/challenge,/api/verify, etc.) - KV Namespace
CAPY_KV→ Stores per-IP rate limits and active challenges - Proof of Work Logic → Generates nonce-based SHA-256 challenges & validates solutions
Tip (Key insight)
Splitting responsibilities between KV and Worker logic kept the code minimal, while ensuring persistence of challenge state without any dedicated database.
Quick Start
You can deploy instantly to Cloudflare Workers by clicking the button below.
This method automatically configures the required KV namespace and deployment settings.
Local Setup
0. Wrangler Prepare
wrangler kv namespace create "CAPY_KV"wrangler kv namespace create "CAPY_KV --preview"1. Create Project Directory
mkdir capybaracd capybara2. Copy Templates
- worker.js (single-file Worker, standalone mode)
- wrangler.jsonc (fill in your KV id/preview_id)
3. Configure wrangler.jsonc
REDIRECT_URL: redirect target for /- Limits:
DEFAULT_DIFFICULTY,MIN_DIFFICULTY,MAX_DIFFICULTY,DEFAULT_DURATION_SEC,MIN_DURATION_SEC,MAX_DURATION_SEC,LIMIT_MAX_CHALLENGES_PER_DAY - Namespacing:
KV_PREFIX_BASE,INSTANCE_ID
4. Run Locally
wrangler dev --local --port 8789 --config wrangler.jsonc | cat5. Deploy
wrangler deploy --config wrangler.jsonc6. Confirm Public URL
Example: https://capybara.yourname.workers.dev
7. Using Captcha
Once deployed, you can start testing and using the provided Captcha components:
HTML Demo
TypeScript Component
These components can be integrated directly into your front-end projects to use Capybara CAPTCHA effortlessly.
Implementation Highlights
- Per-IP Limits: Lightweight abuse prevention via KV counters
- Configurable Difficulty: Adjustable challenge hardness
- Ephemeral Storage: Challenges expire after duration, ensuring no long-term persistence
- Namespacing: Supports multi-instance deployments with
INSTANCE_ID
API Shape
Capybara mimics the familiar shape of CAPTCHA APIs to make integration painless:
// Challenge generationPOST /api/challenge→ ( id, nonce, difficulty, duration )
// Challenge retrievalGET /api/challenge/:id→ ( nonce, difficulty, duration )
// Solution verificationPOST /api/verify→ ( id, solution )→ ( success: boolean )Each challenge is namespaced by KV_PREFIX_BASE:INSTANCE_ID so multiple deployments can coexist without collision.
Challenge Lifecycle
1. Client requests POST `/api/challenge` → Worker checks per-IP limits in KV → Worker generates nonce, assigns difficulty/duration → Challenge is stored in KV → Response sent back with (id, nonce, difficulty, duration)
2. Client later submits POST /api/verify (id, solution) → Worker fetches challenge by id from KV → Proof of Work is verified against SHA-256 target → Returns ( success: true/false )KV Structure
# IP-based limit trackingKey: `KV_PREFIX_BASE`:`INSTANCE_ID`:IP:(ip_address)Value: challengesToday: number lastChallengeTs: timestamp
# Challenge storageKey: `KV_PREFIX_BASE`:`INSTANCE_ID`:CH:(challenge_id)Value: nonce: string difficulty: number duration: number createdAt: timestampThis lightweight KV schema allows both rate-limiting and challenge validation without any external database.
Outcome
The development approach proved effective:
- Fast: ~50ms average Worker response time
- Efficient: ~5KB storage per challenge
- Scalable: 100k daily requests within Cloudflare free tier
Important (Developer note)
By relying only on Workers + KV, Capybara avoids infrastructure overhead while staying transparent, portable, and easy to maintain.
Related Posts
Capybara: PoW based & Serverless CAPTCHA
The main case study covering the problem, solution, and results of building a privacy-first CAPTCHA system.
JWT Payload Implementation
Deep dive into the JWT payload token system that powers Capybara’s secure challenge verification.
Conclusion
Developing Capybara reinforced the power of serverless platforms for building privacy-respecting security tools. What started as a minimal prototype became a reliable, open-source CAPTCHA alternative—demonstrating how simple KV-backed architectures can solve real security problems without complexity or cost.
Got something in mind?
✳︎ ask me anything ✳︎