A JavaScript/TypeScript client for the WaveSpeed AI image generation API. This client is compatible with both JavaScript and TypeScript projects.
npm install wavespeedconst{ WaveSpeed }=require('wavespeed');// Initialize the client with your API keyconstclient=newWaveSpeed('YOUR_API_KEY');// Generate an image and wait for the resultasyncfunctiongenerateImage(){constprediction=awaitclient.run('wavespeed-ai/flux-dev',{prompt: 'A futuristic cityscape with flying cars and neon lights',size: '1024*1024',num_inference_steps: 28,guidance_scale: 5.0,num_images: 1,seed: -1,enable_safety_checker: true});// Print the generated image URLsprediction.outputs.forEach((imgUrl,i)=>{console.log(`Image ${i+1}: ${imgUrl}`);});}generateImage().catch(console.error);importWaveSpeedfrom'wavespeed';// Initialize the client with your API keyconstclient=newWaveSpeed('YOUR_API_KEY');// Generate an image and wait for the resultasyncfunctiongenerateImage(): Promise<void>{constinput: Record<string,any>={prompt: 'A futuristic cityscape with flying cars and neon lights',size: '1024*1024',num_inference_steps: 28,guidance_scale: 5.0,num_images: 1,seed: -1,enable_safety_checker: true};constprediction=awaitclient.run('wavespeed-ai/flux-dev',input);// Print the generated image URLsprediction.outputs.forEach((imgUrl,i)=>{console.log(`Image ${i+1}: ${imgUrl}`);});}generateImage().catch(console.error);If you need more control over the polling process, you can use the create method and manually poll for status updates:
importWaveSpeedfrom'wavespeed';// Initialize the client with your API keyconstclient=newWaveSpeed('YOUR_API_KEY');asyncfunctiongenerateWithManualPolling(): Promise<void>{// Create a prediction without waitingconstprediction=awaitclient.create('wavespeed-ai/flux-dev',{prompt: 'A beautiful mountain landscape at sunset',size: '1024*1024',num_inference_steps: 28,guidance_scale: 5.0,num_images: 1});console.log(`Prediction created with ID: ${prediction.id}`);console.log(`Initial status: ${prediction.status}`);// Manually poll for status updatesletcurrentPrediction=prediction;while(currentPrediction.status!=='completed'&¤tPrediction.status!=='failed'){console.log('Prediction still processing, checking again in 2 seconds...');awaitnewPromise(resolve=>setTimeout(resolve,2000));// Reload the prediction to get the latest statuscurrentPrediction=awaitcurrentPrediction.reload();console.log(`Updated status: ${currentPrediction.status}`);}if(currentPrediction.status==='completed'){console.log('Prediction completed successfully!');currentPrediction.outputs.forEach((imgUrl,i)=>{console.log(`Image ${i+1}: ${imgUrl}`);});}else{console.error(`Prediction failed: ${currentPrediction.error}`);}}generateWithManualPolling().catch(console.error);newWaveSpeed(apiKey?: string,options?: {baseUrl?: string,pollInterval?: number,timeout?: number
})apiKey(string): Your WaveSpeed API keyoptions(object, optional):baseUrl(string): API base URL (default: 'https://api.wavespeed.ai/api/v2/')pollInterval(number): Interval in seconds for polling prediction status (default: 1)timeout(number): Timeout in seconds for API requests (default: 60)
run(modelId: string,input: Record<string,any>,options?: RequestOptions): Promise<Prediction>Generate an image and wait for the result.
create(modelId: string,input: Record<string,any>,options?: RequestOptions): Promise<Prediction>Create a prediction without waiting for it to complete.
The Prediction object contains information about an image generation job:
prediction.id// Unique ID of the predictionprediction.model// Model ID used for the predictionprediction.status// Status of the prediction (processing, completed, failed)prediction.input// Input parameters used for the predictionprediction.outputs// List of output image URLsprediction.urls.get// URL to get the prediction statusprediction.has_nsfw_contents// List of booleans indicating if each image has NSFW contentprediction.created_at// Creation timestampprediction.error// Error message (if any)prediction.executionTime// Time taken to execute the prediction in millisecondsprediction.wait(): Promise<Prediction>// Wait for the prediction to completeprediction.reload(): Promise<Prediction>// Reload the prediction statusWAVESPEED_API_KEY: Your WaveSpeed API keyWAVESPEED_POLL_INTERVAL: Interval in seconds for polling prediction status (default: 1)WAVESPEED_TIMEOUT: Timeout in seconds for API requests (default: 60)
MIT