Skip to content

Latest commit

 

History

History
194 lines (151 loc) · 4.35 KB

File metadata and controls

194 lines (151 loc) · 4.35 KB

MiniMax SDK

TypeScript SDK for the MiniMax AI platform.

Installation

npm install mmx-cli

Quick Start

import { MiniMaxSDK } from 'mmx-cli/sdk';

const sdk = new MiniMaxSDK({
  apiKey: 'sk-xxxxx',
  region: 'global', // or 'cn'
});

You can also omit apiKey if it's already configured via mmx config set api-key <key>.

Modules

Text

const response = await sdk.text.chat({
  model: 'MiniMax-M3',
  messages: [{ role: 'user', content: 'Hello!' }],
  max_tokens: 4096,
});

// Streaming
const stream = await sdk.text.chat({
  model: 'MiniMax-M3',
  messages: [{ role: 'user', content: 'Write a poem' }],
  stream: true,
});

for await (const event of stream) {
  console.log(event.choices[0]?.delta?.content);
}

Image

const result = await sdk.image.generate({
  model: 'image-01',
  prompt: 'A cat in a spacesuit',
  width: 1024,
  height: 1024,
  n: 1,
});

Video

// Synchronous — waits for completion
const video = await sdk.video.generate({
  model: 'MiniMax-Hailuo-2.3',
  prompt: 'Ocean waves at sunset',
});

// MiniMax-H3 — Video Generation V2 with request defaults
const h3Video = await sdk.video.generate({
  model: 'MiniMax-H3',
  prompt: 'Ocean waves at sunset',
});

// Asynchronous — returns task ID immediately
const { taskId } = await sdk.video.generate({
  prompt: 'A robot painting',
  async: true,
});

const { taskId: h3TaskId } = await sdk.video.generate({
  model: 'MiniMax-H3',
  prompt: 'Ocean waves at sunset',
  async: true,
});

const h3Task = await sdk.video.getTask({
  taskId: h3TaskId,
  model: 'MiniMax-H3',
});

const task = await sdk.video.getTask({ taskId });

// Download
const { size, save, downloadUrl } = await sdk.video.download({
  fileId: '176844028768320',
  outPath: './video.mp4',
});

Speech

const speech = await sdk.speech.synthesize({
  model: 'speech-2.8-hd',
  text: 'Hello, world!',
  voice_setting: { voice_id: 'English_expressive_narrator' },
  audio_setting: { format: 'mp3', sample_rate: 32000, bitrate: 128000, channel: 1 },
});

// Streaming
const stream = await sdk.speech.synthesize({
  text: 'Stream me',
  stream: true,
});

for await (const chunk of stream) {
  // process audio chunks
}

// List voices
const voices = await sdk.speech.voices();
const englishVoices = await sdk.speech.voices('en');

// Speech-to-text
const transcript = await sdk.speech.transcribe({
  file: './meeting.mp3',
  language: 'zh',
});
console.log(transcript.text, transcript.duration);

// Speech-to-text, streamed
const deltas = await sdk.speech.transcribe({ file: './meeting.mp3', stream: true });
for await (const event of deltas) {
  process.stdout.write(event.delta); // concatenate delta values in `index` order
}
// The generator ends at the API's final event (finish: true) and releases the
// connection, so breaking out of the loop early is safe. Unlike the CLI, which
// warns and continues, the SDK throws SDKError on a malformed stream chunk.
// Non-SSE responses and streams ending without finish: true also throw SDKError
// during iteration; text received before an error may be incomplete.

Vision

const result = await sdk.vision.describe({
  image: 'https://example.com/photo.jpg',
  prompt: 'What breed is this dog?',
});

console.log(result.content);

Search

const results = await sdk.search.query('MiniMax AI latest news');

for (const item of results.organic) {
  console.log(item.title, item.link, item.snippet);
}

The underlying /v1/coding_plan/search API returns at most 10 results per call and does not currently expose a pagination parameter (see #107). Refine your query if you need different results.

Quota

import {
  isAccountBalanceResponse,
  isQuotaResponse,
} from 'mmx-cli/sdk';

const quota = await sdk.quota.info();

if (isQuotaResponse(quota)) {
  console.log(quota.model_remains);
} else if (isAccountBalanceResponse(quota)) {
  console.log(quota.available_amount);
}

Token Plan credentials and OAuth return QuotaResponse. API secret keys with the sk-api- prefix return AccountBalanceResponse. The type guards above narrow the QuotaInfoResponse union without changing the raw API response.

Custom Base URL

const sdk = new MiniMaxSDK({
  apiKey: 'sk-xxxxx',
  baseUrl: 'https://api.minimax.io',
});