VK Cloud logo

SDK reference for web applications

System requirements

Supported browsers:

  • Google Chrome version 52 and above;
  • Microsoft Edge version 79 and above;
  • Firefox version 57 and above;
  • Safari version 11 and above;
  • Opera version 39 and above;
  • Yandex Browser version 16.9 and above.

Mount the captcha

The captchaWidget.render() method mounts the captcha onto the page and starts the captcha service. The method is called once per page load.

Example of a method call:

await captchaWidget.render({   link: captchaLink,   lang: 'ru',   invisible: true,});

Method parameters:

Parameter

Required

Type

Description

link

SDK reference for web applications

string

Link for launching the captcha widget, received in the link parameter in the response of the GET /captchaNotRobot.createSession method. Contains a session token and metadata:

  • domain — domain where the captcha will be used;
  • session_token — session token;
  • variant — captcha display type (block or popup)

invisible

SDK reference for web applications

boolean

Indicates an invisible captcha. Possible values:

  • true — invisible captcha, the check runs in the background and is triggered by the captchaWidget.execute() method;
  • false — visible captcha

container

SDK reference for web applications

HTMLElement | string

HTML element or CSS selector into which the captcha is embedded. If not specified, the captcha is displayed in a pop-up window over the page using the captchaWidget.execute() method.

If the CSS selector does not find any elements, a pop-up window will be displayed instead of the embedded captcha. This is not an error

lang

SDK reference for web applications

string

Language for localizing the captcha task and the data processing notice. If not specified, the browser language is used. If the browser language is not recognized by the service, the task language will be determined on the server, and the notice will be displayed in Russian. Possible values:

  • ru — Russian;
  • be — Belarusian;
  • de — German;
  • en — English;
  • es — Spanish;
  • fr — French;
  • kk — Kazakh;
  • pl — Polish;
  • tr — Turkish;
  • uk — Ukrainian;
  • uz — Uzbek;
  • zh — Chinese

scheme

SDK reference for web applications

string

Color scheme of the captcha widget. Possible values:

  • light — light;
  • dark — dark.

By default, it matches the color scheme of the page or application where the captcha is integrated

notice

SDK reference for web applications

object

Data processing notice settings. The notice is displayed only with invisible: true. The text and link in the notice cannot be changed, as it is a legal notice

notice.position

SDK reference for web applications

string

Corner of the page where the notice is displayed. Possible values:

  • bottom-right — bottom right (default);
  • bottom-left — bottom left;
  • top-right — top right;
  • top-left — top left

notice.hidden

SDK reference for web applications

boolean

Indicates whether the notice is hidden. Possible values:

  • true — notice is hidden;
  • false — notice is displayed (default). If the data processing notice is hidden, add your own notice

notice.draggable

SDK reference for web applications

boolean

Indicates whether the notice can be dragged along the edge of the screen. The chosen position is remembered for this domain. Possible values:

  • true — dragging is allowed;
  • false — dragging is not allowed (default)

slowLoadAfter

SDK reference for web applications

number

Time in milliseconds after which an event is sent that the captcha widget has not yet loaded. The default value is 10000. After this time, VK Капча SDK sends the captcha: load slow event. Sending the event does not interrupt the captcha loading

The method returns a promise object with captcha readiness:

promise resolvepromise reject <ERROR>

Error codes:

Code

Reason

invalid_link

Invalid link for launching the captcha widget

mount_failed

Failed to mount the captcha onto the page

already_initialized

The captchaWidget.render() method has already been called

failed

The captcha service reported an error

close

The user closed the captcha

destroyed

The captcha was removed from the page by the captchaWidget.destroy() method

Run the user check

The captchaWidget.execute() method:

  • starts the user check;
  • displays a pop-up window with the captcha, if this is set by the captchaWidget.render() method;
  • returns a token of successful captcha completion to pass to the backend.

The method is called once per mounted captcha session. Usually, it is triggered in response to a user's target action, such as submitting a form, but it can also be called by a backend response that initiated the captcha.

The captcha session is consumed by the first check: after a successful check, a repeated call to the method is rejected with the already_checked error. To run the next check on the same page load, remove the captcha with the captchaWidget.destroy() method and mount it again with the captchaWidget.render() method using a new link from the response of the GET /captchaNotRobot.createSession method.

