# Configure TrueProxies with Scrapy

> Configure TrueProxies endpoints and authentication in Scrapy proxy middleware, add Residential IPv4 country targeting, and verify an exit with a spider.

Source: https://docs.trueproxies.com/integrations/scrapy/

> **Use your own endpoints**
>
> The examples below use placeholders. Copy the connection host, ports, username, and proxy password from the **Username and password** tab of your service in the dashboard. See [How to connect](https://docs.trueproxies.com/proxy-instructions/how-to-connect/#connection-host-and-ports).

## Middleware setup

Add the proxy middleware to your Scrapy project’s `settings.py`:

**settings.py**

```python
DOWNLOADER_MIDDLEWARES = {
    "scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware": 110,
}

PROXY_USER = "your_username"
PROXY_PASS = "your_password"
PROXY_HOST = "YOUR_HOST"
PROXY_PORT = 8080
```

## Custom middleware

Create a middleware for automatic proxy injection:

**middlewares.py**

```python
class TrueProxiesMiddleware:
    def process_request(self, request, spider):
        request.meta["proxy"] = (
            f"http://{spider.settings['PROXY_USER']}:{spider.settings['PROXY_PASS']}"
            f"@{spider.settings['PROXY_HOST']}:{spider.settings['PROXY_PORT']}"
        )
```

Enable it in `settings.py`:

```python
DOWNLOADER_MIDDLEWARES = {
    "myproject.middlewares.TrueProxiesMiddleware": 100,
}
```

## Residential IPv4 country targeting

For Residential IPv4, target a specific country in the middleware:

```python
class TrueProxiesMiddleware:
    def process_request(self, request, spider):
        country = request.meta.get("country", "us")
        username = f"{spider.settings['PROXY_USER']}-country-{country}"
        request.meta["proxy"] = (
            f"http://{username}:{spider.settings['PROXY_PASS']}"
            f"@{spider.settings['PROXY_HOST']}:{spider.settings['PROXY_PORT']}"
        )
```

Use it in your spider:

```python
def start_requests(self):
    yield scrapy.Request(url, meta={"country": "de"})
```

## Example spider

```python
import scrapy

class IpSpider(scrapy.Spider):
    name = "ip_check"
    start_urls = ["https://httpbin.org/ip"]

    def parse(self, response):
        self.logger.info(f"IP: {response.json()['origin']}")
```

Run it:

```bash
scrapy crawl ip_check
```

## Related guides

- [Connection and username format](https://docs.trueproxies.com/proxy-instructions/how-to-connect/)
- [HTTP CONNECT and SOCKS5](https://docs.trueproxies.com/proxy-instructions/proxy-protocols/)
- [Trusted IPs](https://docs.trueproxies.com/proxy-instructions/trusted-ips/)
- [Troubleshooting checklist](https://docs.trueproxies.com/support/)
