Rayley Documentation

Bun

Use a static IP address from Bun using fetch.

Bun's fetch has built-in proxy support via the proxy option. No extra packages needed.

Prerequisites

  • A Rayley account with a Proxy Token (rpt_*). Create one in the dashboard.

Quick start

Pass the proxy option to fetch to route the request through your static IP address. Send the token via the Proxy-Authorization header:

proxy-request.ts
const response = await fetch('https://api.example.com/data', {
  proxy: 'https://proxy.rayley.com',
  headers: {
    'Proxy-Authorization': 'Bearer rpt_your_token_here',
  },
});

const data = await response.json();
console.log(data);

Other ways to connect

Embed the token in the proxy URL instead of using the Proxy-Authorization header:

proxy-url-auth.ts
const response = await fetch('https://api.example.com/data', {
  proxy: 'https://token:rpt_your_token_here@proxy.rayley.com',
});

const data = await response.json();
console.log(data);

Full example

Below is a script you can use to test your Proxy Token. It reads the token from the RAYLEY_TOKEN environment variable and takes an optional URL argument (defaulting to https://example.com) to make a request using your static IP address.

example.ts
const proxyToken = process.env.RAYLEY_TOKEN;
const targetUrl = Bun.argv[2] || 'https://example.com/';

if (!proxyToken) {
  console.error('Set RAYLEY_TOKEN to your Proxy Token.');
  process.exit(1);
}

const response = await fetch(targetUrl, {
  proxy: 'https://proxy.rayley.com',
  headers: {
    'Proxy-Authorization': `Bearer ${proxyToken}`,
  },
});

if (response.status === 407) {
  console.error('Authentication failed. Check your RAYLEY_TOKEN.');
  process.exit(1);
}

const data = await response.json();
console.log(`Status: ${response.status}`);
console.log(JSON.stringify(data, null, 2));

Run it:

RAYLEY_TOKEN=rpt_your_token bun example.ts https://example.com/

On this page