Example of a method call:

const successToken = await captchaWidget.execute();

Parameters of the captchaWidget.execute() method:

Parameter

Required

Type

Description

timeout

SDK reference for web applications

number

Timeout for waiting for a response from the captcha service in milliseconds. The default value is 10000.

The countdown starts from the moment the method is called. If the captcha is still loading, the loading wait time is included in the timeout.

When the timeout expires, the promise object is rejected with the timeout error, but the captcha remains on the page: the next call to the captchaWidget.execute() method will be sent to the captcha service as soon as the captcha loads.

If the captcha expands into a visible task, the timeout is removed, and the user solves the task without a time limit

The method returns a promise object with the check result:

promise resolve <CAPTCHA_COMPLETION_TOKEN>promise reject <ERROR>

Here:

  • <CAPTCHA_COMPLETION_TOKEN> — token of successful captcha completion, if the user passed the captcha;
  • <ERROR> — error code.

Error codes:

Code

Reason

not_initialized

The captchaWidget.render() method was not called or ended with an error

not_passed

The user did not interact with the captcha

timeout

The check response did not arrive within the allotted time

token_expired

The token has expired

failed

The check failed or a captcha widget error occurred

already_checked

The check has already been passed

close

The user closed the captcha

destroyed

The captcha was removed from the page by the captchaWidget.destroy() method

Subscribe to events

The captchaWidget.on() method is used to subscribe to events. Event names are available as properties of the CaptchaPublicEvents object.

Example of a method call:

import { CaptchaPublicEvents } from '@vkid/captcha';captchaWidget.on(CaptchaPublicEvents.SUCCESS, () => setCanSubmit(true));

Events you can subscribe to:

Event

Data

When it is sent

captcha: success

{ token }

Check passed. For the embedded checkbox — at the moment the checkbox is clicked

captcha: challenge visible

SDK reference for web applications

The invisible check expanded into a visible task

captcha: challenge hidden

SDK reference for web applications

The visible task is collapsed

captcha: token expired

SDK reference for web applications

The token has expired, sending it to the backend is no longer meaningful

captcha: load slow

{ slowLoadAfter }

The captcha application has been loading longer than the slowLoadAfter threshold specified in the captchaWidget.render() method. This is not an error, the captcha continues loading

When using the deprecated captchaWidget.show() method, events are not sent. The result comes only as a promise object of the method.

Unsubscribe from events

To unsubscribe from events, use the captchaWidget.off() method.

Example of a method call:

import { CaptchaPublicEvents } from '@vkid/captcha';captchaWidget.off(CaptchaPublicEvents.SUCCESS, onSuccess);

Hide the captcha

The captchaWidget.hidePopup() method hides the captcha widget without interrupting the check. The service continues to run, and the widget can be shown again instantly. The method returns nothing in the response.

Example of a method call:

captchaWidget.hidePopup();

Show the captcha

The captchaWidget.showPopup() method returns the captcha widget to the screen. The completion and state of the check are not reset. The method returns nothing in the response.

VK Капча SDK calls this method automatically when an invisible check transitions to a visible task.

Example of a method call:

captchaWidget.showPopup();

Close the captcha

The captchaWidget.close() method closes the captcha widget. The method returns nothing in the response.

Example of a method call:

captchaWidget.close();

Remove the captcha from the page

The captchaWidget.destroy() method removes the captcha from the page and returns the widget to its initial state: after it, the next captcha can be mounted on the same page load using the captchaWidget.render() method. The method returns nothing in the response.

The method is needed for single-page applications (SPA, Single Page Application), where screens change without a page reload. In this case, the captcha is tied to the screen lifecycle: when the screen appears, the captchaWidget.render() method is called; when leaving this screen — captchaWidget.destroy(). Each screen has its own captcha session and its own link. If the next screen also calls the captchaWidget.render() method without a preceding captchaWidget.destroy() call, the promise object is rejected with the already_initialized error.

Each screen needs its own instance of CaptchaWidget. Only one captcha instance should be running on the page at a time. When switching between screens, first call captchaWidget.destroy() on the current instance, then create and mount a new one.

