v1.10.82-f67ee7d
Skip to main content
← Back to Hex Proxies

Ruby Faraday Proxy Integration

Use Hex Proxies with Faraday for Ruby HTTP requests.

Ruby Faraday Proxy Setup

Faraday is Ruby's most popular HTTP client middleware framework. Its pluggable adapter architecture and middleware stack make it ideal for proxy workflows — you can add retry logic, request logging, response parsing, and error handling as composable middleware layers. The proxy configuration applies globally to the connection, routing all requests through Hex Proxies automatically.

Complete Configuration with Middleware

require 'faraday'

proxy_url = "http://#{ENV['PROXY_USER']}:#{ENV['PROXY_PASS']}@gate.hexproxies.com:8080"

conn = Faraday.new(url: 'https://example.com') do |f| f.proxy proxy_url

# Retry middleware for proxy failures f.request :retry, max: 3, interval: 2, interval_randomness: 0.5, backoff_factor: 2, retry_statuses: [403, 429, 503]

# Request headers f.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)' f.headers['Accept-Language'] = 'en-US,en;q=0.9'

# Timeouts f.options.timeout = 30 f.options.open_timeout = 15

# Response parsing f.response :json, content_type: /\bjson$/ f.response :logger if ENV['DEBUG']

f.adapter Faraday.default_adapter end

response = conn.get('/api/data') puts "[#{response.status}] #{response.body}" ```

Geo-Targeted Connections

Create connections with country-specific proxy credentials:

def proxied_connection(country: nil)
  user = ENV['PROXY_USER']

Faraday.new do |f| f.proxy "http://#{user}:#{ENV['PROXY_PASS']}@gate.hexproxies.com:8080" f.request :retry, max: 3, retry_statuses: [403, 429, 503] f.options.timeout = 30 f.options.open_timeout = 15 end end

us_conn = proxied_connection(country: 'us') uk_conn = proxied_connection(country: 'gb')

us_response = us_conn.get('https://example.com/pricing') uk_response = uk_conn.get('https://example.com/pricing') ```

Custom Middleware for Proxy Metrics

class ProxyMetrics < Faraday::Middleware
  def call(env)
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    response = @app.call(env)
    elapsed = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).round
    puts "[#{response.status}] #{env.url} - #{elapsed}ms via proxy"
    response
  end

conn = Faraday.new do |f| f.proxy proxy_url f.use ProxyMetrics f.adapter Faraday.default_adapter end ```

Common Pitfalls

Faraday middleware order matters. The retry middleware must be added before the adapter — middleware runs in the order it is declared. If you add retry after the adapter, it will not catch connection failures. Another issue: Faraday connections are not thread-safe for concurrent modification. Create separate connections per thread or use connection pooling.

Choosing the Right Adapter

Faraday supports multiple HTTP adapters. The default Net::HTTP adapter works for basic proxy needs. For higher throughput, switch to the Typhoeus adapter which uses libcurl for connection pooling and parallel requests through the proxy.

Integration Steps

1

Create Faraday connection with proxy

Initialize Faraday.new and set the proxy URL including Hex Proxies credentials from environment variables.

2

Add retry middleware

Configure faraday-retry with retry_statuses including 403, 429, and 503 for automatic retry on proxy blocks with exponential backoff.

3

Configure timeouts

Set options.timeout to 30 seconds and options.open_timeout to 15 seconds to account for residential proxy connection latency.

4

Test with geo-targeting

Create connections with country codes in the proxy username to verify regional content differences through the proxy.

Operational Tips

Keep sessions stable for workflows that depend on consistent identity. For high-volume collection, rotate IPs and reduce concurrency if you see timeouts or 403 responses.

  • Prefer sticky sessions for multi-step flows (auth, checkout, forms).
  • Rotate per request for scale and broad coverage.
  • Use timeouts and retries to handle transient failures.

Frequently Asked Questions

Does Faraday support SOCKS5?

The default Net::HTTP adapter does not support SOCKS5. Switch to the Typhoeus adapter (faraday-typhoeus gem) which supports SOCKS5 through libcurl, or use the socksify gem with the Net::HTTP adapter.

Why is my Faraday retry middleware not working?

Middleware order matters in Faraday. Declare the retry middleware before the adapter in the connection block. Also ensure you have the faraday-retry gem installed and have specified retry_statuses to retry on HTTP error codes, not just network errors.

How do I rotate IPs with Faraday?

Append a unique session ID to the proxy username for each request. Since the proxy URL is set at connection creation, create a new connection per session group or modify the proxy configuration dynamically in a custom middleware.

Ready to Integrate?

Start using residential proxies with Ruby Faraday today.