Skip to main content

Installation

npm install classer-ai

Quick Start

import { classify } from "classer-ai";

// Uses CLASSER_API_KEY environment variable
const result = await classify({
  text: "I can't log in",
  labels: ["billing", "technical", "sales"]
});

console.log(result.label);      // "technical"
console.log(result.confidence); // 0.94

Configuration

export CLASSER_API_KEY=your-api-key

Client instance

import { ClasserClient } from "classer-ai";

const client = new ClasserClient({
  apiKey: "your-api-key",
  baseUrl: "https://api.classer.ai"  // optional
});

Methods

classify()

Single-label classification — returns the one best-matching label.
const result = await client.classify({
  text: "I can't log in to my account",
  labels: ["billing", "technical", "sales"],
  descriptions: {                             // optional
    billing: "Payment or invoice issues",
    technical: "Login, bugs, errors",
    sales: "Pricing or plan questions",
  },
  model: "qwen",                              // optional (default: "qwen")
});

result.label        // "technical"
result.confidence   // 0.94
result.tokens       // 142
result.latency_ms   // 87
result.cached       // false
You can also reference a saved classifier instead of inline labels:
const result = await client.classify({
  text: "I can't log in",
  classifier: "support-tickets",       // or "support-tickets@v2"
});

tag()

Multi-label tagging — returns all labels that exceed the confidence threshold.
const result = await client.tag({
  text: "Breaking: Tech stocks surge amid AI boom",
  labels: ["politics", "technology", "finance", "sports"],
  threshold: 0.3,                              // optional (default: 0.3)
  descriptions: { technology: "Tech industry news" },  // optional
  model: "qwen",                               // optional
});

for (const t of result.labels) {
  console.log(`${t.label}: ${t.confidence}`);
}
// technology: 0.92
// finance: 0.78

result.tokens       // 200
result.latency_ms   // 203
result.cached       // false

Module-level functions

Both methods are also available as top-level convenience functions that use a default client (reads CLASSER_API_KEY from the environment):
import { classify, tag } from "classer-ai";

const result = await classify({ text: "...", labels: ["a", "b"] });
const tagged = await tag({ text: "...", labels: ["a", "b"], threshold: 0.5 });

Error Handling

import { ClasserClient, ClasserError } from "classer-ai";

const client = new ClasserClient({ apiKey: "your-key" });

try {
  const result = await client.classify({
    text: "Hello",
    labels: ["greeting", "question"]
  });
} catch (error) {
  if (error instanceof ClasserError) {
    console.log(`Error: ${error.message}`);
    console.log(`Status: ${error.status}`);    // HTTP status code
    console.log(`Detail: ${error.detail}`);    // Error detail from the API
  }
}
StatusMeaning
400Bad request — invalid parameters
401Unauthorized — missing or invalid API key
402Insufficient balance
422Validation error
429Rate limit exceeded

Types

ClassifyRequest

interface ClassifyRequest {
  text: string;
  labels?: string[];
  classifier?: string;
  descriptions?: Record<string, string>;
  model?: string;
}

ClassifyResponse

interface ClassifyResponse {
  label?: string;
  confidence?: number;
  tokens: number;
  latency_ms: number;
  cached: boolean;
}

TagRequest

interface TagRequest {
  text: string;
  labels?: string[];
  classifier?: string;
  descriptions?: Record<string, string>;
  model?: string;
  threshold?: number;  // default: 0.3
}

TagLabel

interface TagLabel {
  label: string;
  confidence: number;
}

TagResponse

interface TagResponse {
  labels: TagLabel[];  // empty array when nothing matches
  tokens: number;
  latency_ms: number;
  cached: boolean;
}

ClasserError

class ClasserError extends Error {
  status?: number;
  detail?: string;
}

Requirements

  • Node.js 18+ or modern browser with fetch support