How to use environment variables in TypeScript
Using in Activity code
- TypeScript
- JavaScript
async function runWorker(): Promise<void> {
const activities = createActivities({ apiKey: process.env.MAILGUN_API_KEY });
const worker = await Worker.create({
taskQueue: 'example',
activities,
workflowsPath: require.resolve('./workflows'),
});
await worker.run();
}
const createActivities = (envVars: { apiKey: string }) => ({
async sendNotificationEmail(): Promise<void> {
// ...
await axios({
url: `https://api.mailgun.net/v3/your-domain/messages`,
method: 'post',
params: { to, from, subject, html },
auth: {
username: 'api',
password: envVars.apiKey,
},
});
},
});
async function runWorker() {
const activities = createActivities({ apiKey: process.env.MAILGUN_API_KEY });
const worker = await Worker.create({
taskQueue: 'example',
activities,
workflowsPath: require.resolve('./workflows'),
});
await worker.run();
}
const createActivities = (envVars) => ({
async sendNotificationEmail() {
// ...
await axios({
url: `https://api.mailgun.net/v3/your-domain/messages`,
method: 'post',
params: { to, from, subject, html },
auth: {
username: 'api',
password: envVars.apiKey,
},
});
},
});
Getting into Workflow
If we needed environment variables in our Workflow, here's how we'd use a Local Activity:
- TypeScript
- JavaScript
const worker = await Worker.create({
taskQueue: 'example',
activities: createActivities(process.env),
workflowsPath: require.resolve('./workflows'),
});
type EnvVars = Record<string, string>;
const createActivities = (envVars: EnvVars) => ({
async getEnvVars(): Promise<EnvVars> {
return envVars;
},
async sendNotificationEmail(apiKey: string): Promise<void> {
// ...
await axios({
url: `https://api.mailgun.net/v3/your-domain/messages`,
method: 'post',
params: { to, from, subject, html },
auth: {
username: 'api',
password: apiKey,
},
});
},
});
const worker = await Worker.create({
taskQueue: 'example',
activities: createActivities(process.env),
workflowsPath: require.resolve('./workflows'),
});
const createActivities = (envVars) => ({
async getEnvVars() {
return envVars;
},
async sendNotificationEmail(apiKey) {
// ...
await axios({
url: `https://api.mailgun.net/v3/your-domain/messages`,
method: 'post',
params: { to, from, subject, html },
auth: {
username: 'api',
password: apiKey,
},
});
},
});
- TypeScript
- JavaScript
const { getEnvVars } = proxyLocalActivities({
startToCloseTimeout: '1m',
});
const { sendNotificationEmail } = proxyActivities({
startToCloseTimeout: '1m',
});
async function yourWorkflow() {
const envVars = await getEnvVars();
if (!envVars.apiKey) {
throw new Error('missing env var apiKey');
}
await sendNotificationEmail(envVars.apiKey);
}
const { getEnvVars } = proxyLocalActivities({
startToCloseTimeout: '1m',
});
const { sendNotificationEmail } = proxyActivities({
startToCloseTimeout: '1m',
});
async function yourWorkflow() {
const envVars = await getEnvVars();
if (!envVars.apiKey) {
throw new Error('missing env var apiKey');
}
await sendNotificationEmail(envVars.apiKey);
}