# recaptcha-lib

> Google reCaptcha backend and frontend integration library

Latest version **1.0.19** (published 2022-12-15) · 0 weekly downloads

## Install

```sh
npm install recaptcha-lib
pnpm add recaptcha-lib
yarn add recaptcha-lib
bun add recaptcha-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.19 |
| Published | 2022-12-15 |
| First published | 2022-12-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 242.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | CAKE |
| Maintainers | wasantha-cake |
| Keywords | Google, reCaptcha, library |

## Links

- npm: https://www.npmjs.com/package/recaptcha-lib
- npm.io page: https://npm.io/package/recaptcha-lib

## Recent versions

- 1.0.19 (latest) — 2022-12-15
- 1.0.18 — 2022-12-15
- 1.0.17 — 2022-12-15
- 1.0.16 — 2022-12-15
- 1.0.15 — 2022-12-15
- 1.0.14 — 2022-12-15
- 1.0.13 — 2022-12-15
- 1.0.12 — 2022-12-15
- 1.0.11 — 2022-12-15
- 1.0.10 — 2022-12-15
- 1.0.9 — 2022-12-15
- 1.0.8 — 2022-12-14
- 1.0.7 — 2022-12-14
- 1.0.6 — 2022-12-14
- 1.0.5 — 2022-12-14
- … 5 more at https://npm.io/package/recaptcha-lib/versions

## README

# Google reCAPTCHA JavaScript Library

The Google reCAPTCHA JavaScript third party library allows you to easily integrate the [reCAPTCHA](https://www.google.com/recaptcha) service into your web application. With reCAPTCHA, you can protect your site from spam and abuse while allowing real users to pass through with ease.

## Installation

To include the reCAPTCHA library in your project, execute below npm install command

```console
npm install recaptcha-lib 
```

## Usage

### API key generation

1. Visit the Google reCAPTCHA website and click on the "Admin console" button in the top right corner.
2. Sign in with your Google account and click on the "+" button to create a new site.
3. Enter a label for your site, select the type of reCAPTCHA you want to use, and enter the domain(s) where you will be using the reCAPTCHA.
4. Accept the terms of service and click on the "Submit" button.
5. You will be given a *site key* and a *secret key*. These keys will be used to integrate reCAPTCHA into your application.

### Frontend integration

*Example* - reCAPTCHA integration with button click event

```javascript
import React, { useEffect } from 'react';
import { recaptcha } from 'recaptcha-lib';
import axios from 'axios';

function App() {
    
    useEffect(() => {
        recaptcha.initiate('<SITE-KEY>');
    }, []);
            
    const submit = () => {
        recaptcha.execute((token) => {
            // Backend call for token verification
            // token should be send as a header value
            axios.get('<SERVER-TOKEN-VALIDATION-ENDPOINT>', {
                headers: {
                    "X-recaptcha-token": token
                }
            })
            .then(response => {
                // handle success
            })
            .catch(error => {
                // handle error
            });
        })};

    return (
        <div>
            <button
                onClick={submit}>
                submit
            </button>
        </div>
    );
}
                
export default App;
```

### Server side integration

*Example* - Server side reCAPTCHA token validation endpoint implementation

```javascript
const { recaptcha } = require('recaptcha-lib/server')

app.get('/', (req, res) => {
    
    const token = req.get('X-recaptcha-token');
    const secret = '<SECRET-KEY>';
    
    recaptcha.validateToken(token, secret).then(function (response) {
        // handle success and failure based on response received
        
    });
})
```

Token verification response format

```json
{
  "success": true|false,
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}
```

---
_Source: https://npm.io/package/recaptcha-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
