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

Environment variable

export CLASSER_API_KEY=your-api-key

Client instance

import { ClasserClient } from "classer-ai";

const client = new ClasserClient({
  apiKey: "your-api-key",
  timeout: 30  // optional, in seconds
});

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",
  },
  priority: "standard",                        // optional: "standard" (default, <1s) or "fast" (<200ms)
  cache: true,                                // optional: set false to bypass cache
});

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"
});

Image input

// URL
const result = await client.classify({
  image: "https://example.com/photo.jpg",
  labels: ["cat", "dog", "bird"],
});

// Multiple images
const result = await client.classify({
  image: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
  labels: ["cat", "dog", "bird"],
});

// Text + image together
const result = await client.classify({
  text: "What animal is this?",
  image: "https://example.com/photo.jpg",
  labels: ["cat", "dog", "bird"],
});

result.visual_tokens  // image tokens used

File input (PDF, DOCX)

// Also accepts a URL. Can be combined with text and image inputs.
const result = await client.classify({
  file: base64String,
  labels: ["invoice", "receipt", "contract"],
});

result.tokens         // text tokens
result.visual_tokens  // page image tokens

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.5,                              // optional (default: 0.5)
  descriptions: { technology: "Tech industry news" },  // optional
  priority: "standard",                        // optional: "standard" (default, <1s) or "fast" (<200ms)
  cache: true,                                 // optional: set false to bypass cache
});

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
Tag also accepts image and file parameters, same as classify.

classifyBatch()

Classify multiple texts in a single request. Returns results in the same order as input.
const result = await client.classifyBatch({
  texts: ["I can't log in", "What's the pricing?", "My invoice is wrong"],
  labels: ["billing", "technical", "sales"],
});

for (const r of result.results) {
  console.log(`${r.label}: ${r.confidence}`);
}

result.total_tokens  // total across all texts
result.latency_ms    // total request time

tagBatch()

Tag multiple texts in a single request. Returns results in the same order.
const result = await client.tagBatch({
  texts: ["Tech stocks surge", "Election results are in"],
  labels: ["politics", "technology", "finance"],
  threshold: 0.5,
});

for (const r of result.results) {
  for (const t of r.labels ?? []) {
    console.log(`${t.label}: ${t.confidence}`);
  }
}

Module-level functions

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

const result = await classify({ text: "...", labels: ["a", "b"] });
const tagged = await tag({ text: "...", labels: ["a", "b"], threshold: 0.5 });
const batch = await classifyBatch({ texts: ["x", "y"], labels: ["a", "b"] });
const taggedBatch = await tagBatch({ texts: ["x", "y"], labels: ["a", "b"] });

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>;
  priority?: "fast" | "standard";
  cache?: boolean;
  image?: string | string[];
  file?: string;
}

ClassifyResponse

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

TagRequest

interface TagRequest {
  text?: string;
  labels?: string[];
  classifier?: string;
  descriptions?: Record<string, string>;
  threshold?: number;  // default: 0.5
  priority?: "fast" | "standard";
  cache?: boolean;
  image?: string | string[];
  file?: string;
}

TagLabel

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

TagResponse

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

BatchClassifyRequest

interface BatchClassifyRequest {
  texts: string[];
  labels?: string[];
  classifier?: string;
  descriptions?: Record<string, string>;
  cache?: boolean;
  image?: string | string[];
  file?: string;
}

BatchClassifyResponse

interface BatchClassifyResponse {
  results: BatchClassifyResult[];
  total_tokens: number;
  latency_ms: number;
}

BatchClassifyResult

interface BatchClassifyResult {
  label?: string;
  confidence?: number;
  tokens: number;
  visual_tokens?: number;
  error?: string;
  cached: boolean;
}

BatchTagRequest

interface BatchTagRequest {
  texts: string[];
  labels?: string[];
  classifier?: string;
  descriptions?: Record<string, string>;
  threshold?: number;
  cache?: boolean;
  image?: string | string[];
  file?: string;
}

BatchTagResponse

interface BatchTagResponse {
  results: BatchTagResult[];
  total_tokens: number;
  latency_ms: number;
}

BatchTagResult

interface BatchTagResult {
  labels?: TagLabel[];
  tokens: number;
  visual_tokens?: number;
  error?: string;
  cached: boolean;
}

ClasserError

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

Requirements

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