The link for the next captcha must be new: the captcha session is consumed by the first check. Request a fresh link using the GET /captchaNotRobot.createSession method. You cannot cache the link in the application state: on the next screen, the captcha will be mounted, but the check will return an expired token. Request the link in the same code that calls captchaWidget.render().

Handle the rejection of the promise object with the destroyed error. The captchaWidget.destroy() method rejects unfinished promise objects of the captchaWidget.render() and captchaWidget.execute() methods with the destroyed error. In React StrictMode, the effect is executed twice in development mode, so the first call to captchaWidget.render() is always rejected — in the console, it looks like an unhandled promise rejection.

Before calling captchaWidget.render(), check that the screen is still active. Between requesting the link and the backend response, the user may leave the screen. A captchaWidget.destroy() called after that will do nothing, because the captcha has not yet been mounted, and the captchaWidget.render() called next will mount it on the abandoned screen.

VK Капча SDK does not send any events about captcha removal.

Example of a method call:

let captchaWidget: CaptchaWidget | undefined;let isScreenActive = false;// Screen appeared. In React — the effect of the component containing the captchaasync function mountCaptcha() {    isScreenActive = true;    captchaWidget = new CaptchaWidget();    captchaWidget.on(CaptchaPublicEvents.SUCCESS, onSuccess);    const { link } = await api.post('/api/captcha/session', { client });    // The user may have left the screen while the backend was responding    if (!isScreenActive) {        return;    }    // Leaving the screen before loading rejects the promise object with the destroyed code    await captchaWidget.render({ link, lang: 'ru', invisible: true }).catch(handleRenderError);}// Leaving the screen. In React — the cleanup function of the same effectfunction unmountCaptcha() {    isScreenActive = false;    captchaWidget?.destroy();}

Display the captcha

The captchaWidget.show() method displays the captcha widget to the user in a web application and is used in cases where the backend decides whether a user check is necessary. The captcha widget is displayed in a pop-up window (iframe).

Method parameters:

Parameter

Required

Type

Description

container

SDK reference for web applications

HTMLElement

HTML element in which to display the captcha widget

view

SDK reference for web applications

string

Captcha type. Possible values:

  • popup — pop-up window;
  • block — block

link

SDK reference for web applications

string

Link for launching the captcha widget, received in the link parameter in the response of the GET /captchaNotRobot.createSession method.

Replaces the deprecated iframeSrc parameter. One of the two parameters must be specified.

iframeSrc

SDK reference for web applications

string

Deprecated parameter, kept for compatibility. The new parameter is link. One of the two parameters must be specified.

Link for launching the widget in your application's frontend, received in the link parameter in the response of the GET /captchaNotRobot.createSession method. Contains a session token and metadata:

  • domain — domain where the captcha will be used;
  • session_token — session token;
  • variant — captcha display type (block or popup)

captchaType

SDK reference for web applications

string

Captcha type. Possible value — type_1 (default)

autofocus

SDK reference for web applications

boolean

Indicates captcha autofocus. Used only for the block value in the view argument. Possible values:

  • true — autofocus is enabled (default). The focus on the page will be moved to the web element with the captcha.
  • false — autofocus is disabled. The focus on the page will remain where it was before the web element with the captcha appeared

scheme

SDK reference for web applications

string

Color scheme of the captcha widget. Possible values:

  • light — light;
  • dark — dark.

By default, it matches the color scheme of the page or application where the captcha is integrated

lang

SDK reference for web applications

string

Localization language. If not specified, the value will be determined on the server. Possible values:

  • ru — Russian;
  • be — Belarusian;
  • de — German;
  • en — English;
  • es — Spanish;
  • fr — French;
  • kk — Kazakh;
  • pl — Polish;
  • tr — Turkish;
  • uk — Ukrainian;
  • uz — Uzbek;
  • zh — Chinese

onClose

SDK reference for web applications

() => void

Callback notification about the captcha closing

The method returns a promise object with the captcha completion result:

promise resolve <CAPTCHA_COMPLETION_TOKEN> promise reject <ERROR>

Here:

  • <CAPTCHA_COMPLETION_TOKEN> — token of successful captcha completion, if the user passed the captcha;
  • <ERROR> — the close error, if the user closed the captcha window.
Was this article helpful?