> [!WARNING]
> [Zyte API](../../zyte-api/get-started.md#zyte-api) is replacing Smart Proxy Manager. It is
> no longer possible to sign up to Smart Proxy Manager. If you are an
> existing Smart Proxy Manager user, see spm-migrate.

# Using Smart Proxy Manager with Ruby

> [!WARNING]
> For the code below to work you must first [install the Zyte
> CA certificate](../../misc/ca.md#ca).

> [!NOTE]
> All the code in this documentation has been tested with Ruby 2.6.3p62, Curb 0.9.11,
> Typhoeus 1.4.0, and Mechanize 2.8.2.

## Using Curb

Making use of [curb](https://github.com/taf2/curb), a Ruby binding for libcurl:

```ruby
require 'curb'

url = "https://toscrape.com"
proxy = "proxy.zyte.com:8011"
proxy_auth = "<CRAWLERA_APIKEY>:"

c = Curl::Easy.new(url) do |curl|
  curl.proxypwd = proxy_auth
  curl.proxy_url = proxy
  curl.verbose = true
end

c.perform
puts c.body_str
```

## Using Typhoeus

Making use of [typhoeus](https://github.com/typhoeus/typhoeus),
another Ruby binding for libcurl:

```ruby
require 'typhoeus'

url = "https://toscrape.com"
proxy_host = "proxy.zyte.com:8011"
proxy_auth = "<CRAWLERA_APIKEY>:"

request = Typhoeus::Request.new(
  url,
  method: :get,
  proxy: proxy_host,
  proxyuserpwd: proxy_auth,
  headers: {"X-Crawlera-Timeout" => "60000"}
)

request.run
print "Response Code: "
puts request.response.code
print "Response Time: "
puts request.response.total_time
print "Response Headers: "
puts request.response.headers
print "Response Body: "
puts request.response.body
```

## Using Mechanize

Making use of [mechanize](https://github.com/sparklemotion/mechanize), a
Ruby library for automated web interaction:

```ruby
require 'rubygems'
require 'mechanize'

url = "https://toscrape.com"
proxy_host = "proxy.zyte.com"
proxy_api_key = "<CRAWLERA_APIKEY>"

agent = Mechanize.new
agent.set_proxy proxy_host, 8011, proxy_api_key, ''

res = agent.get(url)
puts res.body
```

To allow Mechanize to use the [Zyte CA certificate](../../misc/ca.md#ca), point the
`SSL_CERT_FILE` environment variable to its path:

```bash
export SSL_CERT_FILE=/path/to/zyte-ca.crt
```
