Captcha OCR

SDKs / Go

Go SDK

Package captchaocr.allvoraz.com/sdk/go, version 1.0.0. Requires Go 1.23+.

Install

The SDK installs straight from our package server, captchaocr.allvoraz.com/sdk, with your usual tools. No public registry account is needed.

# Go modules (served by our own module proxy; nothing to configure)
go get captchaocr.allvoraz.com/sdk/go@v1.0.0

# then:
#   import captchaocr "captchaocr.allvoraz.com/sdk/go"

Prefer the code itself? Download the source (.zip).

Configure and solve

Create the client once with your API key (from Dashboard → API keys) and reuse it. Submit the answer only when reliable is true. Only reliable answers are charged.

import (
	"context"
	"encoding/base64"
	"fmt"
	"os"

	captchaocr "captchaocr.allvoraz.com/sdk/go"
)

cfg := captchaocr.NewConfiguration()
cfg.Servers = captchaocr.ServerConfigurations{{URL: "https://captchaocr.allvoraz.com"}}
client := captchaocr.NewAPIClient(cfg)
// The API key travels in the context.
ctx := context.WithValue(context.Background(), captchaocr.ContextAccessToken, os.Getenv("CAPTCHA_API_KEY"))

data, _ := os.ReadFile("captcha.png")
req := captchaocr.NewSolveRequest(base64.StdEncoding.EncodeToString(data))
req.SetNumeric(true)
req.SetLength(6)

res, _, err := client.SolveAPI.Solve(ctx).SolveRequest(*req).Execute()
if err != nil {
	panic(err)
}
if res.GetReliable() {
	fmt.Println(res.GetText()) // only reliable answers are charged
}

Options: numeric, uppercase, lowercase, length, math, hindi. See choosing options.

Batch

Up to 256 images in one call. Top-level options are shared, and each item can override them. Results keep the request order.

b64 := func(path string) string {
	data, _ := os.ReadFile(path)
	return base64.StdEncoding.EncodeToString(data)
}

a := captchaocr.NewBatchItem(b64("a.png"))
b := captchaocr.NewBatchItem(b64("b.png"))
b.SetLength(5) // per-image override
sum := captchaocr.NewBatchItem(b64("sum.png"))
sum.SetMath(true)

req := captchaocr.NewBatchRequest([]captchaocr.BatchItem{*a, *b, *sum})
req.SetNumeric(true) // shared by every image

batch, _, err := client.SolveAPI.SolveBatch(ctx).BatchRequest(*req).Execute() // ctx carries the API key
if err != nil {
	panic(err)
}
for _, r := range batch.Results { // same order as the request
	fmt.Println(r.GetText(), r.GetReliable())
}

Submit now, fetch later

submit returns a task id immediately and solves in the background. getResult returns processing, then ready with the same answer solve returns (or failed, credit returned). Pass wait (up to 30 s) to hold the call until it's ready.

// Submit: returns a task id at once, the captcha is solved in the background.
task, _, err := client.SolveAPI.Submit(ctx).SolveRequest(*req).Execute()
if err != nil {
	panic(err)
}

// ... do other work ...

r, _, err := client.SolveAPI.GetResult(ctx, task.GetId()).Wait(10).Execute() // waits up to 10 s
if err != nil {
	panic(err)
}
if answer := r.GetResult(); r.GetStatus() == "ready" && answer.GetReliable() {
	fmt.Println(answer.GetText())
}

Or poll without holding the connection:

// Poll without holding the connection.
for r.GetStatus() == "processing" {
	time.Sleep(500 * time.Millisecond)
	r, _, err = client.SolveAPI.GetResult(ctx, task.GetId()).Execute()
	if err != nil {
		panic(err)
	}
}
answer := r.GetResult()
fmt.Println(r.GetStatus(), answer.GetText())

Balance

bal, _, err := client.AccountAPI.Balance(ctx).Execute()
if err != nil {
	panic(err)
}
fmt.Println(bal.Credits)

Low-balance alert

One alert per account: when a solve leaves your credits below threshold, the service POSTs your payload to your HTTPS url. It fires once and re-arms when you add credits. testAlert sends it now; getAlert and deleteAlert read and remove it. See how alerts work.

bal, _, _ := client.AccountAPI.Balance(ctx).Execute()
fmt.Println(bal.Credits)

// When credits drop below 500, POST this payload to your URL (one alert per account).
alert := captchaocr.NewAlertRequest(500, "https://hooks.example.org/captcha-credits")
alert.SetPayload(map[string]interface{}{"text": "Captcha credits low: {{credits}} left"})
alert.SetHeaders(map[string]string{"Authorization": "Bearer my-hook-secret"})
if _, _, err := client.AccountAPI.SetAlert(ctx).AlertRequest(*alert).Execute(); err != nil {
	panic(err)
}
test, _, _ := client.AccountAPI.TestAlert(ctx).Execute() // sends it now
fmt.Println(test.Delivered, test.GetStatus())
// client.AccountAPI.DeleteAlert(ctx).Execute()          // remove it

Report a wrong answer

If the site rejected a reliable answer, report its id within 48 hours to get the credit back.

// The site rejected res.GetText(): get the credit back (within 48 hours).
r, _, err := client.AccountAPI.Report(ctx).ReportRequest(*captchaocr.NewReportRequest(res.GetId())).Execute()
if err != nil {
	panic(err)
}
fmt.Println(r.Refunded, r.GetReason())

Errors

HTTP errors (401 bad key, 402 no credits, 404 unknown task, 422 invalid options, 429 too many concurrent requests, 503 retry later) raise the returned error; *GenericOpenAPIError has the body. An undecodable image is not an exception: it's a normal result with error: "invalid_image". See errors.

More