Client-side rendering
First, you must include the Procaptcha JavaScript resource somewhere in your HTML page. The <script>
must be loaded
via HTTPS and can be placed anywhere on the page. Inside the <head>
tag or immediately after the .procaptcha
container
are both fine.
Now, you can either render the Procaptcha widget implicitly or explicitly.
Implicit Rendering
Section titled Implicit RenderingAdd an empty DOM container where the Procaptcha widget will be inserted automatically. The container is
typically a <div>
(but can be any element) and must have class procaptcha
and a data-sitekey
attribute set to your
public
site key.
Typically, youβll want to include the empty .procaptcha
container inside an HTML form. When a captcha is successfully
solved, a hidden JSON payload will automatically be added to your form that you can then POST to your server for
verification. You can retrieve it server side with POST parameter procaptcha-response
.
Hereβs a full example where Procaptcha is being used to protect a signup form from automated abuse. When the form is
submitted, the procaptcha-response
token will be included with the email and password POST data after the captcha
is solved.
Example of implicit rendering
Section titled Example of implicit renderingExplicit Rendering
Section titled Explicit RenderingIf you prefer to render the widget yourself, you can use the Procaptcha.render()
method. The Procaptcha.render()
must be called after the procaptcha.bundle.js script has loaded.
Example of explicit rendering
Section titled Example of explicit renderingThe script is loaded in the head of the document and given the id procaptcha-script
. A container is created with the
id procaptcha-container
where the widget will be rendered.
An onload
event is added to the script tag to call the render function when the script has loaded.
Procaptcha Options
Section titled Procaptcha OptionsThe Procaptcha.render()
function takes an options object as its second argument. The options object can contain the
following fields:
Key | Type | Description | Required |
---|---|---|---|
siteKey | string | The site key of your application / website. This is required. | β |
callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA is verified. | β |
theme | string | The theme of the CAPTCHA widget. The default is light . The other option is dark . | β |
captchaType | string | The type of CAPTCHA to render. The default is frictionless . Other options are image , pow . | β |
chalexpired-callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA challenge expires. | β |
error-callback | string or function | The name of the window function, or a function, that will be called when an error occurs. | β |
close-callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA is closed. | β |
open-callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA is opened. | β |
expired-callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA solution expires. | β |
failed-callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA challenge fails. | β |
reset-callback | string or function | The name of the window function, or a function, that will be called when the CAPTCHA is reset. | β |
challenge-valid-length | number | The amount of time, in milliseconds, a successful CAPTCHA challenge is valid for. Defaults to 2 minutes. | β |
language | string | The language of the CAPTCHA widget. The default is en . | β |
Data Attributes
Section titled Data AttributesThe same options can be passed to the implicit rendering method by adding them as data attributes to the .procaptcha
container.
For example, to set the theme to dark, you would add data-theme="dark"
to the .procaptcha
container.
To set a callback using a data tag, you would add data-callback="yourCallbackFunction"
to the .procaptcha
container,
and define the callback function on the window object.
captchaType
Section titled captchaTypeYou can choose to implement any of the following types of captcha when rendering the Procaptcha component:
Type | Description |
---|---|
frictionless | The default CAPTCHA type is frictionless . This type of CAPTCHA is invisible to the user, only requiring them to complete an invisible Proof of Work challenge (pow ). Suspected bots are served image captcha challenges (image ). |
pow | The pow CAPTCHA type requires the user to solve a cryptographic puzzle. This puzzle simply requires a small amount of computational work to solve, and slows down bots significantly, making it difficult for them to scrape in high volumes. |
image | The image CAPTCHA type requires the user to solve a simple image CAPTCHA. This is CAPTCHA type most people are familiar with, created by Google reCAPTCHA. |
Please note, the client side CAPTCHA type must be set to the same value as in the portal, or a more secure version. For example, if your portal CAPTCHA Type is frictionless
, you may only set the client side CAPTCHA Type to frictionless
or image
. If your portal CAPTCHA Type is pow
, you may only set the client side CAPTCHA Type to pow
.
React Component
Section titled React ComponentTo use the Procaptcha React Component, import Procaptcha, define a config with ProcaptchaConfigSchema
, optionally define callbacks, and render via the Procaptcha component. A minimal example would be as follows:
Further example usage can be seen in demos/client-example
Learn