To use Routstr’s API securely and anonymously via Tor, you need to first ensure that you have a Tor client running on your local machine. This will allow you to route your network traffic through the Tor network.

Once your Tor client is running (typically on localhost:9050 for SOCKS5 proxy), you can configure your API client to use Routstr’s onion service endpoint and route requests through your local Tor proxy.

Here’s an example using the Python OpenAI client:

import os
import openai
import httpx

client = openai.OpenAI( # type: ignore
    api_key=os.environ["CASHU_TOKEN"],
    base_url="http://mcfftxiqtpwvrsly4ue6fgc6nvo637emrofmnftitn3frmf55frcleid.onion/v1",
    http_client=httpx.Client(
        proxies={"http://": "socks5://localhost:9050"} # to use onion proxy (tor)
    )
)

history: list = []

def chat():
    while True:
        user_msg = {"role": "user", "content": input("\nYou: ")}
        history.append(user_msg)
        ai_msg = {"role": "assistant", "content": ""}

        for chunk in client.chat.completions.create(
            model=os.environ.get("MODEL", "openai/gpt-4o-mini"),
            messages=history,
            stream=True,
        ):
            if len(chunk.choices) > 0:
                ai_msg["content"] += chunk.choices[0].delta.content
                print(chunk.choices[0].delta.content, end="", flush=True)
        print()
        history.append(ai_msg)

if __name__ == "__main__":
    chat()

Explanation of the Code

  • import httpx: The httpx library is used as the underlying HTTP client for openai-python. It allows for advanced configurations like proxy settings.
  • base_url: This is set to Routstr’s onion service address (http://mcfftxiqtpwvrsly4ue6fgc6nvo637emrofmnftitn3frmf55frcleid.onion/v1). Ensure you use the correct and current onion address.
  • http_client=httpx.Client(...): An httpx.Client instance is passed to the OpenAI client.
  • proxies={"http://": "socks5://localhost:9050"}: This crucial line configures httpx to route all HTTP traffic through the SOCKS5 proxy running on localhost:9050, which is the default for many Tor clients.

Prerequisites

  1. Install Tor: Download and install the Tor Browser or a standalone Tor client for your operating system. For detailed installation instructions on Linux, refer to this guide.
  2. Run Tor: Ensure your Tor client is running and configured to provide a SOCKS5 proxy on localhost:9050.

By following these steps, you can leverage the anonymity and security benefits of the Tor network when interacting with Routstr’s API.