content/providers/01-ai-sdk-providers/12-black-forest-labs.mdx
Black Forest Labs provides a generative image platform for developers with FLUX-based models. Their platform offers fast, high quality, and in-context image generation and editing with precise and coherent results.
The Black Forest Labs provider is available via the @ai-sdk/black-forest-labs module. You can install it with
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}> <Tab> <Snippet text="pnpm add @ai-sdk/black-forest-labs" dark /> </Tab> <Tab> <Snippet text="npm install @ai-sdk/black-forest-labs" dark /> </Tab> <Tab> <Snippet text="yarn add @ai-sdk/black-forest-labs" dark /> </Tab>
<Tab> <Snippet text="bun add @ai-sdk/black-forest-labs" dark /> </Tab> </Tabs>You can import the default provider instance blackForestLabs from @ai-sdk/black-forest-labs:
import { blackForestLabs } from '@ai-sdk/black-forest-labs';
If you need a customized setup, you can import createBlackForestLabs and create a provider instance with your settings:
import { createBlackForestLabs } from '@ai-sdk/black-forest-labs';
const blackForestLabs = createBlackForestLabs({
apiKey: 'your-api-key', // optional, defaults to BFL_API_KEY environment variable
baseURL: 'custom-url', // optional
headers: {
/* custom headers */
}, // optional
});
You can use the following optional settings to customize the Black Forest Labs provider instance:
baseURL string
Use a different URL prefix for API calls, e.g. to use a regional endpoint.
The default prefix is https://api.bfl.ai/v1.
apiKey string
API key that is being sent using the x-key header.
It defaults to the BFL_API_KEY environment variable.
headers Record<string,string>
Custom headers to include in the requests.
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.
pollIntervalMillis number
Interval in milliseconds between polling attempts when waiting for image generation to complete. Defaults to 500ms.
pollTimeoutMillis number
Overall timeout in milliseconds for polling before giving up. Defaults to 60000ms (60 seconds).
You can create Black Forest Labs image models using the .image() factory method.
For more on image generation with the AI SDK see generateImage().
import { writeFileSync } from 'node:fs';
import { blackForestLabs } from '@ai-sdk/black-forest-labs';
import { generateImage } from 'ai';
const { image, providerMetadata } = await generateImage({
model: blackForestLabs.image('flux-pro-1.1'),
prompt: 'A serene mountain landscape at sunset',
});
const filename = `image-${Date.now()}.png`;
writeFileSync(filename, image.uint8Array);
console.log(`Image saved to ${filename}`);
Black Forest Labs offers many models optimized for different use cases. Here are a few popular examples. For a full list of models, see the Black Forest Labs Models Page.
| Model | Description |
|---|---|
flux-kontext-pro | FLUX.1 Kontext [pro] handles both text and reference images as inputs, enabling targeted edits and complex transformations |
flux-kontext-max | FLUX.1 Kontext [max] with improved prompt adherence and typography generation |
flux-pro-1.1-ultra | Ultra-fast, ultra high-resolution image creation |
flux-pro-1.1 | Fast, high-quality image generation from text. |
flux-pro-1.0-fill | Inpainting model for filling masked regions of images with new content |
Black Forest Labs models support aspect ratios from 3:7 (portrait) to 7:3 (landscape).
Black Forest Labs Kontext models support powerful image editing capabilities using reference images. Pass input images via prompt.images to transform, combine, or edit existing images.
Transform an existing image using text prompts:
import {
blackForestLabs,
BlackForestLabsImageModelOptions,
} from '@ai-sdk/black-forest-labs';
import { generateImage } from 'ai';
const { images } = await generateImage({
model: blackForestLabs.image('flux-kontext-pro'),
prompt: {
text: 'A baby elephant with a shirt that has the logo from the input image.',
images: [
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
],
},
providerOptions: {
blackForestLabs: {
width: 1024,
height: 768,
} satisfies BlackForestLabsImageModelOptions,
},
});
Combine multiple reference images for complex transformations. Black Forest Labs supports up to 10 input images:
import { blackForestLabs } from '@ai-sdk/black-forest-labs';
import { generateImage } from 'ai';
const { images } = await generateImage({
model: blackForestLabs.image('flux-kontext-pro'),
prompt: {
text: 'Combine the style of image 1 with the subject of image 2',
images: [
'https://example.com/style-reference.jpg',
'https://example.com/subject-reference.jpg',
],
},
});
The flux-pro-1.0-fill model supports inpainting, which allows you to fill masked regions of an image with new content. Pass the source image via prompt.images and a mask image via prompt.mask:
import { blackForestLabs } from '@ai-sdk/black-forest-labs';
import { generateImage } from 'ai';
const { images } = await generateImage({
model: blackForestLabs.image('flux-pro-1.0-fill'),
prompt: {
text: 'A beautiful garden with flowers',
images: ['https://example.com/source-image.jpg'],
mask: 'https://example.com/mask-image.png',
},
});
The mask image should be a grayscale image where white areas indicate regions to be filled and black areas indicate regions to preserve.
Black Forest Labs image models support flexible provider options through the providerOptions.blackForestLabs object. The supported parameters depend on the used model ID:
size.size."jpeg" or "png").X-Webhook-Secret header.The generateImage response includes provider-specific metadata in providerMetadata.blackForestLabs.images[]. Each image object may contain the following properties:
import { blackForestLabs } from '@ai-sdk/black-forest-labs';
import { generateImage } from 'ai';
const { image, providerMetadata } = await generateImage({
model: blackForestLabs.image('flux-pro-1.1'),
prompt: 'A serene mountain landscape at sunset',
});
// Access provider metadata
const metadata = providerMetadata?.blackForestLabs?.images?.[0];
console.log('Seed:', metadata?.seed);
console.log('Cost:', metadata?.cost);
console.log('Duration:', metadata?.duration);
By default, requests are sent to https://api.bfl.ai/v1. You can select a regional endpoint by setting baseURL when creating the provider instance:
import { createBlackForestLabs } from '@ai-sdk/black-forest-labs';
const blackForestLabs = createBlackForestLabs({
baseURL: 'https://api.eu.bfl.ai/v1', // or https://api.us.bfl.ai/v1
});