Rotate IPs and manage sessions
Rotate an exit IP on demand, handle the cooldown, keep one IP with sticky sessions, set auto-rotation and trigger rotation from a scheduler.
POST /api/v1/proxies/{id}/rotateGET /api/v1/rotatePATCH /proxies/{id}/settingsProxy login: -session-ID, -country-xxBefore you start
- An API key and at least one proxy. See the quickstart.
1. Rotate on demand
One call assigns a new exit IP to a proxy. The response is the updated proxy with the new currentIp and cooldownEndsAt.
curl -X POST https://api.premip.com/api/v1/proxies/PROXY_ID/rotate \
-H "X-Api-Key: $PREMIP_API_KEY"2. Handle the cooldown
Each proxy can rotate once per cooldown, 60 seconds by default. Inside the window you get 429 PROXY_ROTATE_COOLDOWN with retryAfterSec and a Retry-After header. Sleep that long, then call again.
def rotate(proxy_id):
while True:
try:
return call("POST", f"/api/v1/proxies/{proxy_id}/rotate")["currentIp"]
except ApiError as err:
if err.status != 429:
raise
time.sleep(err.retry_after or 60)Example output
HTTP 429 {"code":"PROXY_ROTATE_COOLDOWN","message":"Wait 42 seconds before rotating again.","retryAfterSec":42}3. Keep one IP with a sticky session
Add -session-ID to the username. Every request with the same ID keeps one exit IP until the TTL (300, 1800 or 3600 seconds) ends or you rotate. Use a different ID for every independent job. The same works over SOCKS5 on port 1080.
curl -x http://USERNAME-session-job42:[email protected]:8080 https://api.ipify.org
curl -x http://USERNAME-session-job42:[email protected]:8080 https://api.ipify.org # same IP
curl -x http://USERNAME-session-job43:[email protected]:8080 https://api.ipify.org # new session4. Set auto-rotation and session TTL
Let the gateway rotate for you. autoRotateIntervalMin takes 0 (new IP on every request without a session ID), 1, 5, 30 or 60. stickyTtlSec takes 300, 1800 or 3600.
curl -X PATCH https://api.premip.com/proxies/PROXY_ID/settings \
-H "X-Api-Key: $PREMIP_API_KEY" \
-H "Content-Type: application/json" \
-d '{"stickyTtlSec":1800,"autoRotateEnabled":true,"autoRotateIntervalMin":30}'5. Rotate from a scheduler or URL
Tools that can only call a URL, such as cron, a browser profile manager or a monitoring job, can use the GET form. Keep the key in the scheduler secret store because it is part of the request header.
curl "https://api.premip.com/api/v1/rotate?proxy=PROXY_ID" \
-H "X-Api-Key: $PREMIP_API_KEY"Complete script
Fetches a list of public pages through one proxy and rotates its IP every 25 pages, waiting out the cooldown when needed. Put your own URLs in the list (urls.txt, one per line, in the Bash version) and respect each site terms and robots rules.
#!/usr/bin/env bash
# Needs curl, jq and awk. Run: PREMIP_API_KEY=premip_... bash script.sh
set -euo pipefail
API=https://api.premip.com
H="X-Api-Key: $PREMIP_API_KEY"
ROTATE_EVERY=25
proxy=$(curl -fsS $API/proxies -H "$H" | jq '.[0]')
id=$(echo "$proxy" | jq -r .id)
url="http://$(echo "$proxy" | jq -r '.username + ":" + .password + "@" + .host + ":" + (.port|tostring)')"
rotate() {
while true; do
out=$(curl -sS -X POST "$API/api/v1/proxies/$id/rotate" -H "$H" -w '\n%{http_code}')
code=$(echo "$out" | tail -n1)
if [ "$code" = "429" ]; then sleep "$(echo "$out" | head -n1 | jq -r '.retryAfterSec // 60')"; continue; fi
echo "$out" | head -n1 | jq -r .currentIp; return
done
}
n=0
while read -r page; do
n=$((n + 1))
curl -fsS -x "$url" -o /dev/null -w "$page -> %{http_code}\n" "$page" || echo "$page -> failed"
if [ $((n % ROTATE_EVERY)) -eq 0 ]; then echo "Rotated, new IP: $(rotate)"; fi
done < urls.txt
Troubleshooting
| Problem | What to do |
|---|---|
| 429 PROXY_ROTATE_COOLDOWN | Normal. Wait retryAfterSec before the next rotation of that proxy. Rotating other proxies is not affected. |
| Sticky session IP changes earlier than expected | The TTL ended (see stickyTtlSec) or the session ID differs between requests. Reuse the exact same ID. |
| Authentication fails after adding -session- | Do not use hyphens inside the session ID value you build from user input, and keep it under 128 characters. Use letters, digits and underscores. |
| Country code is rejected | The -country- code must match a location you bought. Check the countryCode field in GET /proxies. |