alibaba/spring-ai-alibaba · error · BizException

ApiKeyNotFound

ApiKeyNotFound

Error message

Api key can not be found.

What it means

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.

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:120

		redisManager.put(key, entity);

		String idKey = getApiKeyCacheKey(context.getAccountId(), entity.getId());
		redisManager.put(idKey, entity);

		return entity.getId();
	}

	/**
	 * Updates an existing API key's description.
	 * @param apiKey API key details to update
	 */
	@Override
	public void updateApiKey(ApiKey apiKey) {
		RequestContext context = RequestContextHolder.getRequestContext();

		ApiKeyEntity entity = getApiKeyById(context.getAccountId(), apiKey.getId());
		if (entity == null) {
			throw new BizException(ErrorCode.API_KEY_NOT_FOUND.toError());
		}

		entity.setDescription(apiKey.getDescription());
		entity.setModifier(context.getAccountId());
		entity.setGmtModified(new Date());

		this.updateById(entity);

		// cache it
		String originalKey = AESCryptUtils.decrypt(entity.getApiKey());
		String key = getApiKeyCacheKey(originalKey);
		redisManager.put(key, entity);

		String idKey = getApiKeyCacheKey(context.getAccountId(), entity.getId());
		redisManager.put(idKey, entity);
	}

	/**

View on GitHub (pinned to f82da0b50f)

Solutions

  1. List the account's API keys and update using a valid, currently existing key id.
  2. Verify the RequestContext accountId is the account that owns the key.
  3. Check that apiKey.getId() is actually set before calling update.

Example fix

// before
ApiKey k = new ApiKey();
k.setDescription("prod"); // id never set
apiKeyService.updateApiKey(k); // throws
// after
ApiKey k = new ApiKey();
k.setId(existingKeyId); // fetched from list API
k.setDescription("prod");
apiKeyService.updateApiKey(k);
Defensive patterns

Strategy: validation

Validate before calling

ApiKeyEntity existing = apiKeyService.getApiKeyById(accountId, apiKey.getId());
if (existing == null) {
    throw new IllegalArgumentException("API key not found for id: " + apiKey.getId());
}

Type guard

boolean apiKeyExists(Long id, Long accountId) {
    return id != null && apiKeyService.getApiKeyById(accountId, id) != null;
}

Try / catch

try {
    apiKeyService.updateApiKey(apiKey);
} catch (BizException e) {
    if (e.getMessage().contains("Api key can not be found")) {
        // refresh key list and retry with a valid id
    }
}

Prevention

When it happens

Trigger: Calling updateApiKey with an id referencing a deleted key, a key in another account/workspace, or a malformed/nonexistent id.

Common situations: Stale client cache holding ids of deleted keys; cross-account id copy-paste; id field not populated on the ApiKey passed to updateApiKey.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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