Environment Variables
Access secrets and global constants as environment variables in a Sieve function.
About Environment Variables
Environment variables allow you to access information uniformly across public apps on Sieve and custom-deployed Sieve functions. These can be accessed through os.environ
. Several public apps, like Dubbing and Text-To-Speech, use environment variables to let users supply custom API Keys for external services like OpenAI and ElevenLabs.
If users set secrets, they will be mapped automatically to any environment variable with a matching name. For more information on secrets, click here.
Adding Environment Variables to a Sieve Function
We can add one or more environment variables to a sieve Function using the header parameter environment_variables
.
Each environment variable you would supply is packaged as a sieve.Env
object, which is structured as follows:
To use environment variables, you would pass in a list
of sieve.Env
objects to the environment_variables
parameter of the header. An example function is pasted below.
Example: Generate an image with Dall-E 3
Let’s build a simple function to generate an image using OpenAI’s Dall-E 3 model.
If you run this function via the Sieve dashboard, you will get an error similar to the following:
We don’t want to leak credentials in our source code, so let’s use secrets and environment variables to inject our API key into our function.
First, sign up for an OpenAI API key here.
Then, let’s set the API key as an environment variable via the settings page on the dashboard.
Now, let’s add an environment variable to the code to use this secret.
Now, if you run it on the Sieve dashboard, you should get an output like this!
Was this page helpful?