Skip to content

anyfast-ai/anyfast-node

Repository files navigation

AnyFast Node.js SDK

AnyFast Node.js SDK for AI services including chat completions, image generation, video generation and asset management.

Installation

npm install anyfast

Configuration

import { AnyFast } from 'anyfast';

const client = new AnyFast({
  apiKey: 'your-api-key',
  baseURL: 'https://www.anyfast.ai',  // optional, this is the default
});

Or use the ANYFAST_API_KEY environment variable:

const client = new AnyFast();

Options

Option Default Description
apiKey process.env.ANYFAST_API_KEY API key for authentication
baseURL https://www.anyfast.ai API base URL
gatewayURL https://www.anyfast.ai Gateway URL for file upload
timeout 30000 Request timeout in milliseconds

Chat Completions

OpenAI-compatible (/v1/chat/completions)

Works with GPT, Claude, Doubao, DeepSeek, Qwen, Grok, Gemini (compat), MiniMax, Kimi, etc.

const result = await client.chat.completions({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(result.choices[0].message.content);

Streaming

const stream = await client.chat.completions({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Anthropic Messages (/v1/messages)

const result = await client.chat.messages({
  model: 'claude-sonnet-4-20250514',
  messages: [{ role: 'user', content: 'Hello!' }],
  max_tokens: 1024,
});
console.log(result.content[0].text);

Doubao Responses (/v1/responses)

const result = await client.chat.responses({
  model: 'doubao-seed-2.0-pro',
  input: 'Hello!',
});
console.log(result.output[0].content?.[0].text);

Supports multimodal input:

const result = await client.chat.responses({
  model: 'doubao-seed-1-6-vision-250815',
  input: [
    {
      role: 'user',
      content: [
        { type: 'input_text', text: 'What is in this image?' },
        { type: 'input_image', image_url: 'https://example.com/photo.png' },
      ],
    },
  ],
});

Gemini Native (/v1beta/models/{model}:generateContent)

const result = await client.chat.gemini('gemini-2.0-flash', {
  contents: [
    { role: 'user', parts: [{ text: 'Hello!' }] },
  ],
});
console.log(result.candidates[0].content.parts[0].text);

Image Generation

Synchronous

const result = await client.image.generate({
  model: 'flux-2-pro',
  prompt: 'A beautiful sunset over the ocean',
  size: '1024x1024',
});
console.log(result.data[0]?.url);

Async with Polling

const response = await client.image.generateAsync({
  model: 'doubao-seedream-5-0-260128',
  prompt: 'A beautiful sunset over the ocean',
});

let status = await client.image.queryTask(response.taskId!);
while (status.isProcessing()) {
  await new Promise((r) => setTimeout(r, 2000));
  status = await client.image.queryTask(response.taskId!);
}

Blocking Run

const status = await client.image.run(
  { model: 'flux-2-pro', prompt: 'A beautiful sunset' },
  { pollIntervalMs: 3000, timeoutMs: 180000 }
);

if (status.isCompleted()) {
  console.log(status.result);
}

Video Generation

Seedance

const status = await client.video.run(
  {
    model: 'seedance',
    content: [{ type: 'text', text: 'A cat playing with a ball' }],
    resolution: '720p',
    ratio: '16:9',
    duration: 5,
  },
  { pollIntervalMs: 5000, timeoutMs: 600000 }
);

Image-to-video:

const status = await client.video.run({
  model: 'seedance',
  content: [
    { type: 'text', text: 'Make it come alive' },
    { type: 'image_url', image_url: { url: 'https://example.com/frame.jpg' }, role: 'first_frame' },
  ],
});

Kling

// Text-to-video
const status = await client.video.runKlingText2Video(
  {
    model_name: 'kling-v2-master',
    prompt: 'A sunset over the ocean',
    mode: 'pro',
    duration: '10',
    aspect_ratio: '16:9',
  },
  { pollIntervalMs: 5000 }
);

// Image-to-video
const status = await client.video.runKlingImage2Video({
  model_name: 'kling-v2-master',
  image: 'https://example.com/frame.jpg',
  prompt: 'Make it move',
});

// Multi-image-to-video
const status = await client.video.runKlingMultiImage2Video({
  model_name: 'kling-v1-6',
  image_list: [
    { image: 'https://example.com/1.jpg' },
    { image: 'https://example.com/2.jpg' },
  ],
  prompt: 'Transition between scenes',
});

Manual polling for Kling:

const response = await client.video.klingText2Video({
  model_name: 'kling-v2-master',
  prompt: 'A cat',
});

const status = await client.video.klingQueryTask('text2video', response.taskId!);

Asset Management (ByteDance Volc)

// Create asset group
const group = await client.asset.createGroup({ Name: 'my-group' });

// List groups
const groups = await client.asset.listGroups();

// Upload asset
const asset = await client.asset.createAsset({
  GroupId: group.Id,
  Name: 'reference-image',
  AssetType: 'Image',
  URL: 'https://example.com/image.png',
});

// List assets
const assets = await client.asset.listAssets({
  Filter: { GroupIds: [group.Id] },
});

Error Handling

import { AnyFastError } from 'anyfast';

try {
  const result = await client.chat.completions({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Hello' }],
  });
} catch (error) {
  if (error instanceof AnyFastError) {
    console.error('Status:', error.status);
    console.error('Code:', error.code);
    console.error('Message:', error.message);
  }
}

License

Apache License 2.0

Packages

 
 
 

Contributors