// Addrly Node.js SDK // https://addrly.app/documentation // ============================================================================ // Types // ============================================================================ export interface AddrlyConfig { /** API key (starts with sk_) */ apiKey: string; /** Base URL override (default: https://api.addrly.app) */ baseUrl?: string; /** Request timeout in ms (default: 30000) */ timeout?: number; } export interface EmailValidationResult { status: number; email: string; normalized_email: string; domain: string; domain_age_in_days: number | null; mx: boolean; mx_records: MXRecord[]; disposable: boolean; spam: boolean; public_domain: boolean; relay_domain: boolean; role_account: boolean; did_you_mean: string | null; blocklisted?: boolean; velocity_exceeded?: boolean; environment?: string; } export interface DomainValidationResult { status: number; domain: string; domain_age_in_days: number | null; mx: boolean; mx_records: MXRecord[]; disposable: boolean; spam: boolean; public_domain: boolean; relay_domain: boolean; blocklisted?: boolean; environment?: string; } export interface MXRecord { hostname: string; priority: number; } export interface BulkEmailResult { status: number; results: EmailValidationResult[]; summary: { total: number; valid: number; invalid: number; disposable: number; role_accounts: number; }; environment?: string; } export interface BulkDomainResult { status: number; results: DomainValidationResult[]; summary: { total: number; valid: number; invalid: number; disposable: number; }; environment?: string; } export interface GateDecisionResult { status: number; decision: { action: 'allow' | 'block' | 'challenge'; matched_rule: { id: number; name: string; message: string | null; } | null; }; signals: Record; geo?: Record; durationMs: number; requestId: string; } export interface AddrlyError { status: number; error: string; message?: string; } // ============================================================================ // SDK Client // ============================================================================ export class Addrly { private apiKey: string; private baseUrl: string; private timeout: number; constructor(apiKey: string); constructor(config: AddrlyConfig); constructor(configOrKey: string | AddrlyConfig) { if (typeof configOrKey === 'string') { this.apiKey = configOrKey; this.baseUrl = 'https://api.addrly.app'; this.timeout = 30000; } else { this.apiKey = configOrKey.apiKey; this.baseUrl = (configOrKey.baseUrl || 'https://api.addrly.app').replace(/\/+$/, ''); this.timeout = configOrKey.timeout || 30000; } if (!this.apiKey) { throw new Error('Addrly: API key is required'); } } // ---------- Email Validation ---------- /** Validate a single email address */ async validateEmail(email: string, options?: { environment?: string }): Promise { return this.post('/api/v1/email', { email, ...options }); } /** Validate a single domain */ async validateDomain(domain: string, options?: { environment?: string }): Promise { return this.get(`/api/v1/domain/${encodeURIComponent(domain)}`, options); } // ---------- Bulk Validation ---------- /** Validate multiple emails (Pro: max 500, Ultra: max 1000) */ async bulkValidateEmails(emails: string[], options?: { environment?: string }): Promise { return this.post('/api/v1/bulk-email', { emails, ...options }); } /** Validate multiple domains (Pro: max 500, Ultra: max 1000) */ async bulkValidateDomains(domains: string[], options?: { environment?: string }): Promise { return this.post('/api/v1/bulk-domain', { domains, ...options }); } // ---------- Gates ---------- /** Evaluate an email or domain against a gate */ async gate(gateId: string, input: { email?: string; domain?: string; ip?: string }): Promise { return this.post(`/api/v1/gates/${encodeURIComponent(gateId)}/decide`, input); } // ---------- Shorthand ---------- /** Shorthand: validate email or domain (auto-detects) */ async validate(input: string, options?: { environment?: string }): Promise { if (input.includes('@')) { return this.validateEmail(input, options); } return this.validateDomain(input, options); } // ---------- HTTP ---------- private async request(method: string, path: string, body?: any, query?: Record): Promise { const url = new URL(path, this.baseUrl); if (query) { for (const [k, v] of Object.entries(query)) { if (v !== undefined) url.searchParams.set(k, v); } } const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.timeout); try { const res = await fetch(url.toString(), { method, headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey, }, body: body ? JSON.stringify(body) : undefined, signal: controller.signal, }); clearTimeout(timeoutId); const data = await res.json(); if (!res.ok) { const err = new Error(data.error || `HTTP ${res.status}`) as any; err.status = res.status; err.response = data; throw err; } return data as T; } catch (err: any) { clearTimeout(timeoutId); if (err.name === 'AbortError') { const timeoutErr = new Error('Request timed out') as any; timeoutErr.status = 408; throw timeoutErr; } throw err; } } private post(path: string, body: any): Promise { return this.request('POST', path, body); } private get(path: string, query?: Record): Promise { const q = query ? Object.fromEntries(Object.entries(query).map(([k, v]) => [k, String(v)])) : undefined; return this.request('GET', path, undefined, q); } } export default Addrly;