Skip to main content

Installation

npm install @veri/sdk
Requires Node.js 18 or later.

Quick Start

import { VeriClient } from '@veri/sdk';

const client = new VeriClient({ apiKey: 'your-api-key' });

const result = await client.detect(imageBuffer);
console.log(`Prediction: ${result.prediction}`);
console.log(`Is fake: ${result.isFake} (${(result.confidence * 100).toFixed(1)}%)`);

Client Options

import { VeriClient } from '@veri/sdk';

const client = new VeriClient({
  apiKey: 'your-api-key',
  baseUrl: 'https://api.tryveri.com/v1',  // Custom base URL
  timeout: 30000,  // Request timeout in ms
  maxRetries: 3,   // Number of retry attempts
});

Detection Methods

detect(image, options?)

Detect whether an image is AI-generated.
// From Buffer
const result = await client.detect(imageBuffer);

// From base64 string
const result = await client.detect(base64String);

// With options
const result = await client.detect(imageBuffer, {
  models: ['veri_face'],
  threshold: 0.7,
});

detectUrl(url, options?)

Detect an image from a URL.
const result = await client.detectUrl('https://example.com/image.jpg');

getProfile()

Get the authenticated user’s profile.
const profile = await client.getProfile();
console.log(`User ID: ${profile.userId}`);
console.log(`Credits: ${profile.credits}`);

Error Handling

import {
  VeriClient,
  VeriAPIError,
  VeriValidationError,
  VeriRateLimitError,
  VeriTimeoutError,
  VeriInsufficientCreditsError,
} from '@veri/sdk';

const client = new VeriClient({ apiKey: 'your-api-key' });

try {
  const result = await client.detect(image);
} catch (error) {
  if (error instanceof VeriValidationError) {
    console.log(`Invalid input: ${error.message} (field: ${error.field})`);
  } else if (error instanceof VeriRateLimitError) {
    console.log(`Rate limited. Retry after ${error.retryAfter} seconds`);
  } else if (error instanceof VeriInsufficientCreditsError) {
    console.log('Not enough credits');
  } else if (error instanceof VeriTimeoutError) {
    console.log(`Request timed out after ${error.timeoutMs}ms`);
  } else if (error instanceof VeriAPIError) {
    console.log(`API error: ${error.message} (code: ${error.code})`);
  }
}

Type Definitions

DetectionResult

interface DetectionResult {
  prediction: string;                         // "ai", "real", or "uncertain"
  confidence: number;                         // Overall confidence (0-1)
  ensemble_score: number | null;              // Weighted ensemble score
  verdict: string;                            // "ai_generated", "uncertain", or "likely_real"
  models_succeeded: number;                   // Models that returned results
  models_total: number;                       // Total models invoked
  model_results: Record<string, ModelResult>; // Per-model results
  detection_latency_ms: number;               // Detection latency in ms
  isFake: boolean | null;                     // true=AI, false=real, null=uncertain
  processingTimeMs: number;                   // Total processing time in ms
  imageHash: string;                          // SHA-256 hash of image
  cached: boolean;                            // Whether result was cached
  timestamp: string;                          // ISO 8601 timestamp
}

DetectionOptions

interface DetectionOptions {
  models?: string[];      // Specific models to use
  threshold?: number;     // Classification threshold (default 0.5)
}

Using with Frameworks

Next.js

// app/api/detect/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { VeriClient } from '@veri/sdk';

const client = new VeriClient({ apiKey: process.env.VERI_API_KEY! });

export async function POST(request: NextRequest) {
  const formData = await request.formData();
  const file = formData.get('image') as File;
  const buffer = Buffer.from(await file.arrayBuffer());

  const result = await client.detect(buffer);
  return NextResponse.json(result);
}

Express

import express from 'express';
import multer from 'multer';
import { VeriClient } from '@veri/sdk';

const app = express();
const upload = multer();
const client = new VeriClient({ apiKey: process.env.VERI_API_KEY! });

app.post('/detect', upload.single('image'), async (req, res) => {
  const result = await client.detect(req.file!.buffer);
  res.json(result);
});