JavaScript SDK
JavaScript/Node.js code examples for the PicX Studio API.
Setup
const API_KEY = process.env.PICX_API_KEY;
const BASE_URL = "https://api.picxstudio.com";
const headers = {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
};Generate Image
async function generateImage(prompt, options = {}) {
const res = await fetch(`${BASE_URL}/v1/images/generate`, {
method: "POST",
headers,
body: JSON.stringify({
prompt,
model: options.model || "gemini-3.1-flash-image-preview",
size: options.size || "2K",
}),
});
return res.json();
}
const result = await generateImage("a beautiful sunset");
console.log("URL:", result.url);Generate Video (Async)
async function generateVideo(prompt, options = {}) {
// Submit job
const submitRes = await fetch(`${BASE_URL}/api/generations/video`, {
method: "POST",
headers,
body: JSON.stringify({ prompt, duration: options.duration || 5 }),
});
const job = await submitRes.json();
// Poll for completion
while (true) {
const statusRes = await fetch(`${BASE_URL}/api/generations/${job.id}`, { headers });
const status = await statusRes.json();
if (status.status === "completed") return status.output_url;
if (status.status === "failed") throw new Error(status.error_message);
await new Promise(r => setTimeout(r, 5000));
}
}
const videoUrl = await generateVideo("a cat walking");Webhook Handler (Express)
import express from "express";
import crypto from "crypto";
const app = express();
const SECRET = "whsec_your_secret";
app.post("/webhook", express.raw({ type: "application/json" }), (req, res) => {
const sig = req.headers["x-webhook-signature"] || "";
const expected = `sha256=${crypto.createHmac("sha256", SECRET).update(req.body).digest("hex")}`;
if (sig !== expected) return res.status(401).json({ error: "Invalid signature" });
const payload = JSON.parse(req.body);
if (payload.event === "generation.completed") {
console.log("Completed:", payload.output_url);
}
res.json({ ok: true });
});