https://github.com/rodneylab/hcaptcha-serverless-rust-worker
Demo code for creating an hCaptcha verify endpoint using Cloudflare Workers written in Rust
https://github.com/rodneylab/hcaptcha-serverless-rust-worker
cloudflare-workers hcaptcha rust serverless wasm
Last synced: 3 months ago
JSON representation
Demo code for creating an hCaptcha verify endpoint using Cloudflare Workers written in Rust
- Host: GitHub
- URL: https://github.com/rodneylab/hcaptcha-serverless-rust-worker
- Owner: rodneylab
- License: apache-2.0
- Created: 2021-09-22T15:21:03.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-25T10:44:35.000Z (over 3 years ago)
- Last Synced: 2025-02-26T00:28:45.612Z (11 months ago)
- Topics: cloudflare-workers, hcaptcha, rust, serverless, wasm
- Language: Rust
- Homepage:
- Size: 115 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE_APACHE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README

hCaptcha Serverless Rust Worker
# hcaptcha-serverless-rust-worker
[](https://open.vscode.dev/rodneylab/hcaptcha-serverless-rust-worker)
**Cloudflare Worker written in Rust to create an hCaptcha verification endpoint.**
To learn how to code this up from scratch in Rust using wrangler, see the article on using Rust in serverless Cloudflare workers. If you have any questions, please drop a comment at the bottom of that page.
If you would like to use the Worker read on!
You will need a Cloudflare account as well as an hCaptcha account. Both services have a free tier.
1. Start by cloning this repo:
```shell
git clone https://github.com/rodneylab/hcaptcha-serverless-rust-worker
cd hcaptcha-serverless-rust-worker
```
1. Continue by setting up a Cloudflare account if you do not yet have one.
1. Now create an hCaptcha account, if you don't yet have one.
1. If you do not yet have a Rust development environment set up on your machine, head over to the official Rust site for the recommended one-line terminal command to get that up and running.
1. Install the wrangler tool on your machine:
```shell
cargo install wrangler
```
1. Next link your Cloudflare account to your local environment:
```shell
wrangler login
```
1. Now we will define some variables. Start with your hCaptcha site key (get this from the hCaptcha dashboard):
```
wrangler secret put HCAPTCHA_SITEKEY
```
paste in your site key when prompted.
1. Repeat with your hCaptcha secret key
```
wrangler secret put HCAPTCHA_SECRETKEY
```
1. Finally we will define the CORS origins. This is a comma separated list of valid domains you want to be able to send requests from (typically your live client site and a local development site). If you have the the following domains:
- https://www.example.com,
- http://127.0.0.1:3000,
Enter the secret as `https://www.example.com,http://127.0.0.1:3000` when prompted.
Let's define this now then:
```
wrangler secret put CORS_ORIGIN
```
That should be everything set up.
## Testing
1. Now fire up the dev server for testing:
```
wrangler dev
```
By default the worker will be available locally at `http://127.0.0.1:8787`.
1. Now you need to generate an hCaptcha request from a client app and send it to `http://127.0.0.1:8787/verify` as JSON, using the `POST` method. If you are working in JavaScript, you might code this up something like this:
```javascript
async function handleVerify() {
try {
if (browser) {
const { response } = await hcaptcha.execute(hcaptchaWidgetID, {
async: true,
});
const responsePromise = fetch(`${workerUrl}/verify`, {
method: 'POST',
credentials: 'omit',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
response,
}),
});
}
} catch (error) {
console.error(`Error in handleVerify: ${error}`);
}
}
```