SvelteKit hCaptcha Contact Form: Keeping Bots Away

SvelteKit hCaptcha Contact Form

SvelteKit hCaptcha Contact Form: Keeping Bots Away


📝 hCaptcha Forms in SvelteKit

In this post we look at a SvelteKit hCaptcha contact form for your Svelte site. hCaptcha is an alternative to Google reCAPTCHA. Both can be used to reduce spam submissions on your site's forms. hCaptcha claims to protect user privacy . By asking users to complete the hCaptcha challenge before submitting your form, you can filter some responses and further scrutinise them, based on the hCaptcha verdict.

There are two parts to the hCaptcha verification. The first is on the client side (frontend), where we ask the user to complete the challenge. We send the user challenge responses to hCaptcha straight away (from the client). hCaptcha then responds with a response code. That response code is needed in the second part of the process, which is completed in the backend. We will see how you can use Cloudflare workers to perform the backend part if you want to build a static SvelteKit site. If, however, you prefer server side rendered, we cover you back with some sample code for handling that in SvelteKit too.

If that all sounds exciting, why don't we crack on?

🧱 hCaptcha Forms in SvelteKit

SvelteKit hCaptcha Contact Form: What we're Building: image shows a screenshot of a contact form.  Haeding is Drop me a message. There are input fields for a name, email address and message as well as a Submit form button
SvelteKit hCaptcha Contact Form: What we're Building

The plan of action is the following:

  1. Clone the SvelteKit blog MDsveX starter , so we can hit the ground running.

  2. Add a contact form.

  3. Add the hCaptcha client code.

  4. Look at how Cloudflare workers can be used for the server side verification.

  5. Try an alternative server side rendered implementation.

🗳️ Poll

Do you mostly create static or server side rendered (SSR) site with SvelteKit?
Voting reveals latest results.

⚙️ Getting Started

Let's get started by cloning the SvelteKit blog MDsveX starter :

git clone sveltekit-hcaptcha-form
cd sveltekit-hcaptcha-form
pnpm install
cp .env.EXAMPLE .env
pnpm run dev

We will also use some components from a SvelteKit component library to speed up development. Let's install those now too:

pnpm install -D @rodneylab/sveltekit-components

Finally, you will need hCaptcha credentials to test out your code. See instructions on setting up a free hCaptcha account in the article on Serverless hCaptcha or just head to the hCaptcha site . Once you have credentials add them to your .env file:


The first two credentials will be accessed by the client side so they need the VITE_ prefix.

As a last piece of setup, import the dotenv package in your svelte.config.js file:

1/** @type {import('@sveltejs/kit').Config} */
2import 'dotenv/config';
3import adapter from '@sveltejs/adapter-static';

Then we allow access to client components in src/lib/config/website.js:

22wireUsername: import.meta.env ? import.meta.env.VITE_WIRE_USERNAME : '',
23 hcaptchaSitekey: import.meta.env ? import.meta.env.VITE_HCAPTCHA_SITEKEY : '',
24 workerUrl: import.meta.env ? import.meta.env.VITE_WORKER_URL : '',

With the setup out of the way, if this is your first time using the starter, have a skim through the files and folders of the project. Also head to localhost:3000/ and do some clicking around, to familiarise yourself with the site. When you're ready to carry on.

Hooks Configuration

We just need to tweak the hooks configuration for everything to run smoothly. The src/hooks.js file in the project includes Content Security Policy (CSP) headers. These are an added security measure which only allow the browser to connect to certain hosts. For any site you build with the starter, you will probably need to tweak this file. We need to allow connections to hCaptcha and our Cloudflare worker for this project:

