So verwendest du das WaveSpeedAI JavaScript SDK
Das WaveSpeedAI JavaScript SDK bietet eine einfache Möglichkeit, KI-gesteuerte Bild- und Videogenerierung in deine Node.js- und TypeScript-Anwendungen zu integrieren. Diese Anleitung deckt alles ab, was du zum Einstieg benötigst.
Voraussetzungen
Bevor du anfängst, stelle sicher, dass du folgende Voraussetzungen erfüllst:
- Node.js 18+ auf deinem Computer installiert
- Ein WaveSpeedAI API-Schlüssel von wavespeed.ai/settings/api-keys
Installation
Installiere das SDK mit npm:
npm install wavespeed
Oder mit yarn:
yarn add wavespeed
Authentifizierung einrichten
Das SDK benötigt deinen API-Schlüssel zur Authentifizierung von Anfragen. Du hast zwei Möglichkeiten:
Option 1: Umgebungsvariable (Empfohlen)
Setze die Umgebungsvariable WAVESPEED_API_KEY:
export WAVESPEED_API_KEY="your-api-key-here"
Verwende dann das SDK direkt:
import wavespeed from "wavespeed";
const output = await wavespeed.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" });
Option 2: API-Schlüssel direkt übergeben
Importiere die Client-Klasse und übergebe deinen API-Schlüssel dem Konstruktor:
import { Client } from "wavespeed";
const client = new Client("your-api-key-here");
Generiere dein erstes Bild
Hier ist ein vollständiges Beispiel, das ein Bild mit Z-Image Turbo generiert:
import wavespeed from "wavespeed";
const output = await wavespeed.run(
"wavespeed-ai/z-image/turbo",
{ prompt: "A serene mountain landscape at sunset with golden light" }
);
console.log(output["outputs"][0]); // URL to the generated image
Die run()-Methode handhabt den gesamten Workflow: Absenden der Anfrage, Abfrage des Abschluss und Rückgabe des Ergebnisses.
Dateien hochladen
Für Workflows, die Eingabebilder erfordern (wie Bild-zu-Video), verwende die upload()-Methode, um eine URL zu erhalten, auf die WaveSpeedAI zugreifen kann:
import wavespeed from "wavespeed";
// Upload a local image file
const imageUrl = await wavespeed.upload("./my-image.png");
// Use the uploaded image for video generation
const video = await wavespeed.run(
"wavespeed-ai/wan-2.1/image-to-video",
{
image: imageUrl,
prompt: "Camera slowly zooms in while clouds move in the background"
}
);
console.log(video["outputs"][0]); // URL to the generated video
Konfigurationsoptionen
Client-Optionen
Konfiguriere das Wiederholungsverhalten beim Initialisieren des Clients:
import { Client } from "wavespeed";
const client = new Client(process.env.WAVESPEED_API_KEY, {
maxRetries: 3, // Max retries for failed requests
maxConnectionRetries: 5, // Max retries for connection errors
retryInterval: 1.0 // Seconds between retries
});
Run-Optionen
Konfiguriere einzelne run()-Aufrufe:
import wavespeed from "wavespeed";
const output = await wavespeed.run(
"wavespeed-ai/z-image/turbo",
{ prompt: "A cute orange cat wearing a tiny hat" },
{
timeout: 60, // Max seconds to wait for completion
pollInterval: 0.5, // Seconds between status checks
enableSyncMode: true // Use synchronous mode if available
}
);
Arbeiten mit verschiedenen Modellen
Text-zu-Bild
Generiere Bilder aus Textbeschreibungen:
import wavespeed from "wavespeed";
const output = await wavespeed.run(
"wavespeed-ai/z-image/turbo",
{
prompt: "A futuristic cityscape with flying cars and neon lights",
size: "1024x1024"
}
);
Bild-zu-Video
Transformiere statische Bilder in Videos:
import wavespeed from "wavespeed";
const imageUrl = await wavespeed.upload("./landscape.jpg");
const video = await wavespeed.run(
"wavespeed-ai/wan-2.1/image-to-video",
{
image: imageUrl,
prompt: "Gentle wind blowing through the trees"
}
);
Text-zu-Video
Generiere Videos direkt aus Text:
import wavespeed from "wavespeed";
const video = await wavespeed.run(
"wavespeed-ai/wan-2.1/t2v-480p",
{
prompt: "A golden retriever running through a field of flowers"
}
);
TypeScript-Verwendung
Das SDK enthält TypeScript-Definitionen. Hier ist ein typsicheres Beispiel:
import wavespeed from "wavespeed";
interface GenerationOutput {
outputs: string[];
timings?: Record<string, number>;
}
const output: GenerationOutput = await wavespeed.run(
"wavespeed-ai/z-image/turbo",
{ prompt: "An astronaut riding a horse on Mars" }
);
const imageUrl: string = output.outputs[0];
console.log(imageUrl);
Fehlerbehandlung
Konfiguriere für Produktionsanwendungen Wiederholungen und behandele Fehler elegant:
import { Client } from "wavespeed";
const client = new Client(process.env.WAVESPEED_API_KEY, {
maxRetries: 3,
maxConnectionRetries: 5,
retryInterval: 1.0
});
try {
const output = await client.run(
"wavespeed-ai/z-image/turbo",
{ prompt: "A beautiful sunset over the ocean" },
{ timeout: 120 }
);
console.log("Generated:", output.outputs[0]);
} catch (error) {
console.error("Generation failed:", error.message);
}
Ressourcen
- GitHub Repository: github.com/WaveSpeedAI/wavespeed-javascript
- npm Package: npmjs.com/package/wavespeed
- API-Dokumentation: docs.wavespeed.ai
- Modellbibliothek: wavespeed.ai/models
Beginne heute mit WaveSpeedAI und bringe KI-gesteuerte Bild- und Videogenerierung in deine JavaScript-Anwendungen.




