Changelog #62

·

Environment variables SDK

Eric Allam

Eric Allam

CTO, Trigger.dev

Image for Environment 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

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

Read the full docs for the details.

Ready to start building?

Build and deploy your first task in 3 minutes.

Get started now