{"record":{"id":"7e41af5f89b744d0","repo":"BerriAI/litellm","slug":"bfl-api-key-is-not-set-please-set-it-via-environm-7e41af","errorCode":null,"errorMessage":"BFL_API_KEY is not set. Please set it via environment variable or pass api_key parameter.","messagePattern":"BFL_API_KEY is not set\\. Please set it via environment variable or pass api_key parameter\\.","errorType":"exception","errorClass":"BlackForestLabsError","httpStatus":401,"severity":"error","filePath":"litellm/llms/black_forest_labs/image_generation/transformation.py","lineNumber":159,"sourceCode":"        headers: dict,\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        \"\"\"\n        Validate environment and set up headers for Black Forest Labs.\n\n        BFL uses x-key header for authentication.\n        \"\"\"\n        final_api_key: Final[str | None] = (\n            api_key or get_secret_str(\"BFL_API_KEY\") or get_secret_str(\"BLACK_FOREST_LABS_API_KEY\")\n        )\n\n        if not final_api_key:\n            raise BlackForestLabsError(\n                status_code=401,\n                message=\"BFL_API_KEY is not set. Please set it via environment variable or pass api_key parameter.\",\n            )\n\n        headers[\"x-key\"] = final_api_key\n        headers[\"Content-Type\"] = \"application/json\"\n        headers[\"Accept\"] = \"application/json\"\n\n        return headers\n\n    def _get_model_endpoint(self, model: str) -> str:\n        \"\"\"\n        Get the API endpoint for a given model.\n        \"\"\"\n        # Remove provider prefix if present (e.g., \"black_forest_labs/flux-pro-1.1\")\n        model_name = model.lower()\n        if \"/\" in model_name:\n            model_name = model_name.split(\"/\")[-1]","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/black_forest_labs/image_generation/transformation.py#L141-L177","documentation":"Before any BFL request, validate_environment resolves the key as api_key argument, then BFL_API_KEY, then BLACK_FOREST_LABS_API_KEY secrets. If all are empty it raises BlackForestLabsError 401 with this message. BFL authenticates with the x-key header, which cannot be set without a key.","triggerScenarios":"Calling litellm.image_generation(model=\"black_forest_labs/...\") with neither api_key passed nor BFL_API_KEY/BLACK_FOREST_LABS_API_KEY exported; env var typo (e.g. BFL_APIKEY); key defined in a shell but not in the process running the LiteLLM proxy/server; .env file not loaded.","commonSituations":"CI jobs and containers missing the env var; proxy deployments where the key must be set in the environment of the proxy process, not the caller; local works (exported in shell) but systemd/docker service fails; secret manager name mismatch.","solutions":["export BFL_API_KEY=... (or BLACK_FOREST_LABS_API_KEY) in the process that runs LiteLLM.","Or pass api_key explicitly: litellm.image_generation(..., api_key=KEY).","In LiteLLM proxy, set the env var on the proxy host/container or configure it via the model's environment settings.","Verify with print(os.environ.get('BFL_API_KEY')) inside the same process."],"exampleFix":"# before\nlitellm.image_generation(model=\"black_forest_labs/flux-pro-1.1\", prompt=\"a cat\")\n\n# after\nlitellm.image_generation(\n    model=\"black_forest_labs/flux-pro-1.1\",\n    prompt=\"a cat\",\n    api_key=os.environ[\"BFL_API_KEY\"],\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef bfl_key_available() -> bool:\n    return bool(\n        os.environ.get(\"BFL_API_KEY\")\n        or os.environ.get(\"BLACK_FOREST_LABS_API_KEY\")\n        or os.environ.get(\"BFL_API_KEY_FALLBACK\")  # wherever you inject keys\n    )","typeGuard":null,"tryCatchPattern":"from litellm.llms.black_forest_labs.common_utils import BlackForestLabsError\n\ntry:\n    litellm.image_generation(model=\"black_forest_labs/flux-pro-1.1\", prompt=p)\nexcept BlackForestLabsError as e:\n    if e.status_code == 401 and \"BFL_API_KEY\" in str(e):\n        raise RuntimeError(\"BFL credentials missing — set BFL_API_KEY\") from e\n    raise","preventionTips":["Fail fast at startup: assert the key env var is present before serving traffic.","Pass api_key explicitly from your secret manager rather than relying on ambient env.","Name keys exactly BFL_API_KEY / BLACK_FOREST_LABS_API_KEY."],"tags":["bfl","authentication","api-key","env-var","image-generation"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}