{"record":{"id":"3fa39aefaf473c91","repo":"alibaba/spring-ai-alibaba","slug":"apikeynotfound","errorCode":"ApiKeyNotFound","errorMessage":"Api key can not be found.","messagePattern":"Api key can not be found\\.","errorType":"error_code","errorClass":"BizException","httpStatus":404,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/ApiKeyServiceImpl.java","lineNumber":120,"sourceCode":"\t\tredisManager.put(key, entity);\n\n\t\tString idKey = getApiKeyCacheKey(context.getAccountId(), entity.getId());\n\t\tredisManager.put(idKey, entity);\n\n\t\treturn entity.getId();\n\t}\n\n\t/**\n\t * Updates an existing API key's description.\n\t * @param apiKey API key details to update\n\t */\n\t@Override\n\tpublic void updateApiKey(ApiKey apiKey) {\n\t\tRequestContext context = RequestContextHolder.getRequestContext();\n\n\t\tApiKeyEntity entity = getApiKeyById(context.getAccountId(), apiKey.getId());\n\t\tif (entity == null) {\n\t\t\tthrow new BizException(ErrorCode.API_KEY_NOT_FOUND.toError());\n\t\t}\n\n\t\tentity.setDescription(apiKey.getDescription());\n\t\tentity.setModifier(context.getAccountId());\n\t\tentity.setGmtModified(new Date());\n\n\t\tthis.updateById(entity);\n\n\t\t// cache it\n\t\tString originalKey = AESCryptUtils.decrypt(entity.getApiKey());\n\t\tString key = getApiKeyCacheKey(originalKey);\n\t\tredisManager.put(key, entity);\n\n\t\tString idKey = getApiKeyCacheKey(context.getAccountId(), entity.getId());\n\t\tredisManager.put(idKey, entity);\n\t}\n\n\t/**","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/ApiKeyServiceImpl.java#L102-L138","documentation":"BizException (API_KEY_NOT_FOUND) thrown by ApiKeyServiceImpl.updateApiKey when getApiKeyById(accountId, apiKey.getId()) returns null. The key ID either does not exist or does not belong to the caller's account, so the description update cannot proceed.","triggerScenarios":"Calling updateApiKey with an id referencing a deleted key, a key in another account/workspace, or a malformed/nonexistent id.","commonSituations":"Stale client cache holding ids of deleted keys; cross-account id copy-paste; id field not populated on the ApiKey passed to updateApiKey.","solutions":["List the account's API keys and update using a valid, currently existing key id.","Verify the RequestContext accountId is the account that owns the key.","Check that apiKey.getId() is actually set before calling update."],"exampleFix":"// before\nApiKey k = new ApiKey();\nk.setDescription(\"prod\"); // id never set\napiKeyService.updateApiKey(k); // throws\n// after\nApiKey k = new ApiKey();\nk.setId(existingKeyId); // fetched from list API\nk.setDescription(\"prod\");\napiKeyService.updateApiKey(k);","handlingStrategy":"validation","validationCode":"ApiKeyEntity existing = apiKeyService.getApiKeyById(accountId, apiKey.getId());\nif (existing == null) {\n    throw new IllegalArgumentException(\"API key not found for id: \" + apiKey.getId());\n}","typeGuard":"boolean apiKeyExists(Long id, Long accountId) {\n    return id != null && apiKeyService.getApiKeyById(accountId, id) != null;\n}","tryCatchPattern":"try {\n    apiKeyService.updateApiKey(apiKey);\n} catch (BizException e) {\n    if (e.getMessage().contains(\"Api key can not be found\")) {\n        // refresh key list and retry with a valid id\n    }\n}","preventionTips":["Fetch key ids from the list API immediately before updating.","Never cache key ids across accounts/environments.","Guard update calls to require a non-null id."],"tags":["not-found","api-key","update"],"backgroundTag":"record-not-found","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}