For AI agents: a documentation index is available at /llms.txt
Skip to main content

Retry browser sessions with exponential backoff

When Browserless is over capacity it returns a 429, and individual browser sessions can fail mid-run due to transient network or browser errors. Exponential backoff handles both by waiting baseDelay * 2^attempt ms between retries so you don't hammer a busy service and you don't lose work to one-off failures. For details on capacity and queue behavior, see session management.

Prerequisites

Steps

Browserless queues REST requests automatically when below MAX_QUEUE_LENGTH, so most capacity issues resolve without client-side retry. Network failures and 429s above the queue limit still need handling. Wrap your fetch in a retry loop that checks the response status before parsing.

View Full Code on GitHub

1. Send the request with retry

The loop doubles DELAY after each failed attempt. A 200 exits immediately; anything else retries up to MAX_RETRIES times:

#!/bin/bash
MAX_RETRIES=3
DELAY=1
for i in $(seq 1 $MAX_RETRIES); do
STATUS=$(curl -s -o response.png -w "%{http_code}" -X POST \
"https://production-sfo.browserless.io/screenshot?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{"url": "https://scraping-sandbox.netlify.app/dashboard"}')
if [ "$STATUS" = "200" ]; then
echo "Success"
exit 0
fi
echo "Attempt $i failed with status $STATUS, retrying in ${DELAY}s..."
sleep $DELAY
DELAY=$((DELAY * 2))
done
echo "Failed after $MAX_RETRIES attempts" && exit 1

2. Check the output

Each failed attempt prints Attempt N failed with status 429, retrying in Ns.... On success, response.png contains the screenshot and the script exits 0. If all retries are exhausted, the script exits 1.

Was this page helpful?