Scrapy integration

Proxies for Scrapy

Scrapy sends a request through a proxy when its meta carries one: put the username and password in the proxy URL and the built-in HttpProxyMiddleware hands them to the gateway. Below: a rotating spider and one that keeps a sticky residential IP.

Residential proxies
Built-in HttpProxyMiddleware Code tested on our gateway Traffic never expires

What teams run Scrapy through proxies for

Public data by country

Crawl public pages, such as prices, listings and search results, as a visitor in the country you pick sees them.

Price and catalog monitoring

Track public prices, stock and listings in each market on a schedule, from residential IPs in that market.

Checking your own site

Crawl your own site the way visitors in other countries reach it: geo-redirects, localized content and CDN rules.

Connection details

Hostgate.proxyhat.com
SOCKS5 port1080
HTTP port8080
AuthenticationUsername and password from the Proxies page of your dashboard
Username for one workerUSERNAME-country-US-sid-a3f8c20d1e-ttl-30m

Everything after your username is a flag: -country-US picks the exit country, -type-mobile the mobile carrier pool, -sid-… keeps one IP and -ttl-30m sets for how long (up to 24 hours). Leave out -type-mobile for residential IPs.

Need another proxy format? Proxy format converter →

Scrapy proxy code

Replace USERNAME and PASSWORD with the proxy credentials from the Proxies page of your dashboard. pip install -U scrapy (2.13 or newer: the samples use async def start), save a sample as a .py file and run scrapy runspider ip_spider.py -O ip.json.

import scrapy

# Scrapy takes the login from the proxy URL and sends it as Proxy-Authorization.
# No -sid-: every new connection can exit from a new IP.
PROXY = "http://USERNAME-country-US:PASSWORD@gate.proxyhat.com:8080"


class IpSpider(scrapy.Spider):
    name = "ip"

    async def start(self):
        yield scrapy.Request("https://httpbin.org/ip", meta={"proxy": PROXY})

    def parse(self, response):
        yield {"ip": response.json()["origin"]}

HttpProxyMiddleware is on by default. It takes the username and password out of the proxy URL and sends them as a Proxy-Authorization header; for HTTPS pages, on the CONNECT request that opens the tunnel. Without -sid-, every new connection can exit from a new IP.

Tested on November 29, 2026 against gate.proxyhat.com with Scrapy 2.20.0 on Python 3.13: the rotating spider printed a US IP, the sticky spider the same US IP twice.

Checked against: Scrapy: HttpProxyMiddleware, Scrapy: Spider.start, Scrapy: download handlers · checked November 29, 2026

Managing proxies from code? The ProxyHat SDK for Python, Node.js and Go creates sub-users with their own traffic limits, lists locations and reads usage; the proxy connection itself stays as above. SDK docs →

Rotating, sticky or no rotation

The session type is part of the username. Your code stays the same; only the login string changes.

Rotating

No session ID: each new connection can exit from a different IP. Good for many independent page loads.

USERNAME-country-US

Sticky

Add -sid- with 10 random hex characters and a -ttl-: the IP holds for that long (10m, 30m, 1h, up to 24h). Use it for multi-page flows that should stay on one address.

USERNAME-country-US-sid-a3f8c20d1e-ttl-30m

No rotation

Add -sid- with -norotate-true and no -ttl-: the IP doesn’t rotate on a timer and stays until you switch to a new sid.

USERNAME-country-US-sid-a3f8c20d1e-norotate-true

One sid holds one exit IP and handles about 4–5 connections at a time, so give every worker or browser its own sid. Sticky sessions last up to 24 hours. Add -type-mobile for 4G/5G carrier IPs; leave it out for residential.

Proxy errors and what they mean

407 Proxy Authentication RequiredThe gateway didn’t accept the login: the username or password is wrong, or the username format is (a typo in a flag such as -country-US). Copy both from the Proxies page of your dashboard and make sure the auth step runs before the first page load.
402 Payment RequiredThe traffic balance is used up, or the sub-user has reached its traffic limit. Top up or raise the limit in the dashboard; traffic you buy never expires.
ERR_TUNNEL_CONNECTION_FAILEDChrome’s name for a proxy that refused the connection, and it can hide a 402 or another gateway status. Send the same username and password through curl -x http://gate.proxyhat.com:8080 to see the real code.
ERR_PROXY_CONNECTION_FAILEDChrome couldn’t reach the gateway at all. Check the host and port (gate.proxyhat.com, 8080 for HTTP) and that your network allows outgoing connections to it.

All gateway errors and username flags are in the documentation: ProxyHat docs →

Price

Mobile and residential traffic share one price and one balance: from $4.00/GB for a 1 GB pack down to $1.80/GB for 1,000 GB. Traffic never expires, and all your workers draw from the same balance. See pricing →

Scrapy proxy setup: FAQ

Do I need a custom proxy middleware?

Not for a single proxy setting: meta['proxy'] on each request is enough. When every request in a project should get a proxy, or each needs its own sid, set meta['proxy'] in a small downloader middleware that runs before HttpProxyMiddleware. Our Scrapy middleware guide, linked below, walks through one.

Can Scrapy use SOCKS5?

Not with its default download handler: use HTTP on port 8080, which carries HTTPS sites too. Scrapy’s httpx-based download handler (Scrapy 2.15+) accepts socks5:// proxy URLs, but Scrapy marks it experimental and not yet recommended for production.

Why do several requests in a row show the same IP?

A rotating username changes the IP per connection, not per request, and Scrapy keeps connections to a site open and reuses them. When you want a fixed IP for a flow and a new one for the next, use sticky sessions with a fresh sid per flow.

How long does a sticky IP last?

As long as the -ttl- flag says, up to 24 hours: -ttl-10m, -ttl-30m, -ttl-1h, -ttl-24h. For an IP that doesn't rotate on a timer, use -sid- with -norotate-true and no -ttl-.

How do I pick a country, state or city?

Add -country- with a two-letter code (residential IPs in 195+ countries). For a state or city, add -region- and then -city- with their location codes, for example -country-US-region-new_york-city-new_york_city: the region must come before the city. -type-mobile switches to 4G/5G carrier IPs (150+ countries) at the same price.

What do 407 and 402 look like in Scrapy?

For HTTPS pages the request fails, after Scrapy’s retries, with a TunnelError that names the status the gateway sent. 407 means it didn’t accept the login: check the username, its flags and the password. 402 means the traffic balance or the sub-user’s limit is used up. The errors table above has the details.

Run Scrapy through residential IPs

Create an account, buy a small pack and run the samples above before you scale out.

Pay as you go · Traffic never expires · Crypto checkout