aily API from any Node.js application using the native fetch API or axios. Includes TypeScript types and async/await patterns.">

Using classifaily with Node.js

Call the classifaily API from any Node.js application using the native fetch API or axios. Includes TypeScript types and async/await patterns.

Setup

Store your key in an environment variable. Use .env with dotenv for local development - never commit keys to source control.

# .env
CLASSIFAILY_KEY=cai_live_your_key_here

Using native fetch (Node 18+)

// classify.js
const API_KEY = process.env.CLASSIFAILY_KEY;
const BASE_URL = 'https://api.classifaily.com/v1';

async function classify(input, categories, options = {}) {
  const response = await fetch(`${BASE_URL}/classify`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ input, categories, ...options }),
  });

  if (!response.ok) {
    const err = await response.json().catch(() => ({}));
    throw new Error(`classifaily ${response.status}: ${err.error ?? 'unknown'}`);
  }

  const data = await response.json();
  return data.result;
}

// Usage
const result = await classify(
  'My invoice shows a double charge this month.',
  ['billing', 'technical_bug', 'account_access', 'general']
);

console.log(result.label);       // 'billing'
console.log(result.confidence);  // 0.97

TypeScript types

interface ClassifyResult {
  label: string;
  confidence: number;
  explanation?: string;
}

interface ClassifyOptions {
  explain?: boolean;
  mode?: 'single' | 'multi' | 'top_n';
  top_n?: number;
  threshold?: number;
  schema?: number;
}

async function classify(
  input: string,
  categories: string[],
  options: ClassifyOptions = {}
): Promise<ClassifyResult> {
  const response = await fetch('https://api.classifaily.com/v1/classify', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.CLASSIFAILY_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ input, categories, ...options }),
  });

  if (!response.ok) throw new Error(`API error: ${response.status}`);
  const data = await response.json();
  return data.result as ClassifyResult;
}

Using axios

npm install axios
import axios from 'axios';

const client = axios.create({
  baseURL: 'https://api.classifaily.com/v1',
  headers: {
    'Authorization': `Bearer ${process.env.CLASSIFAILY_KEY}`,
    'Content-Type': 'application/json',
  },
  timeout: 10_000,
});

const { data } = await client.post('/classify', {
  input: 'Cannot log in after password reset.',
  categories: ['billing', 'technical_bug', 'account_access', 'general'],
});

console.log(data.result.label);

Batch classification

async function batchClassify(items, categories) {
  const response = await fetch('https://api.classifaily.com/v1/batch', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.CLASSIFAILY_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ items, categories }),
  });

  const data = await response.json();
  return data.results;
}

const results = await batchClassify(
  [
    { id: 'q1', input: 'Cannot log in after password reset.' },
    { id: 'q2', input: 'Refund request for last month.' },
    { id: 'q3', input: 'Please add dark mode.' },
  ],
  ['billing', 'technical_bug', 'account_access', 'feature_request']
);

results.forEach(r => console.log(`${r.id}: ${r.result.label}`));
Ready to integrate?

See the full API reference for all parameters and response schemas.

API Reference