How to send email without exposing your email provider's API key
To send email without exposing your email provider's API key, keep the key encrypted and only decrypt it inside the code that sends the email. On Lit Protocol, you encrypt the key once to a Lit wallet that belongs to your account. A Lit Action, which is a JavaScript function that runs inside a secure enclave, decrypts it when it needs to send a message and calls the provider directly. Your…
When sending email without exposing your email provider's API key, encrypt the key inside your code that sends the email. On Lit Protocol, encrypt the key to a Lit wallet belonging to your account. Create a Lit Action, a JavaScript function executed in a secure enclave, which decrypts the key when needed and calls the provider directly.
Your application never holds the key; it simply asks the action to send an email. Use Resend as the provider, but the same steps apply to any email API accepting a bearer key. Seal the key once, create a sending-only API key in Resend, and create a Lit wallet in the Lit dashboard or via the API. The Lit wallet is used to encrypt and decrypt data, with only actions in a group with the wallet able to use it.
Run the action once to encrypt the Resend key to that wallet, using the X-Privacy-Mode: true header to prevent Lit's server from logging the request or result. Store the ciphertext in your configuration. After obtaining the ciphertext, remove the action from the group so it only contains the code for sending email. The action sends a verification email, where the sender, subject, and template are predetermined, but the recipient and link token can be chosen.
To send an email, call the action with the recipient and token. The action checks the validity of the recipient and token, decrypts the key using Lit Actions Decrypt, and sends the email using Resend's API. The API key is never revealed in the response or error messages. Store the action and wallet in a group, and give your application a usage key that can only run that group.
This setup protects your servers, logs, and environment variables by never storing the Resend key. If someone obtains the usage key, they can send verification emails, though this may consume your sending quota and tarnish your reputation. Monitor your usage in Resend to detect any unauthorized activity. Use a sending-access-only key restricted to your sending domain to prevent leaks from accessing past messages or altering settings.
Resend and any unauthorized parties can still view the sent emails. If you need to modify the email template or sender, publish a new action version, add it to the group, and remove the old version. The ciphertext remains functional as it is tied to the wallet. This approach is often used in larger email verification flows, such as verifying a user's email and linking it to their wallet.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.