Finding the AES key with access to the function Say an attacker has access to a non-rate-limited API that takes (IV, data) and outputsAES-CBC(key, input, data), with the key remaining on the server side. However, the attacker can pass any (valid) IV and data that they want in an attempt to recover the key.

Is there any optimal way to recover the key with such access?
No. What you have access to is called an oracle. It is an entity that encrypts or decrypts arbitrary data with a given secret key. A modern and secure block cipher like AES remains secure from key recovery even in the face of an encryption or decryption oracle. If it was not, the cipher would be considered fatally broken. The best-known way to recover the key with what you have is exhaustive search (brute force).