Package Exports
- @moonmath-ai/client
- @moonmath-ai/client/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@moonmath-ai/client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
MoonMath AI Client
The official Node.js client for interacting with the MoonMath AI API.
Installation
You can install the client using npm:
npm install @moonmath-ai/clientUsage
First, import and initialize the client.
import { MoonMathClient } from '@moonmath-ai/client';
const client = new MoonMathClient();For development or testing, you can provide a custom baseURL to connect to a local or staging environment.
import { MoonMathClient } from '@moonmath-ai/client';
// Example for connecting to a local server
const localClient = new MoonMathClient('http://localhost:8000');Uploading Files
To generate a video, you first need to upload an image file. The create method takes a file path or a public URL and returns a file ID that you can use in your video generation request.
async function uploadImage(filePathOrUrl: string) {
try {
const response = await client.files.create(filePathOrUrl);
console.log('File uploaded successfully. File ID:', response.id);
return response.id;
} catch (error) {
console.error('File upload failed:', error);
}
}
// Example:
// const imageId = await uploadImage('./path/to/your/image.png');Generating a Video
Once you have an image ID, you can submit a job to generate a video. The images parameter for client.videos.create is an array of objects, where each object specifies an image and its properties. The url can be either an ID of a previously uploaded file (using client.files.create) or a public URL to an image.
Asynchronous Generation
For long-running jobs, it's best to use the asynchronous mode. The API will immediately return a job ID.
async function generateVideoAsync(prompt: string, imageId: string) {
try {
const response = await client.videos.create({
prompt: prompt,
images: [{ url: imageId, start_frame_num: 0 }],
is_async: true,
});
console.log('Async job submitted. Job ID:', response.id);
return response.id;
} catch (error) {
console.error('Video generation failed:', error);
}
}Synchronous Generation
For quick tests, you can use the synchronous mode. The request will hang until the video is generated and then return the final result, which includes the video data as a base64-encoded string.
import { JobStatus } from '@moonmath-ai/client';
import fs from 'fs';
async function generateVideoSync(prompt: string, imageId: string) {
try {
const response = await client.videos.create({
prompt: prompt,
images: [{ url: imageId, start_frame_num: 0 }],
is_async: false,
});
if (response.status === JobStatus.SUCCEEDED && response.data?.[0]?.b64_json) {
console.log('Video generated successfully!');
// Decode the base64 string and save it to a file
const videoData = Buffer.from(response.data[0].b64_json, 'base64');
fs.writeFileSync('generated-video.mp4', videoData);
console.log('Video saved as generated-video.mp4');
return response;
} else {
console.error('Sync generation failed:', response.error);
}
} catch (error) {
console.error('Video generation failed:', error);
}
}Retrieving a Video
If you submitted an asynchronous job, you can check its status and retrieve the result using the job ID. When the job status is SUCCEEDED, the response from client.videos.retrieve(jobId) will contain a public URL to download the video.
The client.videos.retrieveContent(jobId) method is a convenient way to download the video file's raw data.
import { JobStatus } from '@moonmath-ai/client';
import fs from 'fs';
async function retrieveVideo(jobId: string) {
try {
const statusResponse = await client.videos.retrieve(jobId);
if (statusResponse.status === JobStatus.SUCCEEDED) {
console.log('Job succeeded! Downloading video...');
// The `retrieve` response contains the public URL which can be used to download the resulting video.
console.log(`Video URL: ${statusResponse.data[0]?.url}`);
// Alternatively, once the job status is SUCCEEDED, retreiveContent can be used with the same jobId
const videoData = await client.videos.retrieveContent(jobId);
// Save the video to a file
fs.writeFileSync(`${jobId}.mp4`, Buffer.from(videoData));
console.log(`Video saved as ${jobId}.mp4`);
} else if (statusResponse.status === JobStatus.FAILED) {
console.error('Job failed:', statusResponse.error);
} else {
console.log(`Job status: ${statusResponse.status}. Checking again in 10 seconds...`);
// Implement polling logic here if needed
}
} catch (error) {
console.error('Failed to retrieve video:', error);
}
}