Images

The Images primitive makes it incredibly simple to generate stunning AI images using leading models from OpenAI, Together AI, Google, and more. In this quickstart, you'll learn how to generate your first AI image using Langbase.



In this guide, we'll use the Langbase SDK to interact with the Images API:


Step #1

Every request you send to Langbase needs an API key. This guide assumes you already have one. In case you do not have an API key, please check the instructions below.


Step #2

The Images API requires an LLM API key from your chosen image generation provider. Navigate to LLM API keys page and add keys for providers like OpenAI, Together AI, Google, etc.


Step #3

Create a new directory for your project and navigate to it.

Project setup

mkdir ai-image-generator && cd ai-image-generator

Initialize the project

Create a new Node.js project.

Initialize project

npm init -y

Install dependencies

You will use the Langbase SDK to generate images and dotenv to manage environment variables. So, let's install these dependencies.

Install dependencies

npm i langbase dotenv

Create an env file

Create a .env file in the root of your project and add the following environment variables:

.env

LANGBASE_API_KEY=xxxxxxxxx OPENAI_API_KEY=xxxxxxxxx

Replace xxxxxxxxx with your Langbase API key and your OpenAI API key (or any other provider's key).


Step #4

Create a new file named generate-image.ts and add the following code to generate an AI image:

generate-image.ts

import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { 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", 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();

Let's generate your first AI image by running the above file:

Generate image

npx tsx generate-image.ts

This will generate an AI image based on the prompt and return the image URL. You can open the URL in your browser to view the generated image.


Congrats, you have generated your first AI image. We're excited to see what you create with it.


Feel free to experiment with different models, prompts, and parameters.

Share your feedback and suggestions with us. Post on 𝕏 (Twitter), LinkedIn, or email us.

We're here to help you turn your ideas into AI.

Let's go!