Golang captcha solver: a Go API client with net/http
A Golang captcha solver is a small Go program that posts a challenge to a solver API and decodes the answer, using only the standard library: net/http, encoding/json and encoding/base64. With ilovecaptcha it makes two HTTPS calls, createTask then getTaskResult. This page explains the request, the typed reply, the polling loop and the errors, then gives the complete file.
Preview: the API described here is in public beta. Endpoint names, fields and codes can still change until the end of the beta, and no API key is distributed yet.
How do you call a captcha solver API from Go?
With net/ and encoding/. The function post adds the key to a map[string]any under clientKey, serialises it with json.Marshal, sends it with client.Post as application/ and decodes the reply into the struct apiResponse with json.NewDecoder. The client is created once, with a Timeout of 30 seconds, and reused for every call.
| Item | Value |
|---|---|
| Runs on | Go 1.18 or newer |
| Modules to add | None: standard library only |
| File | solve.go |
| Command | go run solve.go |
| HTTP client | net/ |
How do you send the captcha image and wait for the answer in Go?
os.ReadFile loads the image and base64.StdEncoding.EncodeToString produces the string for the task. solve posts it to createTask, then loops: time.Sleep, a call to getTaskResult, and a test on Status until it reads ready.
pollIntervalis 1500 milliseconds, atime.Durationconstant.timeoutis 120 seconds: past that,solvereturns an error built withfmt.Errorf.TaskIDis ajson.Number: the identifier is sent back exactly as received, without passing through a float.Solutionstays ajson.RawMessage, decoded later into the type your captcha needs.
How do you handle the API key and errors in Go?
Read the key with os.Getenv from a variable such as ILOVECAPTCHA_KEY rather than from the constant apiKey, so that neither the binary nor the repository contains it. Errors follow the Go convention: post and solve return an error as their last value. A non-zero ErrorID becomes an error carrying ErrorCode and ErrorDescription; a network or decoding failure is returned unchanged. The example calls panic in main: replace it with your own handling.
What does a complete Go captcha solver client look like?
The file below is the whole client, in 98 lines. It declares the struct apiResponse and the functions post, solve and getBalance, then reads captcha.png, submits it as an aws-waf task and prints the solution. Replace YOUR_API_KEY and YOUR_API_URL with your own values and adapt the task map to your captcha before you run it.
solve.go · Complete Go client: one createTask call, then getTaskResult until the task is ready (preview format).package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
const apiKey = "YOUR_API_KEY"
const baseURL = "YOUR_API_URL"
const pollInterval = 1500 * time.Millisecond
const timeout = 120 * time.Second
type apiResponse struct {
ErrorID int `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
TaskID json.Number `json:"taskId"`
Status string `json:"status"`
Solution json.RawMessage `json:"solution"`
Balance float64 `json:"balance"`
}
var client = &http.Client{Timeout: 30 * time.Second}
func post(route string, body map[string]any) (*apiResponse, error) {
body["clientKey"] = apiKey
payload, err := json.Marshal(body)
if err != nil {
return nil, err
}
response, err := client.Post(baseURL+route, "application/json", bytes.NewReader(payload))
if err != nil {
return nil, err
}
defer response.Body.Close()
var result apiResponse
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
return nil, err
}
if result.ErrorID != 0 {
return nil, fmt.Errorf("%s: %s", result.ErrorCode, result.ErrorDescription)
}
return &result, nil
}
func solve(task map[string]any) (json.RawMessage, error) {
result, err := post("/createTask", map[string]any{"task": task})
if err != nil {
return nil, err
}
if result.Status == "ready" {
return result.Solution, nil
}
taskID := result.TaskID
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
time.Sleep(pollInterval)
result, err = post("/getTaskResult", map[string]any{"taskId": taskID})
if err != nil {
return nil, err
}
if result.Status == "ready" {
return result.Solution, nil
}
}
return nil, fmt.Errorf("task %s not ready after %s", taskID, timeout)
}
func getBalance() (float64, error) {
result, err := post("/getBalance", map[string]any{})
if err != nil {
return 0, err
}
return result.Balance, nil
}
func main() {
data, err := os.ReadFile("captcha.png")
if err != nil {
panic(err)
}
image := base64.StdEncoding.EncodeToString(data)
solution, err := solve(map[string]any{"type": "aws-waf", "image": image})
if err != nil {
panic(err)
}
fmt.Println(string(solution))
balance, err := getBalance()
if err != nil {
panic(err)
}
fmt.Println(balance)
}
Go captcha solver: frequently asked questions
Do I need a Go module or an SDK to solve captchas in Go?
No. The standard library covers the whole exchange: net/http sends the POST request, encoding/json builds the body and decodes the reply, encoding/base64 prepares the image, time spaces the polls. The file on this page is the complete client. Move its functions into a package of your project: go.mod gains no require line, and there is no version to follow.
Why does the Go example decode taskId as json.Number?
Because encoding/json turns a JSON number into a float64 when the target is an untyped value, and a long identifier can lose its last digits on the way. json.Number keeps the digits as text, and json.Marshal writes them back unchanged in the body of getTaskResult. The example therefore expects a numeric identifier and never does arithmetic on it.
What does solve return in Go, and how do you read it?
A json.RawMessage, the solution object as raw bytes, and an error. Decode it with json.Unmarshal into a struct that matches your captcha: a Token string for a token widget, a Points slice of integer pairs for an image grid, an Offset number for a slider, a Text string for characters to type. Declaring one small struct per task type lets the compiler check how you use the answer.
Sources
The facts and figures quoted on this page come from these sources.
- The Go Authors, Package http, Go standard library documentation (Client, Post, Timeout).
- The Go Authors, Package json, Go standard library documentation (Marshal, Decoder, Number, RawMessage).