american-check.png

Details and links

Reference client

cURL against REST API

Check single text for english

curl -X POST \\
  -H "Content-Type: application/json" \\
  -d '{"text": "this is an english sentence"}' \\
  <https://real-trump.fun/language-detection/is-english>

Check multiple texts for english

curl -X POST \\
  -H "Content-Type: application/json" \\
  -d '{"texts": [{"text": "this is an english sentence"}, {"text": "ceci est une phrase en français"}]}' \\
  <https://real-trump.fun/language-detection/is-english-batch>

Torus AgentClient

<aside> ⛔

The AgentServer variation of this is not deployed currently due to the NODE_URL hardcoding bug in the Torus SDK, so the below code will not work against real-trump.fun. This comment will be removed once the AgentServer variation is deployed

The REST API for this service, however, is deployed and available (it’s a hono server not a Torus AgentServer), see the above cURLs for how to interact with it.

</aside>

// This is a reference Torus SDK client for the English language classification service
import { AgentClient, Keypair } from "@torus-network/sdk/agent-client";

const BASE_URL = process.env.BASE_URL || "<http://localhost:3000>";
const TORUS_CLIENT_MNEMONIC = process.env.TORUS_CLIENT_MNEMONIC;

if (!TORUS_CLIENT_MNEMONIC) {
  throw new Error("TORUS_CLIENT_MNEMONIC is not set");
}

class EnglishDetectorClient {
  private client: AgentClient;

  constructor(mnemonic: string, baseUrl: string = "<http://localhost:3000>") {
    const keypair = new Keypair(mnemonic);
    this.client = new AgentClient({ keypair, baseUrl });
  }

  async isEnglish(text: string, defaultOnUndetermined?: boolean) {
    const response = await this.client.call({
      endpoint: "is-english",
      data: { text, defaultOnUndetermined },
    });

    if (response.success) {
      const data = response.data as { isEnglish: boolean };
      return {
        success: true,
        isEnglish: data.isEnglish,
      };
    } else {
      return {
        success: false,
        error: response.error?.message || "Failed to check if text is English",
      };
    }
  }

  async isEnglishBatch(texts: string[], defaultOnUndetermined?: boolean) {
    const response = await this.client.call({
      endpoint: "is-english-batch",
      data: { texts, defaultOnUndetermined },
    });

    if (response.success) {
      const data = response.data as { text: string; isEnglish: boolean }[];
      return {
        success: true,
        results: data,
      };
    } else {
      return {
        success: false,
        error:
          response.error?.message || "Failed to batch check if text is English",
      };
    }
  }
}

Example client usage

async function main() {
  const BASE_URL = process.env.BASE_URL || "<http://localhost:3000>"
  const TORUS_CLIENT_MNEMONIC = process.env.TORUS_CLIENT_MNEMONIC
  if (!TORUS_CLIENT_MNEMONIC) {
    throw new Error(
      "Please set TORUS_CLIENT_MNEMONIC in your .env file to run this example."
    );
  }

  const client = new EnglishDetectorClient(TORUS_CLIENT_MNEMONIC, BASE_URL);

  // Example 1: Check a single English sentence
  console.log('Running: isEnglish("Hello, how are you today?")');
  const response1 = await client.isEnglish("Hello, how are you today?");
  console.log("Response:", response1);
  console.log("---");

  // Example 2: Check a single non-English sentence
  console.log('Running: isEnglish("Bonjour, comment ça va?")');
  const response2 = await client.isEnglish("Bonjour, comment ça va?");
  console.log("Response:", response2);
  console.log("---");

  // Example 3: Batch check multiple sentences
  const texts = [
    "This is an English sentence.",
    "Ceci est une phrase en français.",
    "Torus is a decentralized network.",
    "Wie geht es Ihnen?",
  ];
  console.log(`Running: isEnglishBatch(${JSON.stringify(texts)})`);
  const response3 = await client.isEnglishBatch(texts);
  console.log("Response:", response3);
  console.log("---");

  // Example 4: Using defaultOnUndetermined
  const shortText = "ok";
  console.log(
    `Running: isEnglish("${shortText}", true) - expecting it to default to true`
  );
  const response4 = await client.isEnglish(shortText, true);
  console.log("Response:", response4);
  console.log("---");
}

main().catch((error) => {
  console.error("An error occurred:", error.message);
});