How to add reCAPTCHA protection to a GatsbyJS or ReactJS project

Oscar de la Hera Gomez
Three flowers that represent ReactJS, reCAPTCHA and GatsbyJS side by side. Beneath them sits the text "reCAPTCHA."

A step by step guide on adding reCAPTCHA v2 to a GatsbyJS or ReactJS app.

Please note that this tutorial only covers how to use reCAPTCHA v2 on the front-end without backend validation. If you are seeking to authenticate the reCAPTCHA v2 token that is produced, follow the tutorial below.

Step One: Setup the reCaptcha Site

A screenshot of Google's reCAPTCHA website showing you how to create a new page. We have highlighted the labels and domains. Make sure that you add localhost to guarantee local development, as well as all the other relevant domains (i.e. development, staging or production).

Navigate to Google's reCAPTCHA website and setup your website.

Make sure that you add localhost, any development or staging environment domains as well as the public domain.

Once you're done, press Submit and you will be taken to a page similar to the one below.

A screenshot of Google's reCAPTCHA website showing the page that appears after configuring a site. You an copy the SITE KEY and SECRET KEY by pressing the button with the relevant button.

Copy the SITE KEY and SECRET KEY.

If by any chance you already created the reCAPTCHA site, the SITE KEY and SECRET KEY can be found by pressing the gear (settings) icon under the Google reCAPTCHA website.

A screenshot of Google's reCAPTCHA website showing highlighting the gear icon that appears on the top right of the page for a configured site.

In the page that appears, open the reCAPTCHA keys section, to find the SITE KEY and SECRET KEY.

A screenshot of Google's reCAPTCHA website showing settings page for a configured site. Highlighted is the open “reCAPTCHA keys” section that shows how you can copy the SITE KEY and SECRET KEY.

Step Two: Add the Site Key as an Environment Variable

A screenshot of a .env file showing the SITE KEY and SECRET KEY environment variables.

In your GatsbyJS or ReactJS project, add environment variables for the SITE KEY.

Please note that the reCAPTCHA v2 SECRET KEY is required to verify the token. This should be done in the backend.

Step Three: Add the Dependencies

A screenshot of Terminal showing you how to add the react-google-recaptcha package.

Open Terminal and with the current directory set to that of your project and run the following line of code:

yarn add react-google-recaptcha

If you're using Typescript, make sure to add the relevant types:

@types/react-google-recaptcha

If you're using the GatsbyJS, you will need to add the following plugin:

yarn add gatsby-plugin-recaptcha

Step Four: Add Plugin Configuration (GatsbyJS Only)

A screenshot of VSCode showing the gatsby-config.js file and how we have added the configuration for the gatsby-plugin-recaptcha that's provided below.

Please note this step only applies to GatsbyJS.

In the gatsby-config.js file, add the configuration for the gatsby-plugin-recaptcha using code similar to that below.

Step Five: Add reCAPTCHA protection

A screenshot of delasign.com showing the reCAPTCHA protection on the contact form.

To add reCAPTCHA protection, import and apply the module using the code below.

Step Six: Verification

A screenshot of delasign.com showing the alert bar which states "Please verify that you are not a robot." This appears if a user tries to submit a form with enough data but has not passed the reCAPTCHA test.

To know if a reCAPTCHA test passed or not, you must check the Token is empty.

To gather the Token, get current value of the reference created in Step Five.

  • If it's empty (reCaptchaElement.current.getValue() != ""), then it has not yet been completed.
  • If it is not empty, the user successfully demonstrated that it is not a robot.

Please note that in order to guarantee that the token was produced by a human, you must authenticate it with Google using your Secret Key.

Next Steps: Authenticate the reCAPTCHA Token

To learn how to authenticate a reCAPTCHA token, follow the tutorial below.

Please note that you can authenticate the token on the front-end but, for security purposes, it is strongly recommended that you authenticate the reCAPTCHA token on the backend.

Frequently Asked Questions (FAQ)

How do I reset the reCAPTCHA for subsequent checks ?

To reset the reCAPTCHA, reset the current value of the ref using the following code:

reCaptchaElement.current.reset()

How do I make the reCaptcha show the selection challenge (i.e. select all squares with traffic lights) ?

A screenshot of delasign.com showing the puzzle that appears if you have the highest verification.

To show the most secure version of the reCAPTCHA v2 checkbox, navigate to the settings page for your site and under Security Preference slide the slider to Most Secure.

Make sure you press save so that the update registers.

A screenshot of the Google reCAPTCHA v2 site settings page. Highlighted is the "Secure Preference" section. We have set the security preference to "most secure" by sliding the slider to the far right.

How do I get alerts if Google detects reCAPTCHA problems with my site ?

A screenshot of the Google reCAPTCHA v2 site settings page. We have highlighted the "send alerts to owners" checkbox that appears at the bottom of the page.

To get alerted about misconfigurations or an increase in suspicious traffic, navigate to your sites settings and check Send alerts to owners.

Make sure you press save so that the update registers.

Do I need to authenticate a reCAPTCHA token?

You do not need to verify (authenticate) the token to get the basic functionality out of reCAPTCHA.

However, it is strongly recommended to verify the token to have an extra layer of authentication, guaranteeing that it is not a robot.

How do I authenticate that a reCAPTCHA token was produced by a human?

To authenticate that a reCAPTCHA token was produced by a human, you must create a backend service that calls an Google API with the SECRET KEY and TOKEN.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details