TypeScript & Node.js 22 Guide: Typed Fetch Client & JSON CLI Automation
Run AI Chili across Node.js 22, Next.js Route Handlers, Cloudflare Workers, and Vercel Edge using standard Web Fetch with zero npm bloat, or shell out to aichili generate --json from build scripts.
https://aichili.ai/api/v1 · Streamable HTTP MCP https://aichili.ai/mcp · OpenAPI 3.1 /openapi.json.Architecture & Endpoint Overview
| JS / TS Runtime | Integration Approach | Dependencies | Timeout & State Pattern |
|---|---|---|---|
| Next.js App Router / Edge | Typed fetch client -> enqueue POST /generations, poll from client/cron | 0 npm packages | Return job.id immediately; poll GET /generations/{id} |
| Cloudflare Workers / Queues | Worker submits with Idempotency-Key; Queue consumer polls status | 0 npm packages | Survives Worker CPU limits cleanly |
| Node.js 22 Build / CI Scripts | execFile('aichili', ['generate', '--wait', '--json', ...]) | brew install AI-Chili/tap/aichili | Single command blocks until MP4 output_asset is ready |
4-Step Implementation Blueprint
01 · Copy Zero-Dep Typed Client
Drop the 60-line TypeScript client below into lib/aichili.ts for strict compile-time checking across all 5 model_id values and h3-sol alias.
02 · Quote Before Enqueueing in Route Handlers
Call client.planGeneration(req) first so your Next.js or Worker endpoint can verify user quota or display exact credits.
03 · Return Job ID Immediately on Serverless
Video rendering takes 15s-90s; enqueue POST /api/v1/generations in your serverless handler and poll GET /api/v1/generations/{id} rather than holding an HTTP response open.
04 · Use aichili --json in Local Node Automation
For CLI pipelines, HyperFrames renders, or CI jobs, invoke aichili plan --json and aichili generate --wait --json directly.
1. Zero-Dependency Typed Fetch Wrapper (Next.js / Cloudflare Workers / Node 22)
Uses Web-standard fetch and AbortSignal.timeout with full TypeScript types matching /openapi.json:
export type ChiliModelId =
| 'h3-vdn'
| 'h3-sol'
| 'wan3.0-video'
| 'seedance-2.5'
| 'minimax-h3-gmi'
| 'avatar-talk';
export interface GenerationCreateRequest {
mode: 't2v' | 'i2v';
model_id: ChiliModelId;
workflow?: 'general' | 'reference' | 'photo' | 'talk';
idempotency_key?: string;
params: {
prompt: string;
duration_s?: number;
resolution?: string;
ratio?: '16:9' | '9:16' | '1:1' | '4:3' | '3:4' | '21:9' | 'adaptive';
ref_asset_ids?: string[];
ref_roles?: Array<'reference_image' | 'reference_video' | 'first_frame' | 'last_frame'>;
seed?: number;
};
}
export interface GenerationPlanResponse {
total_credits: number;
total_duration_s: number;
segments: number;
}
export interface GenerationJobResponse {
id: string;
status: 'queued' | 'running' | 'succeeded' | 'failed' | 'canceled';
credits_cost: number;
output_asset?: Record<string, unknown> | null;
error_code?: string | null;
error_message?: string | null;
}
export class AiChiliClient {
constructor(
private readonly apiKey: string,
private readonly baseUrl = 'https://aichili.ai/api/v1',
) {}
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, {
method,
headers: {
'User-Agent': 'curl/8.4.0',
'Authorization': `Bearer ${this.apiKey}`,
'Accept': 'application/json',
...(body !== undefined ? { 'Content-Type': 'application/json' } : {}),
},
body: body !== undefined ? JSON.stringify(body) : undefined,
signal: AbortSignal.timeout(25_000),
});
const data = await res.json();
if (!res.ok) {
throw new Error(`AI Chili API ${res.status}: ${data?.error?.code ?? 'error'} - ${data?.error?.message ?? ''}`);
}
return data as T;
}
planGeneration(req: GenerationCreateRequest): Promise<GenerationPlanResponse> {
return this.request('POST', '/generations/plan', req);
}
createGeneration(req: GenerationCreateRequest): Promise<GenerationJobResponse> {
return this.request('POST', '/generations', req);
}
getGeneration(jobId: string): Promise<GenerationJobResponse> {
return this.request('GET', `/generations/${encodeURIComponent(jobId)}`);
}
}
2. Programmatic CLI JSON Mode in Node.js 22 (child_process.execFile)
Ideal for build scripts and video composition pipelines with npm install -g aichili (or brew install AI-Chili/tap/aichili):
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
async function renderBrollClip(prompt: string, idemKey: string) {
const { stdout: planOut } = await execFileAsync('aichili', [
'plan', '--model', 'h3-vdn', '--mode', 't2v',
'--duration', '15', '--resolution', '768p', '--ratio', '9:16',
'--prompt', prompt, '--json',
]);
const plan = JSON.parse(planOut);
if (plan.total_credits > 20) throw new Error(`Budget exceeded: ${plan.total_credits}`);
const { stdout: jobOut } = await execFileAsync('aichili', [
'generate', '--model', 'h3-vdn', '--mode', 't2v',
'--duration', '15', '--resolution', '768p', '--ratio', '9:16',
'--idempotency-key', idemKey, '--prompt', prompt, '--wait', '--json',
]);
return JSON.parse(jobOut);
}
Recommended AI Chili Engines for TypeScript & Node 22
H3-VDN
Deterministic per-second rate: 0.6 cr/s (480p), 1.0 cr/s (768p), 1.8 cr/s (1080p), 2.5 cr/s (2K), rounded up to whole credits (also accepts alias h3-sol).
Model Spec → seedance-2.5Seedance 2.5
Live pre-calculated quote via POST /v1/generations/plan; reference videos scale proportionally by max(1, 0.6 × (out_s + ref_s) / out_s).
Model Spec → avatar-talkAvatar-Talk
Script-driven billing: 0.2 credits per 5 script units on 'talk' (video loop), plus a +10 credit 768p base-clip charge on 'photo' (portrait still).
Model Spec →Frequently Asked Questions
Can I call https://aichili.ai/api/v1/* directly from browser client code?
Never expose your chili_sk_* secret key in public browser bundles. Call AI Chili from a Next.js Route Handler, Server Action, or Cloudflare Worker so your key stays server-side.
Does the /api/v1 gateway accept an Idempotency-Key HTTP header?
Yes. You can pass idempotency_key in the JSON body or send an Idempotency-Key HTTP header on POST /api/v1/generations, and the gateway bridges it automatically.
Can I generate TypeScript types straight from /openapi.json?
Yes. Run npx openapi-typescript https://aichili.ai/openapi.json -o src/types/aichili.d.ts to generate types directly from our live OpenAPI 3.1 spec.
Explore Other Stack Integrations
Claude Code
Attach AI Chili's remote Streamable HTTP MCP server (https://aichili.ai/mcp) to Claude Code in one command, install…
View Integration → AI IDE & Composer IntegrationCursor IDE
Add https://aichili.ai/mcp (or npx -y @aichili/mcp) to .cursor/mcp.json so Cursor Agent generates promo reels, UI…
View Integration → Hosted MCP & OpenAPI 3.1 Tool CallingOpenAI Agents
Point OpenAI's Responses API Hosted MCP tool directly at https://aichili.ai/mcp with require_approval: 'always' on…
View Integration → Visual Workflow Orchestrationn8n Workflows
Build reliable, budget-guarded n8n video workflows using four standard HTTP Request nodes: inspect GET…
View Integration → Backend Python 3.10+ IntegrationPython (SDK, urllib & httpx)
Install the official zero-dependency Python SDK, CLI & stdio MCP server via pip install…
View Integration →