VirginProxy with Node.js
A complete, copy-ready client for VirginProxy in Node.js (18 or newer): one function that sends a request, retries what is worth retrying, and returns the answer with its price. It uses nothing but the standard HTTP library.
Set your key as an environment variable first:
export VP_KEY="vp_live_…"The client
// vp.mjs: no packages needed
const API = "https://api.virginproxy.com/v1/requests";
const RETRYABLE = new Set(["rate_limited", "concurrency_limited", "busy", "maintenance",
"timeout", "target_unreachable", "connection_failed"]);
export async function fetchPage(url, options = {}, retries = 3) {
for (let attempt = 0; ; attempt++) {
const res = await fetch(API, {
method: "POST",
headers: { Authorization: `Bearer ${process.env.VP_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ url, ...options }),
signal: AbortSignal.timeout(180_000),
});
const data = await res.json();
if (res.ok) return data;
const { code, message, retry_after_ms } = data.error;
if (!RETRYABLE.has(code) || attempt === retries) throw new Error(`${code}: ${message}`);
await new Promise((r) => setTimeout(r, retry_after_ms ?? 1000 * 2 ** attempt));
}
}
const page = await fetchPage("https://httpbin.org/html");
console.log(page.status, page.exit.ip, page.cost.credits);What the client does
- Sends one
POST /v1/requestsper call and waits up to 180 seconds: enough for your plan's timeout, the wait for a free slot and afresh_ip. Withretry, allow one more timeout per extra attempt. - Retries only errors that can succeed later (
rate_limited,concurrency_limited,busy,maintenance,timeout,target_unreachable,connection_failed), waitingretry_after_mswhen the API sends it. None of these is charged. - Raises every other error at once: a wrong URL or key does not get better by trying again.
See Limits and retries for the reasoning and Errors for every code.
Keep one IP for several requests
const first = await fetchPage("https://shop.example/cart", { session: "cart-42" });
const second = await fetchPage("https://shop.example/checkout", { session: "cart-42" });
console.log(first.exit.ip === second.exit.ip); // true for up to 10 minutesImages, PDFs and other files
import { writeFile } from "node:fs/promises";
const data = await fetchPage("https://httpbin.org/image/png");
await writeFile("image.png", Buffer.from(data.body, data.body_encoding === "base64" ? "base64" : "utf8"));Many URLs in parallel
Keep at most as many requests running as your plan allows (1 on Free, 5 on Starter, 20 on Growth, 50 on Scale). More only produces concurrency_limited.
async function fetchAll(urls, limit = 5) { // = your plan's limit
const results = new Array(urls.length);
let next = 0;
const worker = async () => {
while (next < urls.length) {
const i = next++;
results[i] = await fetchPage(urls[i]).catch((err) => ({ error: err.message }));
}
};
await Promise.all(Array.from({ length: limit }, worker));
return results;
}Was this page helpful?
Last updated