← Docs / Proxies
Datacenter proxies with Go
Go sends traffic through a proxy when you set Transport.Proxy. This page uses the datacenter tier: a US datacenter IP that changes for every new connection.
What you need
Your proxy API key, from the Proxies page in the portal, and the standard library. No packages to install.
export PB_PROXY_KEY="YOUR_KEY"
Send a request through the proxy
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
)
func main() {
proxy, _ := url.Parse("http://" + os.Getenv("PB_PROXY_KEY") + ":[email protected]:3128")
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
res, err := client.Get("https://api.ipify.org")
if err != nil {
panic(err)
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body)) // a US datacenter address
}
Go reads the username and password from the URL and sends them to the proxy for you.
One IP, or a new one each time
Go reuses connections. Every request through this client leaves from one address, as long as the connection stays open. To get a new IP for every request, turn keep-alive off:
client := &http.Client{Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
DisableKeepAlives: true,
}}
Keep connections open when you want a steady address or a smaller bill. A new HTTPS connection costs about 4 KB of handshake before your first byte. See how usage is counted.
Always read and close the response body. Go only reuses a connection after the body is closed.
Errors
| What you see | What it means |
|---|---|
407 Proxy Authentication Required |
The API key is wrong or missing. |
400 with Unknown tier "datacentre" |
The tier word is a close misspelling of a real tier. Fix the spelling. A password that is not close to any tier, such as x, uses your account's default tier. |
400 Invalid proxy password settings |
The password asked for something this tier does not do: a country other than US, a session or a TTL. |
A site returns 403 |
The site is refusing datacenter addresses. Try a residential tier such as standard. |
A failed tunnel appears as an error from client.Get, for example Proxy Authentication Required.
Related
Related
Still stuck? Open a ticket — tell us what was missing and the article gets fixed.