When running actions, you typically run your code in a data center such as AWS.

To avoid bot detection, rate limits, and accessing geo-restricted content, we recommend using a proxy.

Proxy Types

Anon provides two types of proxies:

Anon Provided Proxy

We use residential IP addresses that come from human devices, which are less likely to be detected by websites. These proxies are from:

  • Home routers
  • Mobile devices

Bring Your Own Proxy

You can use your own proxy if you have a specific use case.

Proxy Examples

Anon Proxy

Here’s an example for how to configure your own proxy:

import { setupAnonBrowserWithContext } from "@anon/browser";

const { browserContext } = await setupAnonBrowserWithContext(
  client,
  account,
  { 
    type: "local", 
    input: {
      proxy: { isAnonProxyEnabled: true } 
    }
  },
);

Bring Your Own Proxy

Here’s an example for how to configure your own proxy:

import { setupAnonBrowserWithContext } from "@anon/browser";

const { browserContext } = await setupAnonBrowserWithContext(
  client,
  account,
  { 
    type: "local", 
    input: {
      proxy: { server: "http://proxy.example.com:8080", username: "foo", password: "bar" } 
    }
  },
);

Proxy Options

Server Types

Anon supports multiple proxy protocols:

// HTTP/HTTPS proxy
proxy: {
  server: "http://proxy.example.com:8080"
}

// SOCKS proxy
proxy: {
  server: "socks5://proxy.example.com:1080"
}

Authentication

Proxies can be authenticated with a username and password:

proxy: {
  server: "proxy.example.com:8080",
  username: "proxyuser",
  password: "proxypass"
}

Best Practices

Proxy Health Check

Test proxy connectivity before running actions:

import { HttpsProxyAgent } from "https-proxy-agent";
import fetch from "node-fetch";
import { ProxyConfig } from "@anon/sdk-typescript";

const isProxyHealthy = async (proxy: ProxyConfig) => {
  try {
    const ip = await getCurrentIpAddress(proxy);
    return ip.ipAddress !== null;
  } catch (error) {
    return false;
  }
};

const getCurrentIpAddress = async (proxy: ProxyConfig): Promise<IpWithCoordinates> => {
  const proxyUrl = proxy.username && proxy.password
    ? `http://${proxy.username}:${proxy.password}@${proxy.server}`
    : `http://${proxy.server}`;

  const agent = new HttpsProxyAgent(proxyUrl);
  const response = await fetch("https://lumtest.com/myip.json", { agent });
  const json = (await response.json()) as any;
  return {
    ipAddress: json.ip,
    latitude: json.geo.latitude,
    longitude: json.geo.longitude,
    cityName: json.geo.city,
    countryCode: json.country,
  };
}

Error Handling

Handle proxy-related errors gracefully:

import { AnonRuntime } from "@anon/sdk-typescript";
const anon = new AnonRuntime({ apiKey: "anon_..." });

try {
  await anon.run({
    appUserId: "my-user-id",
    apps: ["example"],
    proxy: {
      server: "proxy.example.com:8080",
      username: "proxyuser",
      password: "proxypass"
    },
    action: async (page) => {
      // Your automation code
    }
  });
} catch (error) {
  if (error.message.includes("proxy connection failed")) {
    // Handle proxy error
    console.error("Proxy connection failed:", error);
  }
  throw error;
}