# Source: https://vercel.mintlify-docs-rest-api-reference.com/docs/rest-api/reference/examples/environment-variables.md

> ## Documentation Index
> Fetch the complete documentation index at: https://vercel.mintlify.app/docs/rest-api/reference/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment Variables

> Learn how to use the Vercel SDK through real-life examples.

## Add environment variables to a project

In this example, you will add new environment variables to a project and list the details of the added values.

```ts run.ts theme={"system"}
import { Vercel } from '@vercel/sdk';

const vercel = new Vercel({
  bearerToken: process.env.VERCEL_TOKEN,
});
const projectName = 'my-project'; //The project name used in the deployment URL

async function addAndListEnvVars() {
  try {
    // Add new environment variables
    const addResponse = await vercel.projects.createProjectEnv({
      idOrName: projectName,
      upsert: 'true',
      requestBody: [
        {
          key: 'API_KEY',
          value: 'secret_value',
          target: ['production', 'preview'],
          type: 'encrypted',
        },
        {
          key: 'DEBUG',
          value: 'true',
          target: ['development'],
          type: 'plain',
        },
      ],
    });
    console.log(
      'Added environment variables:',
      JSON.stringify(addResponse, null, 2),
    );
  } catch (error) {
    console.error(
      error instanceof Error ? `Error: ${error.message}` : String(error),
    );
  }
}

addAndListEnvVars();
```

## Manage variables across projects

In this example, you manage environment variables across multiple projects and environments.

```ts run.ts theme={"system"}
import { Vercel } from '@vercel/sdk';
import { OneTarget } from '@vercel/sdk/models/operations/createprojectenv';

const PROJECTS = ['project-id-1', 'project-id-2', 'project-id-3'];
const environments = ['development', 'preview', 'production'];
const VERCEL_TOKEN = process.env.VERCEL_TOKEN;

const vercel = new Vercel({
  bearerToken: VERCEL_TOKEN,
});

async function manageEnvironmentVariables() {
  try {
    const variables = [
      { key: 'API_URL', value: 'https://api.example.com' },
      { key: 'DEBUG', value: 'true', environments: ['development', 'preview'] },
      {
        key: 'SECRET_KEY',
        value: 'super-secret-key',
        encrypt: true,
        environments: ['production', 'preview'],
      },
    ];

    for (const projectId of PROJECTS) {
      console.log(`Managing environment variables for project: ${projectId}`);
      for (const variable of variables) {
        const targets =
          (variable.environments as OneTarget[]) ||
          (environments as OneTarget[]);

        const addEnv = await vercel.projects.createProjectEnv({
          idOrName: projectId,
          upsert: 'true',
          requestBody: {
            key: variable.key,
            value: variable.value,
            target: targets,
            type: variable.encrypt ? 'encrypted' : 'plain',
          },
        });
        console.log(addEnv.created);
      }
      const readEnvs = await vercel.projects.filterProjectEnvs({
        idOrName: projectId,
      });
      console.log(
        'Env Details for ',
        projectId,
        ':',
        JSON.stringify(readEnvs, null, 2),
      );
    }
  } catch (error) {
    console.error('Error:', error.response?.data || error.message);
  }
}

manageEnvironmentVariables();
```

## Add variables to custom environments

In this example, you will add environment variables to a [custom environment](https://vercel.com/docs/deployments/environments#custom-environments) in a project. You can find the project name from the [project settings page](https://vercel.com/d?to=%2F%5Bteam%5D%2F%5Bproject%5D%2Fsettings\&title=Find+your+project+name) and the custom environment slug from the [custom environments page](https://vercel.com/d?to=%2F%5Bteam%5D%2F%5Bproject%5D%2Fsettings%2Fenvironments\&title=Find+your+project+environments).

```ts run.ts theme={"system"}
import { Vercel } from '@vercel/sdk';

const vercel = new Vercel({
  bearerToken: process.env.VERCEL_TOKEN,
});

const projectName = 'my-project'; // The project name that you find in the project settings page
const customEnvironmentSlug = 'staging'; // The custom environment slug that you want to add the variable to

async function addEnvToCustomEnvironment() {
  try {
    // Get custom environments for the project
    const customEnvs = await vercel.environment.getV9ProjectsIdOrNameCustomEnvironments({
      idOrName: projectName,
    });

    console.log('Custom environments:', customEnvs);

    // Find the staging environment
    const stagingEnv = customEnvs.environments.find(
      (env) => env.slug === customEnvironmentSlug,
    );
    const stagingEnvId = stagingEnv?.id; // This will be in format "env_xxx"

    console.log(`Staging environment ID: ${stagingEnvId || 'not found'}`);

    if (stagingEnvId) {
      // Create/upsert an environment variable for the custom environment
      console.log('Creating project environment variable...');
      const createEnvResult = await vercel.projects.createProjectEnv({
        idOrName: projectName,
        upsert: 'true', // Upsert the variable if it already exists
        requestBody: {
          key: 'TEST_VAR',
          value: 'test-value',
          type: 'plain', // or "encrypted", "sensitive"
          customEnvironmentIds: [stagingEnvId], // Array of custom environment IDs
          comment: 'Test variable created via SDK',
        },
      });

      console.log('Environment variable created:', createEnvResult);
    } else {
      console.log(
        'Staging environment not found, skipping environment variable creation',
      );
    }
  } catch (error) {
    console.error(
      error instanceof Error ? `Error: ${error.message}` : String(error),
    );
  }
}

addEnvToCustomEnvironment();
```