recaptcha

reCAPTCHA support for Nim, supporting rendering a capctcha and verifying a user\'s response.

Pure Nim score 30/100 · tests present · docs generated

Summary

Latest Version 1.0.3
License BSD-3-Clause
CI Status Failing
Downloads 0
Last Indexed 2026-08-08 05:33

Authors

  • Euan T

Installation

nimble install recaptcha
choosenim install recaptcha
git clone https://github.com/euantorano/recaptcha.nim

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
recaptcha - - - - - - -

Dependencies

Package Version Optional
nim >= 1.0.0 No

Source

Repository https://github.com/euantorano/recaptcha.nim
Homepage https://github.com/euantorano/recaptcha.nim
Documentation View Documentation
Registry Source nimble_official

README

reCAPTCHA

reCAPTCHA support for Nim, supporting rendering a capctcha and verifying a user's response. This library supports reCaptcha from: * https://www.google.com/recaptcha/admin * https://recaptcha.net * https://hcaptcha.com

Installation

nimble install recaptcha

Usage

Before using this modul, be sure to register at the reCaptcha providers website in order to get your client secret and site key. These are required to use this module. Register at: * Google reCaptcha * hCaptcha

Once you have your client secret and site key, you should create an instance of ReCaptcha:

import recaptcha

let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google)
#let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", RecaptchaNet) <-- using recaptcha.net
#let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Hcaptcha)     <-- using hcaptcha.com

You can print out the required HTML code to show the reCAPTCHA element on the page using the render method:

import recaptcha

let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google)

echo captcha.render()

(or you can use the $ stringify shortcut to achieve the same thing)

Once the user has submitted the captcha back to you, you should verify their response:

import recaptcha, asyncdispatch

let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google)

let response = await captcha.verify(THEIR_RESPONSE)

If the user is a valid user, response will be true. Otherwise, it will be false.

Should there be an issue with the data sent to the reCAPTCHA service, an exception of type CaptchaVerificationError will be thrown.

In most cases, captchas are used within a HTML <form> element. In this case, the user's response will be available within form parameter named g-recaptcha-response for Google and reCaptha.net and within h-recaptcha-repsonse for hCaptcha.

You may also optionally pass the user's IP address along to reCAPTCHA during verification as an extra check:

import recaptcha, asyncdispatch

let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google)

let response = await captcha.verify(THEIR_RESPONSE, USERS_IP_ADDRESS)

Example

The example below uses Google as validator.

import recaptcha, asyncdispatch, jester
from strutils import format

const htmlForm = """
<form method="POST" action="/verify">
  <input type="text" name="email">
  <input type="password" name="password">
  <div class="g-recaptcha" data-sitekey="$1"></div>
  <button type="submit">Verify</button>
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</form>
"""

let 
  secretKey = "123456789"
  siteKey   = "987654321"
  captcha   = initReCaptcha(secretKey, siteKey, Google)

routes:
  get "/login":
    resp(htmlForm.format(siteKey))

  post "/verify":
    let checkCap = await captcha.verify(@"g-recaptcha-response")
    if checkCap:
      resp("Welcome")
    else:
      resp("You failed the captcha")