How to Retrieve API Key Without Manual Methods

I am trying to create or retrieve an API key programmatically instead of manually. Specifically, I am considering the following scenarios:

  1. Creating an API key using Terraform.
  2. Creating an API key manually and then retrieving it via an API, CLI, or any other automated method.

I would like to know if any of these approaches are possible. If not, is there any alternative way to achieve this?

Thank you in advance for your help!