Skip to main content
In this guide, we will build a simple Node.js script that uses Nuabase to classify customer feedback.

Prerequisites

Step 1: Install the SDK

Install the nuabase package and zod for schema definition.
npm install nuabase zod

Step 2: Get your API Key

  1. Log in to the Nuabase Console.
  2. Navigate to API Keys.
  3. Create a new key and copy it.

Step 3: Write your first function

Create a file named index.ts (or index.js):
import { Nua } from 'nuabase';
import { z } from 'zod';

// Initialize with your API Key
const nua = new Nua({ apiKey: 'YOUR_NUABASE_API_KEY' });

// Define the output schema
const SentimentSchema = z.object({
  sentiment: z.enum(['Positive', 'Neutral', 'Negative']),
  confidence: z.number().min(0).max(1),
});

// Create the LLM function
const analyzeSentiment = nua.createFn({
  prompt: "Analyze the sentiment of the following text.",
  output: {
    name: "sentimentAnalysis",
    schema: SentimentSchema,
  },
});

async function main() {
  const text = "The product is great, but the shipping was delayed.";
  
  console.log("Analyzing...");
  const result = await analyzeSentiment(text);

  if (result.isSuccess) {
    console.log("Result:", result.data);
  } else {
    console.error("Error:", result.error);
  }
}

main();

Step 4: Run it

Run the script using ts-node or node.
npx ts-node index.ts
You should see an output similar to:
{
  "sentiment": "Positive",
  "confidence": 0.8
}

What just happened?

  1. You defined a Schema using Zod.
  2. You created a Function with a natural language prompt.
  3. Nuabase handled the prompt execution and guaranteed the output matched your schema.

Next Steps

Now that you’ve seen the basics, learn how to integrate this into your actual application.