How to authenticate a reCAPTCHA token

Oscar de la Hera Gomez
Four flowers that represent AWS Lambda, Google reCAPTCHA, Typescript and Serverless side by side. Beneath them sits the text "reCAPTCHA Authentication."

A step by step guide on verifying that a reCAPTCHA v2 token came from a human and not a robot using Serverless, AWS Lambda and Typescript.

The following tutorial demonstrates how to authenticate a token produced by reCAPTCHA v2 on the frontend, and which is sent to the backend via an API call for authentication.

If you are seeking to setup reCAPTCHA v2 on a react front-end, please complete the tutorial linked below before carrying out this portion.

This tutorial makes use of functionality that is available on our Typescript and Serverless Starter Project. If you are seeking to jumpstart the process, please consult the tutorial below.

PLEASE NOTE THAT THIS CAN BE DONE ON THE FRONT-END BUT FOR SECURITY REASONS, WE STRONGLY RECOMMEND THAT YOU AUTHENTICATE A RECAPTCHA TOKEN ON THE BACKEND VIA AN API.

Step One: Gather the 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.

In the reCAPTCHA website, under the settings of your site copy the SECRET KEY.

Step Two: Add the Environment Variable

A screenshot of a VSCode serverless.yml file showing how we added the SECRET KEY as an environment variable.

Add the SECRET KEY to your projects environment variables.

Step Three: Add the Dependencies

A screenshot of Terminal showing how Axios was added to the project dependencies.

In Terminal, with the directory set to that of the project, add the axios dependency using the code below:

yarn add axios

Step Four: Authenticate the Token

A screenshot of VSCode showing the function that is available below that we use to authenticate the token.

In your project, authenticate the token using a function similar to the one below.

To learn more about our logging system, consult the article below.

Step Five: Test

A screenshot of AWS Cloudwatch logs showing that the token was authenticated successfully and that the request came from a human.

AWS Cloudwatch logs from our contact lambda function demonstrating that the reCAPTCHA authentication was successful and that the request was made by a human.

Deploy and test the reCAPTCHA.

View the Lambda AWS Cloudwatch logs of your function to confirm that the reCAPTCHA token authentication was successful.

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