How to add hCaptcha to a website form.
Adding hCaptcha takes two pieces: a widget in the form, and a check on your server before you accept what the form sent. This guide covers both, from hCaptcha’s own docs, plus testing, local setup and Shopify stores, where hCaptcha is already on by default. The server code was run against hCaptcha’s test keys on 3 November 2026.
What you need first
An hCaptcha account. The Basic plan is free (see hCaptcha pricing). Its developer guide says to copy your public sitekey from the Sites page of the dashboard and your secret from Settings, and to “use the sitekey in your frontend; keep the secret on your server”. You can create one sitekey per site or one per form, for example one for signup and one for login, since behaviour and statistics are set per sitekey. To find the sitekey on a page later, see how to find an hCaptcha sitekey.
Step 1: put the widget in your form
Load hCaptcha’s script over HTTPS anywhere on the page, and add an empty element
with the class h-captcha and your sitekey in data-sitekey
inside the form:
<form action="/contact" method="POST">
<input name="email" type="email" required>
<textarea name="message" required></textarea>
<div class="h-captcha" data-sitekey="YOUR_SITEKEY"></div>
<button type="submit">Send</button>
</form>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
When a visitor passes the check, hCaptcha adds a hidden field named
h-captcha-response to the form, holding a one-time token. It is sent with the
rest of the form when the visitor presses Send. If you would rather run the check when
the button is pressed, with no checkbox on the page, hCaptcha has an invisible mode; see
invisible hCaptcha.
Step 2: check the token on your server
The widget alone protects nothing: a script can post your form with no token at all.
hCaptcha’s guide says to “only allow the protected action when success is
true”, and that “a frontend callback alone does not authorize the
request”. So the server sends the token and your secret to
https://api.hcaptcha.com/siteverify and reads the answer. In Node with
Express:
import express from 'express';
const app = express();
app.post('/contact', express.urlencoded({ extended: false }), async (req, res) => {
const token = req.body?.['h-captcha-response'];
if (!token) return res.status(400).send('Please complete the captcha.');
const check = await fetch('https://api.hcaptcha.com/siteverify', {
method: 'POST',
body: new URLSearchParams({
secret: process.env.HCAPTCHA_SECRET,
response: token,
sitekey: process.env.HCAPTCHA_SITEKEY,
remoteip: req.ip,
}),
});
const result = await check.json();
if (!result.success) return res.status(400).send('Captcha check failed. Please try again.');
// The captcha passed: send the message, save the signup, and so on.
res.send('Thanks, your message was sent.');
});
app.listen(3000); Four details from hCaptcha’s docs are built into that code:
- Form-encoded, not JSON. siteverify “expects a standard
URL-encoded form POST”. Passing a
URLSearchParamsbody tofetchsends it that way. - Send your sitekey. The docs recommend it because it “will prevent tokens issued on one sitekey from being redeemed elsewhere”.
- Send the visitor’s IP in
remoteip. It is optional, and the docs say it “helps improve verification accuracy”. Behind a reverse proxy or load balancer,req.ipis the proxy’s address unless you set Express’strust proxysetting. - One check per token. Tokens “can only be used once and must be verified within a short period of time”, so verify once, right when the form arrives.
We ran that server on 3 November 2026 with Express 5.3 and Node 22, using
hCaptcha’s test keys. The test token got the thank-you reply, a made-up token got
“Captcha check failed” (siteverify answered
invalid-input-response), and a post with no token got “Please complete
the captcha”, and so did a JSON post, which the handler doesn’t read as a
form. Every field siteverify returns is explained in
hCaptcha siteverify, and every error code in
hCaptcha error codes.
Step 3: test it before you go live
- Test keys. hCaptcha publishes a test sitekey and secret that always pass without a puzzle, for automated tests. They give “no anti-bot protection”, so swap in your real keys before launch. The keys and the dummy token are in hCaptcha test sitekeys.
- Local development. The docs say hCaptcha’s API “prohibits
localhost and 127.0.0.1 as supplied hostnames”, and a page opened from a
file://address won’t load it. Add a hosts entry and open your local server on that name:/etc/hosts127.0.0.1 test.mydomain.com - Content Security Policy. If your site sends CSP headers, hCaptcha
asks you to allow
https://hcaptcha.comandhttps://*.hcaptcha.cominscript-src,frame-src,style-srcandconnect-src, and not to hard-code a single subdomain.
If the form later comes back with “invalid captcha” for real visitors, invalid captcha goes through the usual causes.
Frameworks and plugins
You don’t have to write the widget code by hand. hCaptcha’s integrations page has setup guides for React, Vue, Node.js and WordPress, and lists libraries and plugins for Angular and other platforms. Whichever one you use, the server-side check in step 2 still applies.
Adding hCaptcha to a Shopify store
On Shopify you don’t add the script yourself. Shopify’s help center says hCaptcha “is enabled by default on all Shopify stores”, on contact, newsletter signup and comment forms, and on login, create account and password recovery pages. To turn it on or off:
- In your Shopify admin, go to Online Store > Preferences.
- In the Spam protection section, check or uncheck Enable hCaptcha on contact and comment forms and Enable hCaptcha on login, create account, and password recovery pages.
Shopify adds that it “isn’t compatible with custom code or third-party apps
that add CAPTCHA”, so remove another captcha app before relying on it. If your theme
builds its own forms, Shopify’s
developer docs say hCaptcha finds them by
the form’s action and a hidden form_type input, which
Liquid’s form tag writes for you. They also say the theme must keep
the content_for_header object. A form you submit from JavaScript can be
wired up with the data-shopify-captcha attribute or with
window.Shopify.captcha.protect, and the docs say not to call
window.hCaptcha directly:
<form data-shopify-captcha="true" action="/contact" method="post">
<input type="hidden" name="form_type" value="contact">
...
</form> Checkout is separate. Shopify’s bot protection for flash sales lets you pick reCAPTCHA or hCaptcha as the checkpoint challenge, for up to 60 minutes per event, and Shopify Plus Support has to activate it first.
Testing your own form end to end
The test keys cover most automated tests. If you also run end-to-end tests against your real sitekey, for example a staging check that the whole signup flow still works, the test needs a real token. NoneCap is a captcha solver service that focuses on hCaptcha: its hCaptcha solver returns a token for the sitekey and page you send through the solve API, and a solve that returns no token is never charged.
curl "https://api.nonecap.com/v1/solves?wait=90" \
-H "Authorization: Bearer $NONECAP_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "hcaptcha",
"sitekey": "YOUR_SITEKEY",
"url": "https://staging.example.com/contact"
}' Last updated November 2026.
Frequently asked
How do I add hCaptcha to my website form?
api.js script on the page, and put an empty div with the h-captcha class and your sitekey in data-sitekey inside the form. Then, on your server, send the submitted h-captcha-response value and your secret to https://api.hcaptcha.com/siteverify and only accept the form when the answer has success: true.Is the widget enough on its own?
Is hCaptcha free to add to a website?
Where do I find my hCaptcha sitekey and secret?
Can I test hCaptcha on localhost?
127.0.0.1 test.mydomain.com and open your local server on that name, or use hCaptcha’s test keys, which always pass. See hCaptcha test sitekeys.