Developing Capybara
zamkara.uk
Share this article
how-to-build

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:

Serverless

Run entirely on Cloudflare Workers

Privacy-first

No cookies, no tracking, KV only

Customizable

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.

Deploy Worker

Local Setup

0. Wrangler Prepare

Terminal window
wrangler kv namespace create "CAPY_KV"
wrangler kv namespace create "CAPY_KV --preview"

1. Create Project Directory

Terminal window
mkdir capybara
cd capybara

2. 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

Terminal window
wrangler dev --local --port 8789 --config wrangler.jsonc | cat

5. Deploy

Terminal window
wrangler deploy --config wrangler.jsonc

6. 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

Capybara CAPTCHA Demonstration

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 generation
POST /api/challenge
→ ( id, nonce, difficulty, duration )
// Challenge retrieval
GET /api/challenge/:id
→ ( nonce, difficulty, duration )
// Solution verification
POST /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 tracking
Key: `KV_PREFIX_BASE`:`INSTANCE_ID`:IP:(ip_address)
Value:
challengesToday: number
lastChallengeTs: timestamp
# Challenge storage
Key: `KV_PREFIX_BASE`:`INSTANCE_ID`:CH:(challenge_id)
Value:
nonce: string
difficulty: number
duration: number
createdAt: timestamp

This 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.



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 ✳︎

Yo, you open for freelance right now?