Skip to content
Get Started
G2G2
4.7/5
Excellent
Trustpilot

How to use proxies in Ruby

With Net::HTTP, pass the proxy as arguments three to six: Net::HTTP.new("api.ipify.org", 443, "HOST", PORT, "USERNAME", "PASSWORD"), then set use_ssl = true. With Faraday, pass proxy: "http://USERNAME:PASSWORD@HOST:PORT" to Faraday.new.

  • Session Control
  • High Speed
  • Privacy First

What you need

Ruby's standard Net::HTTP and the popular Faraday gem both speak HTTP(S) proxies with Basic authentication, which is what CleanProxies uses. One detail catches people out: Net::HTTP picks up the http_proxy environment variable by default, even for HTTPS URLs, so an exported variable can route traffic you didn't mean to.

Your credential line

From the dashboard's proxy manager: host (or IP), port, username and password. Convert lists with the proxy list formatter.

Ruby ready

  • Ruby 3.x (Net::HTTP ships with Ruby).
  • For Faraday: gem install faraday, or add it to your Gemfile.

Which proxy fits Ruby?

Both products use the same setup in Ruby. What changes is how long an IP stays with you.

A Net::HTTP.start block keeps one keep-alive connection to one dedicated US ISP IP, so every request in the block, and the cookies you send with it, come from the same address. Open one connection (or one Faraday instance) per account.

starting from$0.75/IP

Dedicated static IPs registered to US and Canadian ISPs. The address stays the same for as long as you hold it, with unlimited bandwidth and HTTP(S) or SOCKS5 access.

Each new connection through a rotating line gets a fresh residential IP, but requests inside one Net::HTTP.start block share a connection and therefore an IP. Open a new connection per request to rotate, or use a sticky line for a multi-step flow.

starting from$0.63/GB

Residential IPs in 190+ countries with country, state and city targeting. Rotate on every request or hold a sticky session for up to 90 minutes; GB never expire.

credentials

Your CleanProxies credentials

Every product connects over HTTP(S); static ISP and ISP subnet proxies also run SOCKS5 and IP whitelisting. This is what the dashboard gives you to paste into Ruby.

Static ISP and ISP subnetHTTP(S)SOCKS5IP whitelist
IP:PORT:USER:PASS

One line per dedicated IP. With IP whitelisting on, the line is just IP:PORT and no password is sent.

Rotating residential and mobileHTTP(S)User:pass
HOST:PORT:USER:PASS

Generated in the proxy manager. The username carries the location and session you pick, so generate a new list to change them.

As a URL, for code and most toolshttp://USERNAME:PASSWORD@HOST:PORTsocks5://USERNAME:PASSWORD@HOST:PORT

Use http:// even for HTTPS websites; the TLS session still runs end to end. The socks5:// form is for static ISP and ISP subnet proxies.

How to set up CleanProxies in Ruby

Four steps, then check the exit IP with the free proxy checker.

  1. 1

    Split your proxy line

    Take host, port, username and password from a line in your CleanProxies dashboard.

  2. 2

    Pass them to the client

    Net::HTTP: positional arguments after host and port. Faraday: the proxy: option as a URL or a hash.

  3. 3

    Turn on TLS for HTTPS

    Set use_ssl: true in Net::HTTP. The proxy itself stays http://; the HTTPS session is tunnelled through it.

  4. 4

    Check the exit IP

    Run ruby proxy_check.rb against https://api.ipify.org and print the body.

copy & paste

Ruby proxy examples

Replace the highlighted placeholders with the values from your dashboard. Pick an example to see its code.

Ruby + CleanProxies

Net::HTTP with an authenticated proxy

proxy_check.rbRuby
require "net/http"

uri = URI("https://api.ipify.org")

Net::HTTP.start(uri.host, uri.port, "HOST", PORT, "USERNAME", "PASSWORD",
                use_ssl: true, open_timeout: 15, read_timeout: 30) do |http|
  response = http.get(uri.request_uri)
  puts response.body # the exit IP
end
  • The full signature is Net::HTTP.new(address, port, p_addr, p_port, p_user, p_pass, p_no_proxy, p_use_ssl). Pass nil as p_addr to ignore proxy variables for a connection.

SOCKS5 in Ruby

Net::HTTP and Faraday's default adapter have no SOCKS client, so use the HTTP(S) form of your credentials; it works on every CleanProxies product. SOCKS5 itself exists only on static ISP and ISP subnet proxies, and in Ruby it needs a third-party gem, so HTTP(S) is the simpler choice.

fix it fast

Troubleshooting Ruby proxy errors

Verify your exit IP

The first example prints the exit IP. To see which proxy Net::HTTP picked up, check http.proxy?, http.proxy_address and http.proxy_from_env? before you send the request.

Open What is my IP? through the proxy to see the address and headers a website receives, or paste up to ten lines into the free proxy checker to test connectivity, latency and exit IP.

Still stuck? Our 24/7 support team answers fast: contact us with the error and the line you used (leave the password out).

Ruby error fixes
  • 407 "Proxy Authentication Required" (Net::HTTPClientException)Why: Wrong username or password, or a password with special characters inside a proxy URL.Pass the credentials as separate arguments (Net::HTTP) or in the proxy hash (Faraday).
  • Net::OpenTimeoutWhy: Wrong port, a firewall blocking outbound traffic, or an inactive plan.Test the same line with curl -x, then check the port and firewall.
  • Traffic goes through a proxy you didn't setWhy: Net::HTTP uses http_proxy from the environment by default, also for HTTPS.Pass nil as the third argument, or unset the variable.
  • OpenSSL::SSL::SSLError or an EOFError on HTTPSWhy: use_ssl left off for port 443, or an https:// proxy URL pointed at an HTTP proxy port.Set use_ssl: true and keep the proxy URL http://.

Ruby proxy guides

Setup guides and best practices from the CleanProxies blog.

Get the latest insightsAll Blogs

More integrations

Setup guides for tools people run next to Ruby.

Proxies for sites people reach with Ruby
All integrations

Ruby proxy FAQs

Straight answers about running CleanProxies in Ruby.

Net::HTTP.new(address, port, p_addr, p_port, p_user, p_pass). Arguments three to six are the proxy host, proxy port, username and password; Net::HTTP.start takes the same positional arguments followed by options such as use_ssl.

Got more questions?All FAQs