Black Forest Labs Provider

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.

Setup

The Black Forest Labs provider is available via the @ai-sdk/black-forest-labs module. You can install it with

pnpm add @ai-sdk/black-forest-labs

Provider Instance

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.

Image Models

You can create Black Forest Labs image models using the .image() factory method. For more on image generation with the AI SDK see generateImage().

Basic Usage

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}`);

Model Capabilities

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.

ModelDescription
flux-kontext-proFLUX.1 Kontext [pro] handles both text and reference images as inputs, enabling targeted edits and complex transformations
flux-kontext-maxFLUX.1 Kontext [max] with improved prompt adherence and typography generation
flux-pro-1.1-ultraUltra-fast, ultra high-resolution image creation
flux-pro-1.1Fast, high-quality image generation from text.

Black Forest Labs models support aspect ratios from 3:7 (portrait) to 7:3 (landscape).

Image Editing

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.

Single Image Editing

Transform an existing image using text prompts:

import {
blackForestLabs,
BlackForestLabsImageProviderOptions,
} 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 BlackForestLabsImageProviderOptions,
},
});

Multi-Reference Editing

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',
],
},
});

Input images can be provided as URLs or base64-encoded strings. They support up to 20MB or 20 megapixels per image.

Provider Options

Black Forest Labs image models support flexible provider options through the providerOptions.blackForestLabs object. The supported parameters depend on the used model ID:

  • width number - Output width in pixels (256–1920). When set, this overrides any width derived from size.
  • height number - Output height in pixels (256–1920). When set, this overrides any height derived from size.
  • outputFormat string - Desired format of the output image ("jpeg" or "png").
  • steps number - Number of inference steps. Higher values may improve quality but increase generation time.
  • guidance number - Guidance scale for generation. Higher values follow the prompt more closely.
  • imagePrompt string - Base64-encoded image to use as additional visual context for generation.
  • imagePromptStrength number - Strength of the image prompt influence on generation (0.0 to 1.0).
  • promptUpsampling boolean - If true, performs upsampling on the prompt.
  • raw boolean - Enable raw mode for more natural, authentic aesthetics.
  • safetyTolerance number - Moderation level for inputs and outputs (0 = most strict, 6 = more permissive).
  • pollIntervalMillis number - Interval in milliseconds between polling attempts (default 500ms).
  • pollTimeoutMillis number - Overall timeout in milliseconds for polling before timing out (default 60s).
  • webhookUrl string - URL for asynchronous completion notification. Must be a valid HTTP/HTTPS URL.
  • webhookSecret string - Secret for webhook signature verification, sent in the X-Webhook-Secret header.

To pass reference images for editing, use prompt.images instead of provider options. This supports up to 10 images as URLs or base64-encoded strings.

Regional Endpoints

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
});