Rayley Documentation

Go

Use a static IP address from Go using net/http.

Go's standard library net/http supports proxies through the Transport.Proxy field. No external dependencies needed.

Prerequisites

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

Quick start

Set Transport.Proxy to route requests through your static IP address. Embed the token in the proxy URL as basic auth credentials:

main.go
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
)

func main() {
	proxyURL, _ := url.Parse("https://token:rpt_your_token_here@proxy.rayley.com")

	client := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		},
	}

	resp, err := client.Get("https://api.example.com/data")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
}

Other ways to connect

Set the HTTPS_PROXY environment variable to route all requests through Rayley without any code changes:

export HTTPS_PROXY="https://token:rpt_your_token_here@proxy.rayley.com"

Go's default transport uses http.ProxyFromEnvironment, so all requests pick up the proxy automatically:

main.go
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	// Uses HTTPS_PROXY environment variable automatically
	resp, err := http.Get("https://api.example.com/data")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
}

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.go
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
)

func main() {
	proxyToken := os.Getenv("RAYLEY_TOKEN")
	targetURL := "https://example.com/"
	if len(os.Args) > 1 {
		targetURL = os.Args[1]
	}

	if proxyToken == "" {
		fmt.Fprintln(os.Stderr, "Set RAYLEY_TOKEN to your Proxy Token.")
		os.Exit(1)
	}

	proxyURL, err := url.Parse(fmt.Sprintf("https://token:%s@proxy.rayley.com", proxyToken))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Invalid proxy URL: %v\n", err)
		os.Exit(1)
	}

	client := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		},
	}

	resp, err := client.Get(targetURL)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Request failed: %v\n", err)
		os.Exit(1)
	}
	defer resp.Body.Close()

	if resp.StatusCode == 407 {
		fmt.Fprintln(os.Stderr, "Authentication failed. Check your RAYLEY_TOKEN.")
		os.Exit(1)
	}

	body, _ := io.ReadAll(resp.Body)
	fmt.Printf("Status: %d\n", resp.StatusCode)
	fmt.Println(string(body))
}

Run it:

RAYLEY_TOKEN=rpt_your_token go run example.go https://example.com/

On this page