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 upload method takes a file path and returns a file ID that you can use in your video generation request.
async function uploadImage(filePath: string) {
try {
const response = await client.files.upload(filePath);
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.
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.generate({
prompt: prompt,
image: imageId,
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.
async function generateVideoSync(prompt: string, imageId: string) {
try {
const response = await client.videos.generate({
prompt: prompt,
image: imageId,
is_async: false,
});
if (response.status === 'SUCCEEDED' && response.data.length > 0) {
console.log('Video generated successfully!');
console.log('Video URL:', response.data[0].url);
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.
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...');
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);
}
}