Skip to content
All posts

How to rotate IPs and pick a country with tunneling

Tunneling and IPs3 min read
How to rotate IPs and pick a country with tunneling: the post's first code sample

Sometimes you do not want an extraction API. You already have a scraper, a test suite or a script, and you just need its requests to leave from a different IP each time, or from a specific country. The scrape.land tunnel does that for any HTTP client, with your API key as the only credential and no SDK to install.

The one-line version

Point your client's tunnel setting at gateway.scrape.land:8080 and put your key in the username. With curl that is the -x flag:

curl
curl -x "http://YOUR_KEY-country-de:@gateway.scrape.land:8080" https://api.ipify.org?format=json
JSON
{"ip":"203.0.113.24"}

Run it again and you get a different address:

JSON
{"ip":"203.0.113.87"}

Every request gets a new exit IP. The -country-de suffix on the key asks for an exit in Germany; use any two-letter ISO country code, or leave the suffix off to exit anywhere. The password is empty, which is why the key is followed by a bare :.

From Python

The requests library takes the same URL. Nothing else in your code changes: headers, cookies, sessions and retries all work as before.

Python
import requests

KEY = "YOUR_KEY"
tunnel = f"http://{KEY}-country-de:@gateway.scrape.land:8080"
proxies = {"http": tunnel, "https": tunnel}

for _ in range(3):
    r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=30)
    print(r.json()["ip"])

The output is three different addresses, all in Germany. Replace the ipify URL with the page you actually want, and the rest of your scraper stays the same.

Options you can add to the key

Options are -name-value pairs appended to the key, in any order:

For example, a German exit that stays fixed for a whole login flow:

curl
curl -x "http://YOUR_KEY-country-de-session-checkout7:@gateway.scrape.land:8080" https://example.com/account

HTTPS targets

HTTPS sites work as normal. Your client opens an encrypted connection to the target through the tunnel, so the page content stays between you and the site. Keep the http:// scheme in the tunnel URL even when the target is HTTPS: that scheme describes how your client talks to the gateway, not the site.

If your network blocks port 8080 (common on corporate, university and hotel networks), the gateway also answers on port 443 with identical behaviour and billing: use gateway.scrape.land:443.

Other tools

Anything that supports an HTTP tunnel setting with a username works the same way: Node's https-proxy-agent, Go's http.Transport, Scrapy, Playwright and Puppeteer (as a launch option), or an environment variable for command-line tools:

shell
export HTTPS_PROXY="http://YOUR_KEY-country-us:@gateway.scrape.land:8080"
curl https://api.ipify.org?format=json

Browsers cannot set a different tunnel per request from inside a web page, so call the tunnel from a backend or a script, not from front-end JavaScript.

Tunnel or extraction API?

Use the tunnel when you already have code that fetches and parses pages and you only need rotating exits and country control. Use /v1/fetch or /v1/extract when you would rather send a URL and get back HTML, Markdown or the exact fields you want as JSON, with rendering available for JavaScript pages. Both use the same key and the same request allowance.

What it costs

Each delivered request is 1 request unit, and you only pay for responses that land: blocks and retries are free. The Free plan includes 1,000 requests a month with no card; paid plans go down to $0.139 per 1,000 requests on Business. See pricing.

Next steps

The full list of key options and the header equivalents is in the docs, along with a tunnel URL builder. Create a free account to get a key and run the curl command above.

Start free with 1,000 requests Read the docs