alibaba/spring-ai-alibaba · error · BizException

InvalidRequest

InvalidRequest

Error message

Request invalid, api key can not be more than 20..

What it means

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.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/ApiKeyServiceImpl.java:85

	public ApiKeyServiceImpl(ApiKeyMapper apiKeyMapper, RedisManager redisManager) {
		this.apiKeyMapper = apiKeyMapper;
		this.redisManager = redisManager;
	}

	/**
	 * Creates a new API key for the current account. Enforces maximum API key limit per
	 * account.
	 * @param apiKey API key details
	 * @return ID of the created API key
	 */
	@Override
	public Long createApiKey(ApiKey apiKey) {
		RequestContext context = RequestContextHolder.getRequestContext();

		long apiKeyCount = getApiKeyCount(context.getAccountId());
		if (apiKeyCount >= MAX_API_KEY_PER_ACCOUNT) {
			throw new BizException(
					ErrorCode.INVALID_REQUEST.toError("api key can not be more than " + MAX_API_KEY_PER_ACCOUNT + "."));
		}

		ApiKeyEntity entity = BeanCopierUtils.copy(apiKey, ApiKeyEntity.class);
		String apiKeyString = IdGenerator.genApiKey();
		entity.setApiKey(AESCryptUtils.encrypt(apiKeyString));
		entity.setAccountId(context.getAccountId());
		entity.setGmtCreate(new Date());
		entity.setGmtModified(new Date());
		entity.setCreator(context.getAccountId());
		entity.setModifier(context.getAccountId());

		this.save(entity);

		// cache it
		String key = getApiKeyCacheKey(apiKeyString);
		redisManager.put(key, entity);

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Delete unused API keys via the delete API/console, then retry creation.
  2. Reuse an existing key instead of minting a new one.
  3. Raise the limit by patching MAX_API_KEY_PER_ACCOUNT if your deployment legitimately needs more (requires code change).

Example fix

// before
apiKeyService.createApiKey(newApiKey); // 21st key -> throws
// after
if (apiKeyService.getApiKeyCount(accountId) < 20) {
    apiKeyService.createApiKey(newApiKey);
} else {
    apiKeyService.deleteApiKey(staleKeyId); // free a slot first
}
Defensive patterns

Strategy: validation

Validate before calling

if (apiKeyService.getApiKeyCount(accountId) >= 20) {
    throw new IllegalStateException("API key quota (20) reached; delete unused keys first");
}

Type guard

boolean canCreateApiKey(long currentCount) {
    return currentCount < 20;
}

Try / catch

try {
    apiKeyService.createApiKey(apiKey);
} catch (BizException e) {
    if (e.getMessage().contains("more than 20")) {
        // delete or rotate an existing key, then retry
    }
}

Prevention

When it happens

Trigger: Creating a 21st API key for the same account via the admin API.

Common situations: Automated scripts creating keys per test run without cleanup; team sharing one account and exhausting the quota; forgotten keys accumulating over time.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/f66f46b84fdfa323. Report an issue: GitHub.