# Code Sample

This page provides ready-to-use **code examples** for integrating Rapidproxy proxies in various programming languages and tools.\
You’ll find sample usage for both [**Dynamic (Rotating) Residential Proxies**](https://docs.rapidproxy.io/proxies/dynamic-residential-proxy) and [**Static Residential Proxies**](https://docs.rapidproxy.io/proxies/static-residential-proxy) with common authentication methods.

***

### 1. General Usage & How to Read These Examples

Rapidproxy supports multiple proxy authentication methods including:

* [**Username & Password Auth**](https://docs.rapidproxy.io/proxies/dynamic-residential-proxy/get-proxy/user-and-pass-auth) — credentials for each sub-account
* [**IP Whitelisting**](https://docs.rapidproxy.io/proxies/dynamic-residential-proxy/get-proxy/api/ip-whitelisting) — allowlisted source IP authentication

You’ll see examples using both methods where applicable.

> Note: Replace placeholder values like `YOUR_PROXY_HOST`, `YOUR_PORT`, `YOUR_USERNAME`, `YOUR_PASSWORD` with real values from your Rapidproxy dashboard.

***

### 2. cURL Examples

#### Dynamic Residential Proxy — Username & Password

```
curl -x http://YOUR_USERNAME:YOUR_PASSWORD@YOUR_PROXY_HOST:YOUR_PORT https://example.com
```

#### Dynamic Residential Proxy — IP Whitelist (no credentials)

```
curl -x http://YOUR_PROXY_HOST:YOUR_PORT https://example.com
```

*(Ensure your current IP is whitelisted before using this request.)*

#### Static Residential Proxy

```
curl -x http://YOUR_USERNAME:YOUR_PASSWORD@YOUR_STATIC_IP:YOUR_PORT https://example.com
```

***

### 3. Python Example (requests library)

#### With Username & Password Auth

```
import requests

proxy = "http://YOUR_USERNAME:YOUR_PASSWORD@YOUR_PROXY_HOST:YOUR_PORT"

proxies = {
    "http": proxy,
    "https": proxy
}

url = "https://example.com"
response = requests.get(url, proxies=proxies)
print(response.text)
```

#### With IP Whitelisting Only

```
import requests

proxy = "http://YOUR_PROXY_HOST:YOUR_PORT"

proxies = {
    "http": proxy,
    "https": proxy
}

response = requests.get("https://example.com", proxies=proxies)
print(response.text)
```

***

### 4. Node.js Example (axios)

#### With Username & Password Auth

```
import axios from "axios";

const proxy = {
  host: "YOUR_PROXY_HOST",
  port: YOUR_PORT,
  auth: {
    username: "YOUR_USERNAME",
    password: "YOUR_PASSWORD"
  }
};

axios.get("https://example.com", { proxy })
  .then(res => console.log(res.data))
  .catch(err => console.error(err));
```

#### With IP Whitelisting Only

```
import axios from "axios";

const proxy = {
  host: "YOUR_PROXY_HOST",
  port: YOUR_PORT
};

axios.get("https://example.com", { proxy })
  .then(res => console.log(res.data))
  .catch(err => console.error(err));
```

***

### 5. Puppeteer Example (Headless Browser)

#### With Username & Password Auth

```
const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({
    args: [
      "--proxy-server=YOUR_PROXY_HOST:YOUR_PORT"
    ]
  });

  const page = await browser.newPage();
  await page.authenticate({
    username: "YOUR_USERNAME",
    password: "YOUR_PASSWORD"
  });

  await page.goto("https://example.com");
  console.log(await page.content());
  await browser.close();
})();
```

***

### 6. Java Example (Apache HttpClient)

#### With Username & Password Auth

```
HttpHost proxy = new HttpHost("YOUR_PROXY_HOST", YOUR_PORT);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
    new AuthScope(proxy),
    new UsernamePasswordCredentials("YOUR_USERNAME", "YOUR_PASSWORD")
);

CloseableHttpClient client = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .build();

HttpGet request = new HttpGet("https://example.com");
HttpResponse response = client.execute(proxy, request);

System.out.println(EntityUtils.toString(response.getEntity()));
```

***

### 7. Tips for Using Proxies in Code

#### Protocol Support

* HTTP and HTTPS are both supported
* SOCKS5 can be used if your client supports the protocol

#### Sticky Sessions (Dynamic Proxies)

If you configured a **sticky session** when generating proxy credentials:

* Each request within the session will use the **same IP**
* Useful for login, form submission, and session-based automation

#### IP Whitelist

If using IP whitelisting:

* Confirm the source IP of your application or server is added to the whitelist
* Requests from non-whitelisted IPs will be blocked

***

### 8. Troubleshooting

If your code receives proxy errors:

* Double-check proxy host/port values
* Ensure credentials are correct (for username/password authentication)
* Verify your IP is whitelisted (if applicable)
* Check that your tool/client supports the proxy protocol you are using

If issues persist, contact support:

* **Online live chat on the Rapidproxy website**
* **Email:** `support@rapidproxy.io`

***

### 9. Security Notes

* Never share credentials publicly
* Store proxy usernames & passwords in secure configuration or environment variables
* Rotate credentials if compromised


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rapidproxy.io/proxies/code-sample.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
