diff --git a/deepgram/__init__.py b/deepgram/__init__.py index dcdb5990..d8658897 100644 --- a/deepgram/__init__.py +++ b/deepgram/__init__.py @@ -1,4 +1,5 @@ from typing import Union +import re from ._types import Options from .keys import Keys from .transcription import Transcription @@ -12,6 +13,11 @@ from .errors import DeepgramSetupError, DeepgramApiError +def api_key_is_valid(api_key: str) -> bool: + pattern = r"^[a-f0-9]{40}$" + return re.match(pattern, api_key) is not None + + class Deepgram: def __init__(self, options: Union[str, Options]) -> None: if not isinstance(options, (str, dict)): @@ -23,6 +29,8 @@ def __init__(self, options: Union[str, Options]) -> None: if "api_key" not in options: raise DeepgramSetupError("API key is required") + if not api_key_is_valid(options["api_key"]): + raise DeepgramSetupError("Invalid API key") if "api_url" in options and options.get("api_url", None) is None: raise DeepgramSetupError("API URL must be valid or omitted")