This example demonstrates how to generate images using OpenAI's DALL-E model through the Langbase Images API.
Generate Image with OpenAI Example
Generate Image with OpenAI
import 'dotenv/config';
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
// Check if API keys are available
if (!process.env.LANGBASE_API_KEY) {
console.error('❌ LANGBASE_API_KEY is not set in environment variables');
return;
}
if (!process.env.OPENAI_API_KEY) {
console.error('❌ OPENAI_API_KEY is not set in environment variables');
console.log('Please add your OpenAI API key to the .env file');
return;
}
const result = await langbase.images.generate({
prompt: "A futuristic cityscape with flying cars and neon lights, cyberpunk style, highly detailed, 8k resolution",
model: "openai:gpt-image-1",
size: "1024x1024",
apiKey: process.env.OPENAI_API_KEY!
});
console.log('✅ Image generated successfully!');
console.log('Image URL:', result.choices[0].message.images[0].image_url.url);
}
main();