Rayley Documentation

Axios

Use a static IP address from Node.js using Axios.

Axios has built-in proxy support that works with Rayley out of the box. Configure the proxy on a per-client or global basis to route requests through your static IP address.

Prerequisites

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

Installation

npm install axios

Quick start

Create an Axios client with the proxy option to route its requests through your static IP address. Pass the token via the Proxy-Authorization header:

proxy-request.mjs
import axios from 'axios';

const client = axios.create({
  proxy: {
    protocol: 'https',
    host: 'proxy.rayley.com',
  },
  headers: {
    'Proxy-Authorization': 'Bearer rpt_your_token_here',
  },
});

const response = await client.get('https://api.example.com/data');
console.log(response.data);

Global defaults

Set axios.defaults to route all requests through Rayley globally, including from libraries that use axios internally:

proxy-all.mjs
import axios from 'axios';

axios.defaults.proxy = {
  protocol: 'https',
  host: 'proxy.rayley.com',
};
axios.defaults.headers.common['Proxy-Authorization'] = 'Bearer rpt_your_token_here';

// All axios requests now go through the proxy
const response = await axios.get('https://api.example.com/data');
console.log(response.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.mjs
import axios from 'axios';

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

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

const client = axios.create({
  proxy: {
    protocol: 'https',
    host: 'proxy.rayley.com',
  },
  headers: {
    'Proxy-Authorization': `Bearer ${proxyToken}`,
  },
  timeout: 30000,
});

try {
  const response = await client.get(targetUrl);

  if (response.status === 407) {
    throw new Error('Authentication failed. Check your RAYLEY_TOKEN.');
  }

  console.log(`Status: ${response.status}`);
  console.log(JSON.stringify(response.data, null, 2));
} catch (error) {
  if (error.response?.status === 407) {
    console.error('Authentication failed. Check your RAYLEY_TOKEN.');
  } else {
    console.error('Request failed:', error.message);
  }
  process.exit(1);
}

Run it:

RAYLEY_TOKEN=rpt_your_token node example.mjs https://example.com/

On this page