Skip to content

Commit e74e16a

Browse files
committed
Added sync env vars docs example
1 parent 768036a commit e74e16a

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: "Syncing environment variables from your Vercel projects"
3+
sidebarTitle: "Syncing Vercel env vars"
4+
description: "This example demonstrates how to sync environment variables from your Vercel project to Trigger.dev."
5+
---
6+
7+
## Overview
8+
9+
This example shows how to automatically sync environment variables from your Vercel project to Trigger.dev.
10+
11+
## Build configuration
12+
13+
To sync environment variables from your Vercel project to Trigger.dev, you'll first need to add this build configuration to your `trigger.config.ts` file. This extension will then automatically run every time you deploy your project.
14+
15+
This code syncs encrypted environment variables, filtering them based on the current environment (production, preview, or development).
16+
17+
```ts trigger.config.ts
18+
import { defineConfig } from "@trigger.dev/sdk/v3";
19+
import { vercelSyncEnvVars } from "@trigger.dev/build/extensions/vercelSyncEnvVars";
20+
21+
export default defineConfig({
22+
build: {
23+
extensions:
24+
extensions: [
25+
syncVercelEnvVars(),
26+
syncEnvVars(async (ctx) => {
27+
const environmentMap = {
28+
// Account for the different environment names used by Vercel
29+
prod: "production",
30+
staging: "preview",
31+
dev: "development",
32+
} as const;
33+
34+
const vercelEnvironment =
35+
environmentMap[ctx.environment as keyof typeof environmentMap];
36+
37+
const vercelApiUrl =
38+
`https://api.vercel.com/v8/projects/${process.env.VERCEL_PROJECT_ID}/env?decrypt=true`;
39+
40+
const response = await fetch(vercelApiUrl, {
41+
headers: {
42+
Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN}`,
43+
},
44+
});
45+
46+
if (!response.ok) {
47+
throw new Error(`HTTP error! status: ${response.status}`);
48+
}
49+
50+
const data = await response.json();
51+
52+
const filteredEnvs = data.envs
53+
.filter(
54+
(env: { type: string; value: string; target: string[] }) =>
55+
env.type === "encrypted" &&
56+
env.value &&
57+
env.target.includes(vercelEnvironment),
58+
)
59+
.map((env: { key: string; value: string }) => ({
60+
name: env.key,
61+
value: env.value,
62+
}));
63+
64+
return filteredEnvs;
65+
}),
66+
],,
67+
},
68+
});
69+
```
70+
71+
<Note>
72+
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
73+
customize the build process or the resulting bundle and container image (in the case of
74+
deploying). You can use pre-built extensions or create your own.
75+
</Note>
76+
77+
## Running the sync operation
78+
79+
To run the sync operation, simply run the `deploy` command. You should see some output in the console indicating that the environment variables have been synced, and they should now be available in the Trigger.dev dashboard.
80+
81+
```bash
82+
npx trigger.dev@latest deploy
83+
```

docs/mint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@
316316
"guides/examples/supabase-storage-upload",
317317
"guides/examples/react-pdf",
318318
"guides/examples/resend-email-sequence",
319-
"guides/examples/vercel-ai-sdk"
319+
"guides/examples/vercel-ai-sdk",
320+
"guides/examples/vercel-sync-env-vars"
320321
]
321322
},
322323
{

0 commit comments

Comments
 (0)