Integrations

Go

Go integration with standard library

Integration guide for Go applications using the standard library.

Full Example

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

Configuration#

Set your Ray CAPTCHA credentials:

main.go
const (
    apiKey  = "" // Your API key
    siteKey = "" // Your site key
)

Token Validation#

Create a struct and function to validate the CAPTCHA token:

main.go
import (
    "bytes"
    "encoding/json"
    "net/http"
)
 
type TokenValidation struct {
    APIKey string `json:"api_key"`
    Token  string `json:"token"`
}
 
func validateToken(token string) (bool, error) {
    payload := TokenValidation{
        APIKey: apiKey,
        Token:  token,
    }
 
    jsonData, err := json.Marshal(payload)
    if err != nil {
        return false, err
    }
 
    resp, err := http.Post(
        "https://challenge.captchacat.com/validate_token",
        "application/json",
        bytes.NewBuffer(jsonData),
    )
    if err != nil {
        return false, err
    }
    defer resp.Body.Close()
 
    return resp.StatusCode >= 200 && resp.StatusCode < 300, nil
}

Template#

Create a form template with the widget:

templates/index.html
<!DOCTYPE html>
<html>
<head>
  <title>Login Form</title>
</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">Login</button>
  </form>
 
  <script src="https://challenge.captchacat.com/ray/widget.js"></script>
</body>
</html>

HTTP Handlers#

main.go
import (
    "html/template"
    "net/http"
)
 
type PageData struct {
    SiteKey string
}
 
func handleIndex(w http.ResponseWriter, r *http.Request) {
    tmpl, err := template.ParseFiles("templates/index.html")
    if err != nil {
        http.Error(w, "Template error", http.StatusInternalServerError)
        return
    }
 
    tmpl.Execute(w, PageData{
        SiteKey: siteKey,
    })
}
 
func handleSubmit(w http.ResponseWriter, r *http.Request) {
    if r.Method != http.MethodPost {
        http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
        return
    }
 
    if err := r.ParseForm(); err != nil {
        http.Error(w, "Failed to parse form", http.StatusBadRequest)
        return
    }
 
    token := r.FormValue("captchacat-token")
 
    if token == "" {
        http.Error(w, "CAPTCHA required", http.StatusBadRequest)
        return
    }
 
    valid, err := validateToken(token)
    if err != nil || !valid {
        http.Error(w, "CAPTCHA verification failed", http.StatusBadRequest)
        return
    }
 
    // Token valid - process the form
    username := r.FormValue("username")
    // ... handle form data
 
    w.Write([]byte("Login successful!"))
}
 
func main() {
    http.HandleFunc("/", handleIndex)
    http.HandleFunc("/submit", handleSubmit)
 
    http.ListenAndServe(":8080", nil)
}

Built with precision. Designed for developers.