v1.10.90-0e025b8
Skip to main content
Back to Hex Proxies

Ruby Faraday Proxy Setup

Last updated: April 2026

By Hex Proxies Engineering Team

Integrate Hex Proxies into Ruby applications using Faraday. Covers proxy configuration, middleware stacks, retry handling, and session management.

intermediate15 minuteslanguage-integration

Prerequisites

  • Ruby 3.0 or later
  • Faraday gem installed
  • Hex Proxies account with credentials

Steps

1

Install Faraday

Add faraday and faraday-retry to your Gemfile and run bundle install.

2

Create a connection

Use Faraday.new with the proxy option pointing to gate.hexproxies.com:8080.

3

Test the proxy

Send a GET to httpbin.org/ip and confirm the response IP differs from your own.

4

Add retry middleware

Configure faraday-retry to handle 429 and 503 with exponential backoff.

5

Enable sticky sessions

Append a session ID to the proxy username for consistent IP assignment.

6

Add logging

Use Faraday response logger or a custom middleware to monitor proxy performance.

Ruby Faraday Proxy Integration

Faraday is a flexible HTTP client library for Ruby with a middleware architecture. It supports proxy configuration through connection options, and its middleware stack makes it easy to add retry logic and logging around proxied requests.

Basic Proxy Setup

require 'faraday'

conn = Faraday.new( url: 'https://httpbin.org', proxy: 'http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080', request: { timeout: 30, open_timeout: 10 } ) do |f| f.response :logger f.adapter Faraday.default_adapter end

response = conn.get('/ip') puts "Status: #{response.status}" puts "Body: #{response.body}" ```

Sticky Sessions

session_user = "YOUR_USERNAME-session-ruby001"
conn = Faraday.new(
  url: 'https://example.com',
  proxy: "http://#{session_user}:YOUR_PASSWORD@gate.hexproxies.com:8080"
)

Both requests exit from the same IP login_resp = conn.post('/login', { user: 'me', pass: 'secret' }.to_json) dashboard_resp = conn.get('/dashboard') ```

Retry Middleware

require 'faraday/retry'

conn = Faraday.new( url: 'https://httpbin.org', proxy: 'http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080' ) do |f| f.request :retry, { max: 3, interval: 1, interval_randomness: 0.5, backoff_factor: 2, retry_statuses: [429, 503], exceptions: [Faraday::ConnectionFailed, Faraday::TimeoutError] } f.adapter Faraday.default_adapter end ```

Concurrent Requests with Threads

urls = %w[
  https://httpbin.org/ip
  https://httpbin.org/headers
  https://httpbin.org/get
]

threads = urls.map do |url| Thread.new do resp = conn.get(URI.parse(url).path) puts "#{url}: #{resp.status}" end end

threads.each(&:join) ```

Custom Middleware for Proxy Logging

class ProxyLogger < Faraday::Middleware
  def call(env)
    start = Time.now
    response = @app.call(env)
    elapsed = ((Time.now - start) * 1000).round(2)
    puts "[Proxy] #{env.url} -> #{response.status} (#{elapsed}ms)"
    response
  end
end

conn = Faraday.new(proxy: 'http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080') do |f| f.use ProxyLogger f.request :retry, max: 3, retry_statuses: [429, 503] f.adapter Faraday.default_adapter end ```

Environment-Based Configuration

proxy_url = ENV.fetch('HEX_PROXY_URL')
conn = Faraday.new(url: 'https://example.com', proxy: proxy_url)

Faraday makes it easy to swap backends — the same proxy configuration works with Net::HTTP, Typhoeus, or Patron adapters.

Tips

  • Faraday retry middleware integrates natively — use it instead of manual retry loops.
  • Use the proxy option on the connection, not on individual requests.
  • Custom middleware is the best way to monitor proxy latency and success rates in Ruby.
  • The same proxy config works across all Faraday adapters (Net::HTTP, Typhoeus, Patron).

Ready to Get Started?

Put this guide into practice with Hex Proxies.