Node.js と TypeScript
コネクションプーリングを活用しながら、undici・axios・標準 fetch・SOCKS5 で Node.js と TypeScript のリクエストを専用固定 IPv4 経由にできます。
このページの目次8
適切なクライアントはプロキシプロトコルと Node.js ランタイムによって異なります。利用中のスタックに合うパターンを選んでください。
| クライアント | 対応プロキシスキーム | 最適な用途 |
|---|---|---|
undici(ProxyAgent) |
https://、http:// |
fetch を使う Node 20+ サーバーおよびサーバーレス関数のデフォルト推奨 |
axios + https-proxy-agent |
https://、http:// |
既存の axios コードベース |
標準 fetch + NODE_USE_ENV_PROXY=1 |
https://、http:// |
Node 22.21+ および Node 24+ で追加依存なし |
socks-proxy-agent |
socks5h:// |
SOCKS5 が必要な node:https または axios クライアント |
ステップ 1. プロキシ URL を取得する
- Prontaro ダッシュボード(my.prontaro.com/ips)の IP アドレス を開き、専用の IPv4 を選択します。
- プロキシ認証情報 から
HTTPSプロキシ URL(:443)をコピーします。
プロキシ文字列を環境変数 PRONTARO_PROXY_URL に設定します。
export PRONTARO_PROXY_URL="https://user:pass@<entry>.prontaro.io:443"undici(ProxyAgent)— 推奨
undici をインストールし、リクエスト間で単一の TCP/TLS keep-alive プールを共有できるよう、モジュールレベルで 1 つだけ ProxyAgent を作成します。
npm install undici// egress-client.ts
import { fetch, ProxyAgent } from 'undici';
const proxyUrl = process.env.PRONTARO_PROXY_URL;
if (!proxyUrl) {
throw new Error('PRONTARO_PROXY_URL is not set');
}
// One shared agent per process — do not close it after every request
export const proxyAgent = new ProxyAgent({
uri: proxyUrl,
connections: 10,
});
export async function checkOutboundIp(): Promise<string> {
const res = await fetch('https://api.ipify.org', {
dispatcher: proxyAgent,
signal: AbortSignal.timeout(15_000),
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${await res.text()}`);
}
return (await res.text()).trim();
}
// Close only during graceful process shutdown
process.once('SIGTERM', () => {
void proxyAgent.close();
});
console.log(await checkOutboundIp());アウトバウンド IP を確認します。
npx tsx egress-client.ts
203.0.113.10api.ipify.org が専用 IPv4 を返すことを確認したら、ダッシュボード(IP アドレス → プロキシ認証情報 → アクセス)で接続先を絞り込めます。
axios + https-proxy-agent
Axios 組み込みの proxy オプションは https:// プロキシホストの扱いが不安定で、環境変数とも競合しやすいです。HttpsProxyAgent を httpsAgent に渡し、proxy: false を明示指定してください。
npm install axios https-proxy-agent// axios-client.ts
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
const proxyUrl = process.env.PRONTARO_PROXY_URL;
if (!proxyUrl) {
throw new Error('PRONTARO_PROXY_URL is not set');
}
const httpsAgent = new HttpsProxyAgent(proxyUrl);
export const partnerApi = axios.create({
httpsAgent,
proxy: false, // disable Axios's built-in HTTP_PROXY handling
timeout: 15_000,
});
const { data } = await partnerApi.get<string>('https://api.ipify.org');
console.log(data.trim());NODE_USE_ENV_PROXY=1 を使った標準 fetch(Node 22 / 24)
Node.js 24+ および Node.js 22.21+ では、NODE_USE_ENV_PROXY=1(または --use-env-proxy)を付けて起動すると、追加依存なしで組み込みの fetch() が HTTPS_PROXY・HTTP_PROXY・NO_PROXY を参照します。
// check-env.mjs
const res = await fetch('https://api.ipify.org', {
signal: AbortSignal.timeout(15_000),
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
console.log((await res.text()).trim());HTTPS_PROXY="$PRONTARO_PROXY_URL" \
NO_PROXY="localhost,127.0.0.1,::1,169.254.169.254,.internal" \
NODE_USE_ENV_PROXY=1 \
node check-env.mjsプロセス内のすべての fetch() に適用されるため、社内サービスやテレメトリが Prontaro を経由しないよう必ず NO_PROXY を設定してください。
socks-proxy-agent を使った SOCKS5(socks5h://)
undici.ProxyAgent は http:// と https:// のみ対応しています。socks5h://(:1080)を使う場合は、SocksProxyAgent を axios または node:https と組み合わせます。
npm install axios socks-proxy-agent// socks-client.ts
import axios from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';
// Use socks5h:// so DNS resolves on the Prontaro server
const socksUrl = process.env.PRONTARO_SOCKS_URL; // socks5h://user:pass@<entry>.prontaro.io:1080
if (!socksUrl) {
throw new Error('PRONTARO_SOCKS_URL is not set');
}
const socksAgent = new SocksProxyAgent(socksUrl);
const { data } = await axios.get<string>('https://api.ipify.org', {
httpAgent: socksAgent,
httpsAgent: socksAgent,
proxy: false,
timeout: 15_000,
});
console.log(data.trim());Node.js でよくある問題
- グローバル
fetchと npm 版ProxyAgentの混在。 同梱版とのバージョン差でdispatcherが壊れるため、両方とも'undici'からインポートします。 axiosでproxy: falseを省略する。httpsAgentと同時にHTTPS_PROXYが設定されていると、Axios が二重にプロキシを通そうとします。SocksProxyAgentをfetch({ dispatcher })に渡す。SocksProxyAgentはundici.Dispatcherではなく Node のhttp.Agent実装です。- リクエストごとのエージェント生成。 ハンドラ内で毎回
new ProxyAgent(...)を作ると毎回 TLS 接続が発生し、IP あたりの同時接続上限に達する恐れがあります。
Relay アドレス経由で API を呼び出す
プロキシエージェントを設定できないエッジランタイムやコードベースでは、ダッシュボード(IP アドレス)で Relay を作成し、fetch で直接呼び出せます。
const res = await fetch(`${process.env.PRONTARO_RELAY_URL}/v1/status`, {
headers: {
Authorization: `Bearer ${process.env.PARTNER_API_TOKEN}`,
// パスワードを設定している場合のみ含める(ダッシュボードの「IP アドレス → Relay アドレス → アクセス」):
'Prontaro-Relay-Auth': `${process.env.RELAY_LOGIN}:${process.env.RELAY_PASSWORD}`,
},
signal: AbortSignal.timeout(15_000),
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${await res.text()}`);
}
console.log(await res.json());TLS 終端の詳細や制限は Relay アドレス を参照してください。
次のステップ
- curl と環境変数 —
curl -vで407や403エラーを診断できます。 - HTTP・SOCKS5 プロキシ —
access・targets許可リストの設定と認証情報ローテーションを確認できます。 - Vercel 向け固定 IP — Vercel Functions でのプロキシと Relay アドレスの設定手順です。
- AWS Lambda 向け固定 IP — VPC 構築や NAT Gateway なしで固定 IP を利用する手順です。
このページに問題がありますか? お問い合わせ