# Captcha

Middleware captcha provides captcha service for Macaron [Instances](/core_concepts.md#instances).

* [GitHub](https://github.com/go-macaron/captcha)
* [API Reference](https://gowalker.org/github.com/go-macaron/captcha)

### Installation

```bash
go get github.com/go-macaron/captcha
```

## Usage

{% hint style="info" %}
To use this middleware, you have to register [cache](/middlewares/cache.md) first.
{% endhint %}

```go
// main.go
import (
    "github.com/go-macaron/cache"
    "github.com/go-macaron/captcha"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.Classic()
    m.Use(cache.Cacher())
    m.Use(captcha.Captchaer())

    m.Get("/", func(ctx *macaron.Context, cpt *captcha.Captcha) string {
        if cpt.VerifyReq(ctx.Req) {
            return "valid captcha"
        }
        return "invalid captcha"
    })

    m.Run()
}
```

```markup
<!-- templates/hello.tmpl -->
{{.Captcha.CreateHtml}}
```

## Options

`captcha.Captchaer` comes with a variety of configuration options:

```go
// ...
m.Use(captcha.Captchaer(captcha.Options{
    // URL prefix of getting captcha pictures. Default is "/captcha/".
    URLPrefix:            "/captcha/",
    // Hidden input element ID. Default is "captcha_id".
    FieldIdName:        "captcha_id",
    // User input value element name in request form. Default is "captcha".
    FieldCaptchaName:    "captcha",
    // Challenge number. Default is 6.
    ChallengeNums:        6,
    // Captcha image width. Default is 240.
    Width:                240,
    // Captcha image height. Default is 80.
    Height:                80,
    // Captcha expiration time in seconds. Default is 600.
    Expiration:            600,
    // Cache key prefix captcha characters. Default is "captcha_".
    CachePrefix:        "captcha_",
}))
// ...
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://go-macaron.com/middlewares/captcha.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
