{"record":{"id":"62cd1b79373d2fe8","repo":"rohitg00/ai-engineering-from-scratch","slug":"api-key-must-be-non-empty","errorCode":null,"errorMessage":"api_key must be non-empty","messagePattern":"api_key must be non-empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"certifications/claude/lessons/30-developer-application-capstone/code/main.py","lineNumber":44,"sourceCode":"\nclass MessagesTransport(Protocol):\n    \"\"\"Small dependency-injection boundary for a Messages API call.\"\"\"\n\n    def create_message(self, payload: dict[str, Any]) -> dict[str, Any]: ...\n\n\nclass AnthropicMessagesHTTPTransport:\n    \"\"\"Opt-in stdlib HTTP transport that never logs or persists its API key.\"\"\"\n\n    def __init__(\n        self,\n        api_key: str,\n        endpoint: str = ANTHROPIC_MESSAGES_URL,\n        timeout_seconds: float = 30.0,\n        opener: Callable[..., Any] = urllib.request.urlopen,\n    ) -> None:\n        if not api_key.strip():\n            raise ValueError(\"api_key must be non-empty\")\n        self._api_key = api_key\n        self.endpoint = endpoint\n        self.timeout_seconds = timeout_seconds\n        self._opener = opener\n\n    def create_message(self, payload: dict[str, Any]) -> dict[str, Any]:\n        body = json.dumps(payload).encode(\"utf-8\")\n        request = urllib.request.Request(\n            self.endpoint,\n            data=body,\n            method=\"POST\",\n            headers={\n                \"content-type\": \"application/json\",\n                \"anthropic-version\": ANTHROPIC_VERSION,\n                \"x-api-key\": self._api_key,\n            },\n        )\n        try:","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/certifications/claude/lessons/30-developer-application-capstone/code/main.py#L26-L62","documentation":"Raised in AnthropicMessagesHTTPTransport.__init__ when the api_key argument is empty or whitespace-only. The transport must never send an unauthenticated request, so it refuses construction rather than deferring failure to the first HTTP call.","triggerScenarios":"Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:44 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Supply the real key from an environment variable (e.g. os.environ['ANTHROPIC_API_KEY'])","Fail fast at app startup when the key variable is unset instead of constructing the transport","Use a dummy non-empty key in tests via dependency-injected fakes rather than blank strings"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}