Generating Images from Text
Learn how to generate high-quality images using the Synexa AI API.
Basic Usage
To generate images using the Synexa AI API, you can use either our Python or Node.js client libraries.
Python Example
import synexa
# Generate an image
output = synexa.run(
"black-forest-labs/flux-schnell",
input={
"prompt": "a cute puppy in the style of pixar animation"
}
)
# Save the generated image
with open('puppy.webp', 'wb') as f:
f.write(output[0].read())
Node.js Example
import Synexa from 'synexa';
const synexa = new Synexa({
auth: process.env.SYNEXA_API_TOKEN
});
// Generate an image
const [output] = await synexa.run("black-forest-labs/flux-schnell", {
input: {
prompt: "a cute puppy in the style of pixar animation"
}
});
// Get the image URL or download it
if (output instanceof FileOutput) {
const imageUrl = output.url();
const imageData = await output.blob();
}
Advanced Options
You can customize the image generation by adding parameters to the input:
# Python example with advanced options
output = synexa.run(
"black-forest-labs/flux-schnell",
input={
"prompt": "a cute puppy in the style of pixar animation",
"negative_prompt": "blurry, low quality", # What to avoid
"num_inference_steps": 30, # More steps = higher quality
"guidance_scale": 7.5 # Higher = more prompt-adherent
}
)
// Node.js example with advanced options
const [output] = await synexa.run("black-forest-labs/flux-schnell", {
input: {
prompt: "a cute puppy in the style of pixar animation",
negative_prompt: "blurry, low quality",
num_inference_steps: 30,
guidance_scale: 7.5
}
});
Error Handling
Always handle potential errors when generating images:
# Python error handling
try:
output = synexa.run("black-forest-labs/flux-schnell",
input={"prompt": "a cute puppy"}
)
except Exception as e:
print(f"Error generating image: {e}")
// Node.js error handling
try {
const [output] = await synexa.run("black-forest-labs/flux-schnell", {
input: { prompt: "a cute puppy" }
});
} catch (error) {
console.error("Error generating image:", error);
}