Integrations

Express.js

Express.js integration guide

Integration guide for Express.js applications.

Full Example

A complete working example is available on GitHub: Captchacat-Integrations/ExpressJs

Configuration#

Set your Captchacat credentials:

app.js
const CAPTCHACAT_API_KEY = "";   // Your API key
const CAPTCHACAT_SITE_KEY = "";  // Your site key

Frontend#

Create a form with the CAPTCHA widget:

views/index.ejs
<!DOCTYPE html>
<html>
<head>
  <title>Login Form</title>
  <script src="https://challenge.captchacat.com/ray/widget.js"></script>
</head>
<body>
  <form action="/submit" method="POST">
    <input type="text" name="username" placeholder="Username" required />
    <input type="password" name="password" placeholder="Password" required />
 
    <div class="captcha-widget" data-sitekey="<%= siteKey %>"></div>
 
    <button type="submit">Sign In</button>
  </form>
</body>
</html>

Routes#

app.js
const express = require("express");
const app = express();
 
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
 
// Render the form
app.get("/", (req, res) => {
  res.render("index", {
    siteKey: CAPTCHACAT_SITE_KEY,
  });
});
 
// Handle form submission
app.post("/submit", async (req, res) => {
  const token = req.body["captchacat-token"];
 
  if (!token) {
    return res.status(400).send("Please complete the CAPTCHA");
  }
 
  // Validate the token
  const response = await fetch("https://challenge.captchacat.com/validate_token", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      api_key: CAPTCHACAT_API_KEY,
      token: token
    })
  });
 
  if (response.ok) {
    // Token valid - process the form
    res.send("Login successful!");
  } else {
    res.status(400).send("CAPTCHA verification failed");
  }
});
 
app.listen(3000);

Built with precision. Designed for developers.