8const directives = {
9 'base-uri': ["'self'"],
10 'child-src': ["'self'"],
11 // 'connect-src': ["'self'", 'ws://localhost:*'],
12 'connect-src': [
13 "'self'",
14 'ws://localhost:*',
15 '',
16 'https://*',
17 process.env['VITE_WORKER_URL'],
18 ],
19 'img-src': ["'self'", 'data:'],
20 'font-src': ["'self'", 'data:'],
21 'form-action': ["'self'"],
22 'frame-ancestors': ["'self'"],
23 'frame-src': [
24 "'self'",
25 // "https://*",
26 // "https://*",
27 // "https://*",
28 '',
29 'https://*',
30 ],
31 'manifest-src': ["'self'"],
32 'media-src': ["'self'", 'data:'],
33 'object-src': ["'none'"],
34 // 'style-src': ["'self'", "'unsafe-inline'"],
35 'style-src': ["'self'", "'unsafe-inline'", '', 'https://*'],
36 'default-src': [
37 "'self'",
38 rootDomain,
39 `ws://${rootDomain}`,
40 // 'https://*',
41 // 'https://*',
42 // 'https://*',
43 // 'https://*',
44 // 'https://*',
45 // 'https://*',
46 // 'https://*',
47 // 'https://*',
48 // 'https://*',
49 // 'https://*',
50 ],
51 'script-src': [
52 "'self'",
53 "'unsafe-inline'",
54 // 'https://*',
55 // 'https://*',
56 // 'https://*',
57 '',
58 'https://*',
59 // 'https://*',
60 // '',
61 ],
62 'worker-src': ["'self'"],
63 // remove report-to & report-uri if you do not want to use Sentry reporting
64 'report-to': ["'csp-endpoint'"],
65 'report-uri': [
66 `${import.meta.env.VITE_SENTRY_PROJECT_ID}/security/?sentry_key=${
67 import.meta.env.VITE_SENTRY_KEY
68 }`,
69 ],

You will need to make these changes during development, whether you are creating a static or server side rendered site. For a static production site, the file is not used. You can add HTTP headers to achieve the same effect. Check how to do this with your hosting platform.

🧑🏽 Contact Form

Here's the code for the basic contact form. We are using the component library to save us typing out all the boiler plate needed for accessible form inputs. You can see how to create you own SvelteKit component library in a recent video post. Paste the code into a new file at src/lib/components/ContactForm.svelte:

2 import { EmailInputField, TextArea, TextInputField } from '@rodneylab/sveltekit-components';
3 import website from '$lib/config/website';
4 import { onMount, onDestroy } from 'svelte';
5 import { browser } from '$app/env';
7 const { hcaptchaSitekey, workerUrl } = website;
9 onMount(() => {
11 });
13 onDestroy(() => {
15 });
17 let name = '';
18 let email = '';
19 let message = '';
20 let errors: {
21 name?: string;
22 email?: string;
23 message?: string;
24 };
25 $: errors = {};
26 $: submitting = false;
28 function clearFormFields() {
29 name = '';
30 email = '';
31 message = '';
32 }
34<form class="form" on:submit|preventDefault={handleSubmit}>
35 <h2>Drop me a message</h2>
36 <TextInputField
37 id="form-name"
38 value={name}
39 placeholder="Your name"
40 title="Name"
41 error={errors?.name ?? null}
42 on:update={(event) => {
43 name = event.detail;
44 }}
45 style="padding-bottom:1rem"
46 />
47 <EmailInputField
48 id="form-email"
49 value={email}
50 placeholder="[email protected]"
51 title="Email"
52 error={errors?.email ?? null}
53 on:update={(event) => {
54 email = event.detail;
55 }}
56 style="width:100%;padding-bottom:1rem"
57 />
58 <TextArea
59 id="form-message"
60 value={message}
61 placeholder="Enter your message here"
62 title="Message"
63 error={errors?.message ?? null}
64 on:update={(event) => {
65 message = event.detail;
66 }}
67 style="padding-bottom:1rem"
68 />
69 <button type="submit" disabled={submitting}>Submit form</button>
72<style lang="scss">
73 .form {
74 display: flex;
75 flex-direction: column;
76 width: 80%;
77 margin: $spacing-6 auto;
78 }
79 button {
80 cursor: pointer;
81 padding: $spacing-2 $spacing-0;
82 }

The EmailInputField, TextArea and TextInputField components come from the component library. They make use of Svelte's component events to keep the value displayed in sync with the email, message and name variables in this component. Follow the previous link to the Svelte tutorial if you are not yet familiar with this API.

To stop this post getting too long, we won't go into detail on the rest of the form code here. That said, do let me know if you would appreciate a separate post on Svelte forms and binding form fields to variables.

🤖 Adding hCaptcha

We will add the client hCaptcha script directly to the DOM. You have probably seen this pattern if you have looked at tracking or analytics code previously. In SvelteKit, you will see you don't need to add any extra packages to make this work. Before we do that, let's actually load the script in the component onMount function:

9let hcaptcha = { execute: async (_a, _b) => ({ response: '' }), render: (_a, _b) => {} };
10 let hcaptchaWidgetID;
12 onMount(() => {
13 if (browser) {
14 hcaptcha = window.hcaptcha;
15 if (hcaptcha.render) {
16 hcaptchaWidgetID = hcaptcha.render('hcaptcha', {
17 sitekey: hcaptchaSitekey,
18 size: 'invisible',
19 theme: 'dark',
20 });
21 }
22 }
23 });
25 onDestroy(() => {
26 if (browser) {
27 hcaptcha = { execute: async () => ({ response: '' }), render: () => {} };
28 }
29 });

We are adding an invisible hCaptcha, so we will use the hcaptchaWidgetID variable to identify it. The first lines are just there to keep types consistent and to be able to link and unlink the hCaptcha script to a local variable during component creation and destruction. We add our hCaptcha site key in the hCaptcha initialisation, within onMount.

Next we need a handleSubmit function:

43async function handleSubmit() {
44 try {
45 const { response: hCaptchaResponse } = await hcaptcha.execute(hcaptchaWidgetID, {
46 async: true,
47 });
48 /* for a static site, you can use a Cloudflare worker to manage the server part of the
49 * hCaptcha and send your site admin an email with the contact details
50 *
51 * in this case, use:
52 *
53 * fetch(`${workerUrl}/verify`, {
54 *
55 * for a server side rendered app, use the verify endpoint to do the processing:
56 *
57 * fetch('/verify.json', {
58 */
59 fetch(`${workerUrl}/verify`, {
60 method: 'POST',
61 credentials: 'omit',
62 headers: {
63 'Content-Type': 'application/json',
64 },
65 body: JSON.stringify({
66 name,
67 email,
68 message,
69 response: hCaptchaResponse,
70 }),
71 });
72 console.log('Details: ', { name, email, message });
73 clearFormFields();
74 } catch (error) {
75 console.error('Error in contact form submission');
76 }
77 }

The function starts with a hcaptcha.execute function call. This displays the captcha and waits for the user to complete it. It then contacts hCaptcha to get a response which we will need for the second part. Interestingly, execute gathers information on mouse movement while solving the challenge as well as the user answers.

The rest of the function includes two possibilities. If we have a static site, we can send our form data and the hCaptcha response to a Cloudflare worker for processing. If you are a SvelteKit purist and go for a server side rendered site, you can send the request to a SvelteKit endpoint. Let's look at both ways in more detail in a moment.

As we mentioned earlier, we can add the hCaptcha script to the DOM:

81 <script src="" async defer></script>

Then we need a placeholder div for it to render:

119<button type="submit" disabled={submitting}>Submit form</button>
120 <div
121 id="hcaptcha"
122 class="h-captcha"
123 data-sitekey={hcaptchaSitekey}
124 data-size="invisible"
125 data-theme="dark"
126 />

🔗 SvelteKit hCaptcha Contact Form: Linking it all Up

Importantly, we should import the ContactForm component on the contact page, so we can render it:

19import ContactForm from '$lib/components/ContactForm.svelte';
84<ContactForm />
86<style lang="scss"

🤖 Adding hCaptcha: Rust Cloudflare Worker Style

Cloudflare workers run in a Web Assembly (WASM) environment, which means you can write your code in Rust or even C++ instead of JavaScript if you choose. I like this as a solution because if you are building client sites in SvelteKit as well as other frameworks, you only need to maintain one codebase for parts of your backend. You can use the same code for contact form submission from your SvelteKit and Next apps. Rust also offers opportunities for code optimisation. You can see how to set up a Rust Cloudflare service worker to handle hCaptcha in a recent post. For local testing, you will probably have your worker running on, which is the value we defined in the .env file. You will just need to set it up to listen for POST requests on the /verify route.

SvelteKit hCaptcha Contact Form: Cloudflare Worker output: image shows console output from successful Cloud flare worker request
SvelteKit hCaptcha Contact Form: Cloudflare Worker Output

🔥 Adding hCaptcha: SvelteKit Server Side Route Style

Finally let's check the SvelteKit way to handle the hCaptcha server side work. Create a new file at src/routes/verify.json.js and paste in the following code:

1export async function post(request) {
2 try {
3 const { name, email, message, response: hCaptchaClientResponse } = request.body;
5 const secret = process.env['HCAPTCHA_SECRETKEY'];
6 const sitekey = process.env['VITE_HCAPTCHA_SITEKEY'];
7 const body = new URLSearchParams({ response: hCaptchaClientResponse, secret, sitekey });
9 const response = await fetch('', {
10 method: 'POST',
11 credentials: 'omit',
12 headers: {
13 'Content-Type': 'application/x-www-form-urlencoded',
14 },
15 body: body.toString(),
16 });
18 const data = await response.json();
19 const { success } = data;
20 console.log('data: ', data);
21 if (success) {
22 console.log('hCaptcha says yes!');
23 } else {
24 console.log('hCaptcha says no!');
25 }
27 // process name, email and message here e.g. email site admin with message details
28 console.log({ name, email, message });
30 return {
31 status: 200,
32 };
33 } catch (err) {
34 const error = `Error in /verify.json.js: ${err}`;
35 console.error(error);
36 return {
37 status: 500,
38 error,
39 };
40 }

The hCaptcha request needs to be submitted as form data and the response is JSON. A successful field on the response indicates whether hCaptcha considers the user a bot or not. For more details pull up the hCaptcha docs .

SvelteKit hCaptcha Contact For: CORS errors

If you get CORS errors testing the site, you should try tweaking your DNS settings. This involves creating a hostname proxy for (localhost). On MacOS you can add the following line to the /private/etc/hosts file:


Then, instead of accessing the site via http://localhost:3030, in your browser use This worked for me on macOS. The same will work on typical Linux and Unix systems, though the file you change will be /etc/hosts. If you are using DNSCryprt Proxy or Unbound, you can make a similar change in the relevant config files. If you use windows and know how to do this, please drop a comment below to help out other windows users.

🙌🏽 SvelteKit hCaptcha Contact Form: What we Learned

SvelteKit hCaptcha Contact Form: hCaptcha Test

We have just covered the basics here. In a real-world app, you should add verification, at least on the server side. Feedback on the client side is a good idea too to improve user experience.

In this post we learned:

  • how to use hCaptcha with SvelteKit,

  • a way to integrate Rust Cloudflare workers into a static site, making it easier to share code across different frameworks,

  • tweaking the Content Security Policy via the hooks.js file to allow connection to external hosts.

I do hope there is at least one thing in this article which you can use in your work or a side project. As always get in touch with feedback if I have missed a trick somewhere!

You can see the full code for this SvelteKit hCaptcha Contact Form project on the Rodney Lab Git Hub repo .

🙏🏽 SvelteKit hCaptcha Contact Form: Feedback

Have you found the post useful? Do you have your own methods for solving this problem? Let me know your solution. Would you like to see posts on another topic instead? Get in touch with ideas for new posts. Also if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.

Thanks for reading this post. I hope you found it valuable. Please get in touch with your feedback and suggestions for posts you would like to see. Read more about me

Rodney from Rodney Lab


  • TSR Codes profile avatar
  • Svelte Society 🧡 profile avatar
Reposts & likes provided by Twitter via Webmentions.

Leave a comment

Your information will be handled in line with our Privacy Policy.