A script that needs an API key should not have the key written in it. On a Mac the keychain is right there, and the security command talks to it from the terminal.
Store the secret once:
security add-generic-password -a "$USER" -s myapi -w
-a is the account and -s the service name; both are required. Ending the command with -w and no value makes it prompt for the password, which the man page recommends, so the secret does not end up in your shell history.
Read it back in a script:
API_KEY=$(security find-generic-password -a "$USER" -s myapi -w)
With -w, only the password is printed, nothing else, so it drops straight into a variable.
Changing the value later? The man page says an item cannot already exist when you add it, unless you pass -U to update it. Without a keychain name it goes into your default keychain.
Caveat: there is an -A option that lets any application read the item without a warning, and the man page itself calls that insecure and not recommended. Leave it off. The point of the keychain is that not every program can read your secrets.
Further reading: security man page (Apple, mirrored)