# Node.js

Source: https://docs.evomi.com/proxy-instructions/integration-guides/nodejs/

This guide explains how to use Evomi proxies with Node.js using **Axios** (the most popular HTTP client) and **node-fetch**. Both libraries support HTTP proxies, and SOCKS5 support is available via additional packages.

## Prerequisites

- Node.js 18+ installed
- Your Evomi proxy credentials (username and password)

## Axios

### Installation

```bash
npm install axios
```

### HTTP Proxy

Axios has built-in support for HTTP/HTTPS proxies:

```javascript
const axios = require('axios');

const client = axios.create({
  proxy: {
    protocol: 'http',
    host: 'rp.evomi.com',
    port: 1000,
    auth: {
      username: 'your_username',
      password: 'your_password_session-anychars_mode-speed',
    },
  },
});

client.get('https://ip.evomi.com/s')
  .then(res => console.log('Proxy IP:', res.data.trim()))
  .catch(err => console.error('Error:', err.message));
```

### SOCKS5 Proxy

Axios does not natively support SOCKS5. Use `socks-proxy-agent`:

```bash
npm install socks-proxy-agent
```

```javascript
const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');

const proxyUser = 'your_username';
const proxyPass = 'your_password_session-anychars_mode-speed';
const agent = new SocksProxyAgent(
  `socks5h://${proxyUser}:${proxyPass}@rp.evomi.com:1002`
);

axios.get('https://ip.evomi.com/s', {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false,
})
  .then(res => console.log('Proxy IP:', res.data.trim()))
  .catch(err => console.error('Error:', err.message));
```

When using an agent, set `proxy: false` in Axios options to prevent Axios from trying to apply its own proxy configuration on top of the agent.

## node-fetch

### Installation

```bash
npm install node-fetch https-proxy-agent
```

### HTTP Proxy

```javascript
const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');

const proxyUser = 'your_username';
const proxyPass = 'your_password_session-anychars_mode-speed';
const agent = new HttpsProxyAgent(
  `http://${proxyUser}:${proxyPass}@rp.evomi.com:1000`
);

fetch('https://ip.evomi.com/s', { agent })
  .then(res => res.text())
  .then(body => console.log('Proxy IP:', body.trim()))
  .catch(err => console.error('Error:', err.message));
```

### SOCKS5 Proxy

```bash
npm install node-fetch socks-proxy-agent
```

```javascript
const fetch = require('node-fetch');
const { SocksProxyAgent } = require('socks-proxy-agent');

const proxyUser = 'your_username';
const proxyPass = 'your_password_session-anychars_mode-speed';
const agent = new SocksProxyAgent(
  `socks5h://${proxyUser}:${proxyPass}@rp.evomi.com:1002`
);

fetch('https://ip.evomi.com/s', { agent })
  .then(res => res.text())
  .then(body => console.log('Proxy IP:', body.trim()))
  .catch(err => console.error('Error:', err.message));
```

## ES Modules Syntax

If your project uses ES modules (`"type": "module"` in `package.json`), use `import` instead of `require`:

```javascript
import axios from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';

const proxyUser = 'your_username';
const proxyPass = 'your_password_session-anychars_mode-speed';
const agent = new SocksProxyAgent(
  `socks5h://${proxyUser}:${proxyPass}@rp.evomi.com:1002`
);

const { data } = await axios.get('https://ip.evomi.com/s', {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false,
});
console.log('Proxy IP:', data.trim());
```

## Evomi Proxy Endpoints

| Proxy Type | HTTP | HTTPS | SOCKS5 |
|---|---|---|---|
| **Residential** | `rp.evomi.com:1000` | `rp.evomi-proxy.com:1001` | `rp.evomi.com:1002` |
| **Mobile** | `mp.evomi.com:3000` | `mp.evomi-proxy.com:3001` | `mp.evomi.com:3002` |
| **Datacenter** | `dcp.evomi.com:2000` | `dcp.evomi-proxy.com:2001` | `dcp.evomi.com:2002` |

HTTPS endpoints use the `evomi-proxy.com` domain — that is the hostname our TLS certificate covers, and it reaches the same servers. A client that verifies certificates will reject the `evomi.com` form on the HTTPS port.

## Tips and Troubleshooting

- **Credentials**: Double-check your username and password. The password format is `your_password_session-anychars_mode-speed`.
- **`proxy: false` with agents**: When using `httpAgent`/`httpsAgent` in Axios, always set `proxy: false` to avoid conflicts.
- **`socks5h://` vs `socks5://`**: Use `socks5h://` to resolve DNS on the proxy server (recommended). Use `socks5://` for local DNS resolution.
- **Timeouts**: Set timeouts to avoid hanging requests: `axios.get(url, { timeout: 15000 })` or `fetch(url, { agent, signal: AbortSignal.timeout(15000) })`.
- **Special characters**: If your password contains special characters, URL-encode them with `encodeURIComponent()`.
- **node-fetch v3**: Version 3+ is ESM-only. If using CommonJS, stick with `node-fetch@2` or switch to ES modules.
