{"record":{"id":"5f75a7860f255195","repo":"datawhalechina/hello-agents","slug":"mx-apikey-export-mx-apikey-your-5f75a7","errorCode":null,"errorMessage":"MX_APIKEY 环境变量未设置，请先设置环境变量：\nexport MX_APIKEY=your_api_key_here\n或者在初始化时传入 api_key 参数","messagePattern":"MX_APIKEY 环境变量未设置，请先设置环境变量：\nexport MX_APIKEY=your_api_key_here\n或者在初始化时传入 api_key 参数","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Co-creation-projects/lcyting-StockSage-agent/skills/金融数据/mx-data/mx_data.py","lineNumber":194,"sourceCode":"                    wide_rows.append(row_d)\n                return wide_rows, col_labels\n\n    # Fallback\n    return [], []\n\nclass MXData:\n    \"\"\"妙想金融数据查询客户端\"\"\"\n    \n    BASE_URL = \"https://mkapi2.dfcfs.com/finskillshub/api/claw/query\"\n    \n    def __init__(self, api_key: Optional[str] = None):\n        \"\"\"\n        初始化客户端\n        :param api_key: MX API Key，如果不提供则从环境变量 MX_APIKEY 读取\n        \"\"\"\n        self.api_key = api_key or os.getenv(\"MX_APIKEY\")\n        if not self.api_key:\n            raise ValueError(\n                \"MX_APIKEY 环境变量未设置，请先设置环境变量：\\n\"\n                \"export MX_APIKEY=your_api_key_here\\n\"\n                \"或者在初始化时传入 api_key 参数\"\n            )\n    \n    def query(self, tool_query: str) -> Dict[str, Any]:\n        \"\"\"\n        查询金融数据\n        :param tool_query: 自然语言查询问句，如 \"东方财富最新价\"\n        :return: API 响应结果\n        \"\"\"\n        headers = {\n            \"Content-Type\": \"application/json\",\n            \"apikey\": self.api_key\n        }\n        data = {\n            \"toolQuery\": tool_query\n        }","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/lcyting-StockSage-agent/skills/金融数据/mx-data/mx_data.py#L176-L212","documentation":"MXData.__init__ raises ValueError when both the api_key parameter and the MX_APIKEY environment variable are empty. It is an identical credential guard to the one in mx_search.py, applied to the financial-data query client (https://mkapi2.dfcfs.com/finskillshub/api/claw/query). Because the check runs in the constructor, any downstream MXData().query(...) call can never execute until the key is supplied.","triggerScenarios":"Instantiating MXData() without arguments in a process where MX_APIKEY is unset; passing api_key=\"\" or None explicitly; setting the var after the Python process started (os.getenv only reads at call time inside __init__, so late exports in the parent shell don't help a running interpreter); CI jobs and Docker containers that were not given the variable.","commonSituations":"Following the skill README on a new machine without completing the env setup step; secrets managed in .env but the data skill script has no load_dotenv(); key configured for the search skill (mx_search) but the data skill run by a different launcher lacking the env; name mismatch such as MX_API_KEY.","solutions":["Construct with the key: MXData(api_key=os.environ[\"MX_APIKEY\"]).","export MX_APIKEY=your_api_key_here in the shell that launches the script and verify with print(os.getenv(\"MX_APIKEY\")).","Call load_dotenv() at program entry if the key is stored in .env next to the skill.","Inject MX_APIKEY into the deployment environment (Docker ENV/secret, systemd Environment=, CI secret variable).","Centralize the check: one shared get_mx_apikey() helper used by both MXSearch and MXData so the failure message is consistent."],"exampleFix":"// before\ndata = MXData()\n// after\nimport os\napi_key = os.getenv(\"MX_APIKEY\")\nif not api_key:\n    raise SystemExit(\"MX_APIKEY missing — add it to .env and run load_dotenv()\")\ndata = MXData(api_key=api_key)","handlingStrategy":"validation","validationCode":"import os\nif not os.getenv(\"MX_APIKEY\"):\n    raise SystemExit(\"MX_APIKEY not set — add it to the environment or .env before using MXData\")","typeGuard":null,"tryCatchPattern":"try:\n    data = MXData(api_key=os.getenv(\"MX_APIKEY\"))\nexcept ValueError:\n    logger.error(\"MX_APIKEY missing; refusing to start\")\n    raise","preventionTips":["Share one credential-loading helper across MXSearch and MXData","Add a preflight env check in the deployment healthcheck","Keep .env out of git but ship .env.example with the exact variable names"],"tags":["api-key","environment-variables","configuration","python","startup"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}