Get an API key Your API keys

VirginProxy with Go

A complete, copy-ready client for VirginProxy in Go: 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:

Shell
export VP_KEY="vp_live_…"

The client

vp.go
package vp

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"time"
)

type Response struct {
	ID           string            `json:"id"`
	Status       int               `json:"status"`
	Headers      map[string]string `json:"headers"`
	Body         string            `json:"body"`
	BodyEncoding string            `json:"body_encoding"`
	Exit         struct {
		IP      string `json:"ip"`
		Country string `json:"country"`
	} `json:"exit"`
	Cost struct {
		Credits int `json:"credits"`
	} `json:"cost"`
}

type apiError struct {
	Error struct {
		Code         string `json:"code"`
		Message      string `json:"message"`
		RetryAfterMS int    `json:"retry_after_ms"`
	} `json:"error"`
}

var retryable = map[string]bool{"rate_limited": true, "concurrency_limited": true, "busy": true,
	"maintenance": true, "timeout": true, "target_unreachable": true, "connection_failed": true}

var client = &http.Client{Timeout: 180 * time.Second}

// Fetch sends one request through VirginProxy. options may hold method, headers, body, session …
func Fetch(url string, options map[string]any, retries int) (*Response, error) {
	payload := map[string]any{"url": url}
	for k, v := range options {
		payload[k] = v
	}
	body, _ := json.Marshal(payload)
	for attempt := 0; ; attempt++ {
		req, _ := http.NewRequest("POST", "https://api.virginproxy.com/v1/requests", bytes.NewReader(body))
		req.Header.Set("Authorization", "Bearer "+os.Getenv("VP_KEY"))
		req.Header.Set("Content-Type", "application/json")
		res, err := client.Do(req)
		if err != nil {
			return nil, err
		}
		if res.StatusCode == http.StatusOK {
			var out Response
			err = json.NewDecoder(res.Body).Decode(&out)
			res.Body.Close()
			return &out, err
		}
		var e apiError
		json.NewDecoder(res.Body).Decode(&e)
		res.Body.Close()
		if !retryable[e.Error.Code] || attempt == retries {
			return nil, fmt.Errorf("%s: %s", e.Error.Code, e.Error.Message)
		}
		wait := time.Duration(e.Error.RetryAfterMS) * time.Millisecond
		if wait == 0 {
			wait = time.Second << attempt
		}
		time.Sleep(wait)
	}
}

What the client does

See Limits and retries for the reasoning and Errors for every code.

Keep one IP for several requests

Go
first, _ := vp.Fetch("https://shop.example/cart", map[string]any{"session": "cart-42"}, 3)
second, _ := vp.Fetch("https://shop.example/checkout", map[string]any{"session": "cart-42"}, 3)
fmt.Println(first.Exit.IP == second.Exit.IP) // true for up to 10 minutes

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.

Go
sem := make(chan struct{}, 5) // = your plan's limit
var wg sync.WaitGroup
for _, url := range urls {
	wg.Add(1)
	go func(url string) {
		defer wg.Done()
		sem <- struct{}{}
		defer func() { <-sem }()
		if r, err := vp.Fetch(url, nil, 3); err == nil {
			fmt.Println(url, r.Status, r.Cost.Credits)
		}
	}(url)
}
wg.Wait()

Was this page helpful?

Last updated