Changelog #62

·

Environment variables SDK

Eric Allam

Eric Allam

CTO, Trigger.dev

Image forEnvironment variables SDK

From v3 SDK version 3.0.0-beta.34 we have a fully-featured SDK for managing environment variables as well as a convenient way of syncing them from other services. Read the full docs.

Directly manipulating environment variables

You can directly use SDK functions to manipulate environment variables. Here is a list of available functions:

FunctionDescription
envvars.list()List all environment variables
envvars.upload()Upload multiple env vars. You can override existing values.
envvars.create()Create a new environment variable
envvars.retrieve()Retrieve an environment variable
envvars.update()Update a single environment variable
envvars.del()Delete a single environment variable

Syncing environment variables from other services

Instead of using the SDK functions above it's much easier to use our resolveEnvVars function in your trigger.config file.

In this example we're using env vars from Infisical. You can use this with any secrets manager or environment variable provider.

/trigger.config.ts

_48
import type {
_48
TriggerConfig,
_48
ResolveEnvironmentVariablesFunction,
_48
} from "@trigger.dev/sdk/v3";
_48
_48
//This runs when you run the deploy command or the dev command
_48
export const resolveEnvVars: ResolveEnvironmentVariablesFunction = async ({
_48
//the project ref (starting with "proj_")
_48
projectRef,
_48
//any existing env vars from a .env file or Trigger.dev
_48
env,
_48
//"dev", "staging", or "prod"
_48
environment,
_48
}) => {
_48
//the existing environment variables from Trigger.dev (or your local .env file)
_48
if (
_48
env.INFISICAL_CLIENT_ID === undefined ||
_48
env.INFISICAL_CLIENT_SECRET === undefined
_48
) {
_48
//returning undefined won't modify the existing env vars
_48
return;
_48
}
_48
_48
const client = new InfisicalClient({
_48
clientId: env.INFISICAL_CLIENT_ID,
_48
clientSecret: env.INFISICAL_CLIENT_SECRET,
_48
});
_48
_48
const secrets = await client.listSecrets({
_48
environment,
_48
projectId: env.INFISICAL_PROJECT_ID!,
_48
});
_48
_48
return {
_48
variables: secrets.map((secret) => ({
_48
name: secret.secretKey,
_48
value: secret.secretValue,
_48
})),
_48
// this defaults to true
_48
// override: true,
_48
};
_48
};
_48
_48
//the rest of your config file
_48
export const config: TriggerConfig = {
_48
project: "proj_1234567890",
_48
//etc
_48
};

Read the full docs for the details.

Ready to start building?

Build and deploy your first task in 3 minutes.

Get started now
,