Skip to content
LightningBytes

Code Snippets

Putting a generated endpoint to work in cURL, JavaScript, Python, Go, Java, Ruby and PHP, including the tunnelling form the dashboard shows.

Last updated

Once you have an endpoint, the next question is how to use it in your own code. The dashboard shows a snippet for the language you select; this article explains the shape of each.

Generate an endpoint first with Generating Proxy Endpoints, then copy it in the URL format if the language expects a connection string.

cURL

The dashboard's own example uses the tunnelling form, which is what you want for HTTPS targets through an HTTP proxy.

curl --proxytunnel \
  --proxy "http://customer-USERNAME:PASSWORD@proxy.lightningbytes.com:1080" \
  https://ip.lightningbytes.com

--proxytunnel makes cURL open a tunnel through the proxy for the encrypted request. Without it, cURL may try to send the request in a form the proxy refuses for HTTPS destinations. Use --proxy with the URL-format endpoint.

JavaScript

Two common routes: a fetch with an explicit agent, or an HTTP client library.

// Node: route fetch through the proxy with an agent.
import { ProxyAgent, fetch as undiciFetch } from "undici";

const agent = new ProxyAgent("http://customer-USERNAME:PASSWORD@proxy.lightningbytes.com:1080");

const res = await undiciFetch("https://ip.lightningbytes.com", { dispatcher: agent });
console.log(await res.text());
// Playwright: bind the proxy at context creation.
const context = await browser.newContext({
  proxy: { server: "http://proxy.lightningbytes.com:1080", username: "customer-USERNAME", password: "PASSWORD" },
});

Browsers cannot be given a proxy from page JavaScript, so browser automation configures it at the browser or context level rather than in a fetch call.

Python

import httpx

proxy = "http://customer-USERNAME:PASSWORD@proxy.lightningbytes.com:1080"

with httpx.Client(proxy=proxy, timeout=20.0) as client:
    response = client.get("https://ip.lightningbytes.com")
    response.raise_for_status()
    print(response.text)

With requests, the equivalent is a mapping:

import requests

proxies = {
    "http": "http://customer-USERNAME:PASSWORD@proxy.lightningbytes.com:1080",
    "https": "http://customer-USERNAME:PASSWORD@proxy.lightningbytes.com:1080",
}

r = requests.get("https://ip.lightningbytes.com", proxies=proxies, timeout=20)

In both, set a timeout. A request with no timeout can hang a worker indefinitely when an address is unhealthy.

Go

proxyURL, _ := url.Parse("http://customer-USERNAME:PASSWORD@proxy.lightningbytes.com:1080")
client := &http.Client{
    Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
    Timeout:   20 * time.Second,
}
resp, err := client.Get("https://ip.lightningbytes.com")

Go reads proxy settings from the environment by default with http.ProxyFromEnvironment, so setting HTTPS_PROXY is an alternative to configuring the transport directly.

Java

Set the proxy system properties before creating the connection.

System.setProperty("https.proxyHost", "proxy.lightningbytes.com");
System.setProperty("https.proxyPort", "1080");
// Authenticated proxies need a custom Authenticator for the credentials.

Where the proxy requires authentication, attach an Authenticator rather than putting credentials in the URL, because Java's standard client does not read them from the URL.

Ruby

require "net/http"

proxy = URI("http://proxy.lightningbytes.com:1080")
http = Net::HTTP.new("ip.lightningbytes.com", 443, proxy.host, proxy.port)
http.use_ssl = true

request = Net::HTTP::Get.new("/")
request.basic_auth("customer-USERNAME", "PASSWORD")
puts http.request(request).body

PHP

$ch = curl_init("https://ip.lightningbytes.com");
curl_setopt($ch, CURLOPT_PROXY, "proxy.lightningbytes.com:1080");
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "customer-USERNAME:PASSWORD");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
echo curl_exec($ch);
curl_close($ch);

Common mistakes

SymptomCause
Works for HTTP, fails for HTTPSThe proxy flag for tunnelling is missing
Authentication errorCredentials in the wrong field for that client, or copied with surrounding whitespace
Hangs indefinitelyNo timeout set
Uses your own addressThe client ignored the setting, often because the proxy was given in a form it does not read

The last one is the most common. Confirm the exit address rather than assuming: the Proxy Checker reports what a given endpoint presents, and the IP Lookup tells you where that address sits.

Related

Start working with cleaner IPs

Clean, pre-filtered residential and mobile proxies, sign up and send your first request in minutes.

We use cookies for authentication and security. With your consent we also enable optional marketing & analytics cookies. See our privacy policy.