More languages
How to get past the Cloudflare challenge page in Go
The Cloudflare challenge page stops a Go client before it reaches any content. This guide obtains a cf_clearance cookie through your own proxy at $0.84 per 1,000, stores it in a cookie jar, and sends it with the matching User-Agent.
What the challenge page needs
When Cloudflare decides a visitor must be checked, it returns an interstitial page (often a 403 or 503 with a "Just a moment" title) instead of the site. A browser that passes the check receives a cf_clearance cookie, and with it the site loads normally for a while. That cookie is bound to the IP address and the User-Agent that earned it. So three things must stay together in your Go program: the proxy, the User-Agent string and the cookie. The task type AntiCloudflareTask takes your proxy as a required field for exactly that reason.
Recognize the challenge
Look at the response before treating it as content. Cloudflare marks challenge responses with the header cf-mitigated: challenge on many configurations, and the HTML contains recognizable markers.
package cfpass
import (
"bytes"
"net/http"
"strings"
)
func looksChallenged(rs *http.Response, body []byte) bool {
if strings.EqualFold(rs.Header.Get("cf-mitigated"), "challenge") {
return true
}
if rs.StatusCode != http.StatusForbidden && rs.StatusCode != http.StatusServiceUnavailable {
return false
}
return bytes.Contains(body, []byte("challenge-platform")) ||
bytes.Contains(body, []byte("Just a moment"))
}
A plain 403 without these markers is usually a firewall rule or an IP block, and no cookie will help. The guide on identifying Cloudflare protection helps tell them apart.
Request cf_clearance through your proxy
The task needs the page URL and the proxy in http://user:pass@host:port form. Passing a User-Agent is optional; if you do, the solution is produced under it. The reply's solution contains the cookie (in cookies.cf_clearance, also given as token) and the userAgent to use with it.
type clearance struct {
Value string
Agent string
}
type cfReply struct {
Fail int `json:"errorId"`
Why string `json:"errorCode"`
Ticket string `json:"taskId"`
Progress string `json:"status"`
Pass struct {
Jar map[string]string `json:"cookies"`
Value string `json:"token"`
Agent string `json:"userAgent"`
} `json:"solution"`
}
func getClearance(ctx context.Context, apiKey, pageURL, proxy, ua string) (clearance, error) {
task := map[string]any{"type": "AntiCloudflareTask", "websiteURL": pageURL, "proxy": proxy}
if ua != "" {
task["userAgent"] = ua
}
var made cfReply
if err := postJSON(ctx, "/createTask", map[string]any{"clientKey": apiKey, "task": task}, &made); err != nil {
return clearance{}, err
}
if made.Fail != 0 {
return clearance{}, errors.New("createTask: " + made.Why)
}
pace := time.NewTicker(3 * time.Second)
defer pace.Stop()
for round := 1; round <= 40; round++ {
select {
case <-ctx.Done():
return clearance{}, ctx.Err()
case <-pace.C:
}
var st cfReply
if err := postJSON(ctx, "/getTaskResult", map[string]any{"clientKey": apiKey, "taskId": made.Ticket}, &st); err != nil {
return clearance{}, err
}
if st.Fail != 0 {
return clearance{}, errors.New("getTaskResult: " + st.Why)
}
if st.Progress == "ready" {
v := st.Pass.Jar["cf_clearance"]
if v == "" {
v = st.Pass.Value
}
return clearance{Value: v, Agent: st.Pass.Agent}, nil
}
if st.Progress == "failed" {
return clearance{}, errors.New("challenge task failed, not charged")
}
}
return clearance{}, errors.New("cf_clearance not ready in time")
}
The helper postJSON marshals the body, posts it to https://gatesolver.com plus the path, and decodes the reply into the given struct, leaving the errorId check to the caller as shown. It is the same few lines as in any Go JSON client; write it once in your package.
A client that keeps the three together
Build one http.Client per proxy, with a transport that uses that proxy and a jar that will hold the cookie. Then wrap requests so the User-Agent from the solution is always set.
type Session struct {
web *http.Client
agent string
}
func NewSession(ctx context.Context, apiKey, siteURL, proxy string) (*Session, error) {
pu, err := url.Parse(proxy)
if err != nil {
return nil, err
}
jar, _ := cookiejar.New(nil)
web := &http.Client{Jar: jar, Timeout: 30 * time.Second,
Transport: &http.Transport{Proxy: http.ProxyURL(pu), ForceAttemptHTTP2: true}}
cl, err := getClearance(ctx, apiKey, siteURL, proxy, "")
if err != nil {
return nil, err
}
su, _ := url.Parse(siteURL)
jar.SetCookies(su, []*http.Cookie{{Name: "cf_clearance", Value: cl.Value, Path: "/", Domain: su.Hostname()}})
return &Session{web: web, agent: cl.Agent}, nil
}
func (s *Session) Get(ctx context.Context, target string) (*http.Response, error) {
rq, _ := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
rq.Header.Set("User-Agent", s.agent)
rq.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
rq.Header.Set("Accept-Language", "en-US,en;q=0.9")
return s.web.Do(rq)
}
Imports for this file are context, errors, net/http, net/http/cookiejar, net/url and time. Setting Domain on the cookie makes the jar send it to the host and its subdomains, which is how Cloudflare usually scopes it.
The TLS caveat
Be aware of one limit before you rely on a pure Go client. Go's crypto/tls produces a handshake that differs from Chrome's, and some Cloudflare configurations compare the TLS fingerprint with the browser claimed in the User-Agent. On such sites, a correct cf_clearance sent from net/http can still lead to a new challenge. Many sites do not check this strictly, so test on your target. Where it is checked, the practical options are a client library that mimics a browser handshake, or driving a real browser from Go with chromedp and setting the cookie there. Both keep the API part unchanged.
Detecting expiry and refreshing
A clearance lasts for a period the site chooses. Run every response through looksChallenged; when it reports a challenge again, request a new clearance for the same proxy, replace the cookie in the jar and repeat the request once. Never move a clearance to a different proxy, and never change the User-Agent mid session.
Several proxies at once
Run one Session per proxy. Sessions are independent and safe to use from several goroutines each, because http.Client and the jar are concurrency safe. The API places no limit on concurrent tasks; mind the 600 requests per minute per key when many sessions refresh at the same time.
Each clearance costs $0.84 per 1,000 and covers many page loads, so the cost per page is low. Failed tasks are free. The Cloudflare Challenge page lists the fields, and the article on reusing cf_clearance goes deeper into lifetimes.
Choosing a proxy for the task
The proxy you pass in the task is the one the solve runs through, and the one your Go client must keep using afterwards. Use an address that your client can hold for the whole session: a sticky session on a rotating pool, or a static address. If the pool rotates the exit IP between requests, the clearance stops matching and the challenge returns. Write the proxy in the exact format http://user:pass@host:port; the FAQ on the proxy format for tasks lists the accepted forms.
Keeping requests calm
A clearance tells Cloudflare the visitor passed a check, but rate rules still apply. A Go program can send requests far faster than any browser, and a burst can trigger a new challenge or a rate limit block within seconds. Add a small delay between requests per session, or a token bucket with golang.org/x/time/rate, and spread work across sessions rather than hammering one.
Logging per session
Log the proxy host (without credentials), the time each clearance was obtained, how many requests it served and why it ended. Over a few days this shows the typical clearance lifetime for each site, which tells you how often to expect a refresh and what each page really costs.
Common questions
Why is a proxy required for this task?
The cf_clearance cookie only works from the IP that earned it, so the solve must run through the proxy your Go client uses.
Which User-Agent should my requests send?
The userAgent returned in the solution. Changing it invalidates the clearance on most sites.
For how long can one clearance be reused?
The site sets the lifetime. Reuse it until a challenge appears again, then request a new one for the same proxy.
Why might the site challenge again right after?
Some sites compare the TLS fingerprint of Go net/http with the claimed browser. Test on your target, or use a browser for those sites.
Is the challenge page the same as Turnstile?
No. Turnstile is a widget inside a form and returns a token; the challenge page blocks the whole site and is cleared with a cookie.
Keep reading
- Cloudflare Challenge solver$0.84 per 1,000 tasks, answers under 10 seconds.
- cf_clearance cookieThe cookie Cloudflare sets after a passed challenge, why it only works from one IP and user agent, and its lifespan.
- Reusing cf_clearance cookies safelyDesign sessions around cf_clearance: bind each cookie to one IP and user agent, track its age and refresh only when needed.
- Tell Turnstile, managed challenges and blocks apartStatus codes, headers, page markers and cookies that separate a Turnstile widget, a challenge page and a hard Cloudflare block.