{"record":{"id":"1f4da499b89ce5cc","repo":"crewAIInc/crewAI","slug":"missing-api-key-you-can-get-the-key-from-https","errorCode":null,"errorMessage":"Missing API key, you can get the key from https://serpapi.com/manage-api-key","messagePattern":"Missing API key, you can get the key from https://serpapi\\.com/manage-api-key","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py","lineNumber":46,"sourceCode":"        try:\n            from serpapi import Client\n        except ImportError:\n            import click\n\n            if click.confirm(\n                \"You are missing the 'serpapi' package. Would you like to install it?\"\n            ):\n                import subprocess\n\n                subprocess.run([\"uv\", \"add\", \"serpapi\"], check=True)  # noqa: S607\n                from serpapi import Client  # type: ignore[import-untyped]\n            else:\n                raise ImportError(\n                    \"`serpapi` package not found, please install with `uv add serpapi`\"\n                ) from None\n        api_key = os.getenv(\"SERPAPI_API_KEY\")\n        if not api_key:\n            raise ValueError(\n                \"Missing API key, you can get the key from https://serpapi.com/manage-api-key\"\n            )\n        self.client = Client(api_key=api_key)\n\n    def _omit_fields(\n        self, data: dict[str, Any] | list[Any], omit_patterns: list[str]\n    ) -> None:\n        if isinstance(data, dict):\n            for field in list(data.keys()):\n                if any(re.compile(p).match(field) for p in omit_patterns):\n                    data.pop(field, None)\n                else:\n                    if isinstance(data[field], (dict, list)):\n                        self._omit_fields(data[field], omit_patterns)\n        elif isinstance(data, list):\n            for item in data:\n                self._omit_fields(item, omit_patterns)\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py#L28-L64","documentation":"ValueError raised by the SerpApi base tool during initialization when the SERPAPI_API_KEY environment variable is unset or empty. The tool exclusively reads the key from the environment (os.getenv) — there is no constructor parameter — and fails fast with a pointer to the SerpApi key-management page rather than sending unauthenticated requests. It fires right after the serpapi import succeeds.","triggerScenarios":"Constructing any SerpApi tool with SERPAPI_API_KEY absent from the environment: not exported in the shell, missing from .env (or python-dotenv not loaded), or empty string.","commonSituations":"Forgetting to export the key in CI/cron/Docker (`docker run` without -e); .env file present but never loaded into the process; key defined under a different name (SERP_API_KEY, SERPAPI_KEY); new machine/deployment where secrets were never provisioned.","solutions":["Set the variable: export SERPAPI_API_KEY=... (shell), add it to .env with dotenv loaded, or pass -e SERPAPI_API_KEY=... to docker run.","Verify the exact variable name — only SERPAPI_API_KEY (from https://serpapi.com/manage-api-key) is read.","Add a startup assertion in your app: if not os.getenv('SERPAPI_API_KEY'): fail with a clear config message before building crews."],"exampleFix":"# before\ntool = SerpApiGoogleSearchTool()  # ValueError: Missing API key\n\n# after\nimport os\nfrom dotenv import load_dotenv\nload_dotenv()  # .env contains SERPAPI_API_KEY=...\nassert os.getenv(\"SERPAPI_API_KEY\"), \"SERPAPI_API_KEY not configured\"\ntool = SerpApiGoogleSearchTool()","handlingStrategy":"validation","validationCode":"import os\n\nif not os.getenv(\"SERPAPI_API_KEY\"):\n    raise RuntimeError(\"SERPAPI_API_KEY is not set — get one at https://serpapi.com/manage-api-key\")\ntool = SerpApiGoogleSearchTool()","typeGuard":null,"tryCatchPattern":"try:\n    tool = SerpApiGoogleSearchTool()\nexcept ValueError as e:\n    if \"API key\" in str(e):\n        raise SystemExit(\"Configure SERPAPI_API_KEY in the environment (or .env) and rerun\") from e\n    raise","preventionTips":["The tool reads only the SERPAPI_API_KEY env var — name it exactly, load .env explicitly.","Pass the key explicitly in CI/Docker: -e SERPAPI_API_KEY=... / secret mount.","Assert required env vars at process startup so misconfig fails before work begins."],"tags":["configuration","api-key","serpapi","environment","auth"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}