For the ‘API key’ use case, the terms ‘key’ and ‘token’ sometimes get used casually to refer to a value used similarly to a password, to provide access to data stored in other database tables (encrypted or not) - and often tied in to the user authorization system in Anvil. The term ‘key’ otherwise is used to perform both encryption and decryption of values stored in a table column.
If your use case is ‘API key’, you can provide a user interface for validated users to generate hash values from user-entered values, from randomly generated values, etc. (using bcrypt or other libraries), and save those hash values to a column in the user table (or link a table containing hash values, to the user table), and validate users that way. With that sort of interface, users can change their keys after logging in with valid credentials - or administrators, back-end system logic (including logic to manage scheduled expiration practices), etc. - any entity with validated access to the API key table/column - can create new keys and delete old keys as needed - but no one can perform a reverse lookup of those keys. They’re basically just used as a secure way to save validation ‘keys’/‘tokens’ (with hashes that can only be confirmed to match an input, but never be read), as opposed to a way to save ‘keys’ that are used to encrypt and decrypt values in the database.
I hope this hasn’t come across as patronizing, just hoping to avoid potential indirection down an incorrect path, if your use case is simply to handle ‘API keys’ that are securely hashed (can’t be looked up). If your intent is to encrypt and decrypt database columns (so they can be written to and read from, not just validated as correct), with keys that need to be rotated often, then using Anvil secrets provides a mechanism to do that, but requires developing an internal process to manage rotating keys (periodically reading all the data from columns with a previously saved encryption key, and then encrypting that data again in the same cells of the table, with a new key (stored in Anvil secrets) - and those secrets can’t be managed by the code of an app (that’s not entirely true - code in an anvil server module could potentially be used to edit environment variables if you’re self hosting…). If you have compliance requirements to satisfy (HIPAA, PCI, etc.), then at least some data in the app needs to be encrypted at rest (you can also look into full hard drive encryption, and database systems like MSSQL and Firestore that provide automated encryption and key rotation).
My assumption, from your description (despite the word ‘encryption’), is that you’re most likely asking about how to store keys that provide account access (rather than keys to decrypt encrypted values). If that’s the case, storing hashed values in database table, along with a column storing expiration dates, etc., is the practice you’re looking for. The hashed values can’t be decrypted, so they are generally a secure way of storing values that need to be validated for access.