{"record":{"id":"f66f46b84fdfa323","repo":"alibaba/spring-ai-alibaba","slug":"invalidrequest","errorCode":"InvalidRequest","errorMessage":"Request invalid, api key can not be more than 20..","messagePattern":"Request invalid, api key can not be more than 20\\.\\.","errorType":"error_code","errorClass":"BizException","httpStatus":400,"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":85,"sourceCode":"\n\tpublic ApiKeyServiceImpl(ApiKeyMapper apiKeyMapper, RedisManager redisManager) {\n\t\tthis.apiKeyMapper = apiKeyMapper;\n\t\tthis.redisManager = redisManager;\n\t}\n\n\t/**\n\t * Creates a new API key for the current account. Enforces maximum API key limit per\n\t * account.\n\t * @param apiKey API key details\n\t * @return ID of the created API key\n\t */\n\t@Override\n\tpublic Long createApiKey(ApiKey apiKey) {\n\t\tRequestContext context = RequestContextHolder.getRequestContext();\n\n\t\tlong apiKeyCount = getApiKeyCount(context.getAccountId());\n\t\tif (apiKeyCount >= MAX_API_KEY_PER_ACCOUNT) {\n\t\t\tthrow new BizException(\n\t\t\t\t\tErrorCode.INVALID_REQUEST.toError(\"api key can not be more than \" + MAX_API_KEY_PER_ACCOUNT + \".\"));\n\t\t}\n\n\t\tApiKeyEntity entity = BeanCopierUtils.copy(apiKey, ApiKeyEntity.class);\n\t\tString apiKeyString = IdGenerator.genApiKey();\n\t\tentity.setApiKey(AESCryptUtils.encrypt(apiKeyString));\n\t\tentity.setAccountId(context.getAccountId());\n\t\tentity.setGmtCreate(new Date());\n\t\tentity.setGmtModified(new Date());\n\t\tentity.setCreator(context.getAccountId());\n\t\tentity.setModifier(context.getAccountId());\n\n\t\tthis.save(entity);\n\n\t\t// cache it\n\t\tString key = getApiKeyCacheKey(apiKeyString);\n\t\tredisManager.put(key, entity);\n","sourceCodeStart":67,"sourceCodeEnd":103,"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#L67-L103","documentation":"BizException (INVALID_REQUEST) thrown by ApiKeyServiceImpl.createApiKey when the account already owns MAX_API_KEY_PER_ACCOUNT (20) API keys. Creation is a hard quota check against getApiKeyCount(accountId) before inserting a new key.","triggerScenarios":"Creating a 21st API key for the same account via the admin API.","commonSituations":"Automated scripts creating keys per test run without cleanup; team sharing one account and exhausting the quota; forgotten keys accumulating over time.","solutions":["Delete unused API keys via the delete API/console, then retry creation.","Reuse an existing key instead of minting a new one.","Raise the limit by patching MAX_API_KEY_PER_ACCOUNT if your deployment legitimately needs more (requires code change)."],"exampleFix":"// before\napiKeyService.createApiKey(newApiKey); // 21st key -> throws\n// after\nif (apiKeyService.getApiKeyCount(accountId) < 20) {\n    apiKeyService.createApiKey(newApiKey);\n} else {\n    apiKeyService.deleteApiKey(staleKeyId); // free a slot first\n}","handlingStrategy":"validation","validationCode":"if (apiKeyService.getApiKeyCount(accountId) >= 20) {\n    throw new IllegalStateException(\"API key quota (20) reached; delete unused keys first\");\n}","typeGuard":"boolean canCreateApiKey(long currentCount) {\n    return currentCount < 20;\n}","tryCatchPattern":"try {\n    apiKeyService.createApiKey(apiKey);\n} catch (BizException e) {\n    if (e.getMessage().contains(\"more than 20\")) {\n        // delete or rotate an existing key, then retry\n    }\n}","preventionTips":["Rotate keys by replacing old ones, not accumulating new ones.","Clean up test-created keys in CI teardown.","Track key count per account with a periodic audit."],"tags":["quota","api-key","limit-exceeded"],"backgroundTag":"rate-limit-exceeded","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"}