|
| 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 | +``` |
0 commit comments