{"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":"critical","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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/black_forest_labs/image_generation/transformation.py#L141-L177","documentation":"Before any BFL request, validate_environment resolves the API key from (in order) the explicit api_key argument, BFL_API_KEY, or BLACK_FOREST_LABS_API_KEY secrets. If none is found, this BlackForestLabsError (HTTP 401) is raised. It fires before any network traffic, purely from missing configuration.","triggerScenarios":"Calling a bfl model with no api_key parameter while neither BFL_API_KEY nor BLACK_FOREST_LABS_API_KEY is present in the process environment / secret store.","commonSituations":"Forgotten env var in a new deploy or CI job; .env file not loaded; key set under a different name (e.g. only OPENAI_API_KEY); virtualenv/container where the variable wasn't exported; typos in the variable name.","solutions":["export BFL_API_KEY=<your key> (or BLACK_FOREST_LABS_API_KEY) in the environment running litellm.","Or pass the key explicitly: litellm.images.generate(..., api_key=<key>).","In server deployments, add it to the litellm proxy environment config / secrets manager so workers inherit it.","Verify with: python -c \"from litellm import get_secret_str; print(bool(get_secret_str('BFL_API_KEY')))\""],"exampleFix":"# before\nlitellm.images.generate(model=\"black_forest_labs/flux-pro-1.1\", prompt=p)\n\n# after\nlitellm.images.generate(model=\"black_forest_labs/flux-pro-1.1\", prompt=p, api_key=os.environ[\"BFL_API_KEY\"])","handlingStrategy":"validation","validationCode":"from litellm import get_secret_str\n\nif not (get_secret_str(\"BFL_API_KEY\") or get_secret_str(\"BLACK_FOREST_LABS_API_KEY\")):\n    raise RuntimeError(\"Configure BFL_API_KEY before starting the service\")","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.images.generate(model=\"bfl/flux-pro-1.1\", prompt=p)\nexcept BlackForestLabsError as e:\n    if e.status_code == 401 and \"BFL_API_KEY is not set\" in str(e):\n        fail_fast_config_error(\"missing BFL_API_KEY\")  # alert ops, do not retry\n    raise","preventionTips":["Fail fast at startup: check required provider keys in a boot-time config check.","In proxies, declare required env vars in deployment manifests so missing keys block deploy, not requests.","Run a smoke image request in staging after key rotation."],"tags":["bfl","auth","api-key","environment"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}