SUTRA-V2 with NodeJs SDK

This guide demonstrates how to use the SUTRA-V2 model with the NodeJs SDK to build multilingual AI applications. SUTRA-V2, developed by TWO AI, powers chat and instruction tasks via an OpenAI-compatible API.

🔧 Prerequisites

  • Obtain your SUTRA API key from https://developer.two.ai.
  • Store the API key securely (e.g., in environment variables or a secure configuration system).

📦 Step 1: Install Dependencies

# SUTRA models are OpenAI API compatible
npm install openai
# or
yarn add openai

🔐 Step 2: Initialize SUTRA Client

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.SUTRA_API_KEY, // Use environment variables for security
  baseURL: "https://api.two.ai/v2",
});

💬 Step 3: Basic Chat Completion

async function generateResponse() {
  const response = await client.chat.completions.create({
    model: "sutra-v2",
    messages: [
      {
        role: "system",
        content: "You are a helpful AI that answers concisely.",
      },
      { role: "user", content: "Explain AI in 3 sentences in Spanish." },
    ],
    max_tokens: 1024,
    temperature: 0.7,
  });

  console.log(response.choices[0].message.content);
}

generateResponse();

🌐 Browser Usage

Warning:

The following example is for local testing or internal demos only. Do not use this approach in production. Your API key will be visible and can be stolen.

For browser environments, you'll need to handle CORS and API key security:

// IMPORTANT: Never expose your API key in client-side code
// Use a backend proxy or service to make API calls

import OpenAI from "openai";

// This should be handled by your backend
const client = new OpenAI({
  apiKey: "YOUR_SUTRA_API_KEY", // This should come from your backend
  baseURL: "https://api.two.ai/v2",
  dangerouslyAllowBrowser: true, // Only use this flag with proper security measures
});

// Rest of the code is similar to the Node.js example

🛠 Troubleshooting

  • Invalid API Key: Ensure your API key is correct and stored securely.
  • Model Not Found: Use sutra-v2. SUTRA-V1 was deprecated on March 22, 2025.
  • Rate Limits: Reduce request frequency or contact TWO AI at support@two.ai.
  • CORS Issues: If using in browser, ensure you're using a backend proxy to make API calls.

📎 Resources