1. Docs
  2. Pulumi ESC
  3. Development
  4. Languages & SDKs
  5. TypeScript (Node.js)

Pulumi ESC: TypeScript/JavaScript SDK

The JavaScript/TypeScript SDK for Pulumi ESC (Environments, Secrets, and Configuration) allows you to automate Pulumi ESC.

Here are some of the scenarios the SDK can automate:

  • List environments and read environment definitions
  • Open environments to access config and resolve secrets
  • Create, update, decrypt, and delete environment definitions
    • Supports both structured types and yaml text
  • List environment revisions and create new revision tags
  • Check environment definitions for errors

Install the SDK package

Run npm install @pulumi/esc-sdk or yarn add @pulumi/esc-sdk to install the SDK package.

Initializing the SDK client

The easiest way to initialize an ESC SDK client is to run:

import * as esc from "@pulumi/esc-sdk";

const client = esc.DefaultClient();
Copy

This method will first look for the PULUMI_ACCESS_TOKEN environment variable, and if it’s not present, it will fall back to CLI credentials that are present on your machine if you have logged in using Pulumi CLI or ESC CLI.

If the default behavior does not work for you, you can always manually initialize the client configuration and pass it into the client constructor:

import * as esc from "@pulumi/esc-sdk";

const configuration = new esc.Configuration({
    accessToken: myAccessToken
})
const client = new esc.EscApi(configuration);
Copy

Examples

All of these examples expect a PULUMI_ACCESS_TOKEN and PULUMI_ORG environment variable to be set.

Manage environment example

This example creates a new environment, opens that environment to access a secret, and the list the environments.

import * as esc from "@pulumi/esc-sdk";

async function main() {
    const orgName = process.env.PULUMI_ORG!;
    const client = esc.DefaultClient();

    const projName = "examples";
    const envName = "sdk-typescript-example";

    // Create a new environment
    await client.createEnvironment(orgName, projName, envName);

    const envDef: esc.EnvironmentDefinition = {
        values: {
            my_secret: {
                "fn::secret": "shh! don't tell anyone",
            },
        },
    };

    // Update the environment with the new definition
    await client.updateEnvironment(orgName, projName, envName, envDef);

    // Open and read the environment
    const openEnv = await client.openAndReadEnvironment(orgName, projName, envName);

    if (!openEnv) {
        console.error("Failed to open and read the environment");
        return;
    }

    // Acces the value of the secret
    const secretValue = openEnv.values?.my_secret;
    console.log(`Secret value: ${secretValue}\n`);

    // List all the environments in the organization
    const orgEnvs = await client.listEnvironments(orgName);
    if (!orgEnvs || !orgEnvs.environments) {
        console.log("No environments found");
        return;
    }

    for (const env of orgEnvs.environments) {
        console.log(`Environment: ${env.project}/${env.name}`);
    }
}

(async ()=>{
    await main();
})();
Copy

Tag revision example

This example lists revisions for an environment, tags a revision, and lists revision tags.


import * as esc from "@pulumi/esc-sdk";

async function main() {
    const orgName = process.env.PULUMI_ORG!;
    const client = esc.DefaultClient();

    const projName = "examples";
    const envName = "sdk-typescript-example";

    // List environment revisions
    const revisions = await client.listEnvironmentRevisions(orgName, projName, envName);

    if (!revisions || revisions.length < 2) {
        throw new Error(`Expected at least 2 revisions for environment ${projName}/${envName}`);
    }

    // Get the second latest revision
    const revision = revisions[1];
    await client.createEnvironmentRevisionTag(orgName, projName, envName, "stable", revision.number);

    console.log(`Tagged revision ${revision.number} as 'stable'`);

    // List tags
    const tags = await client.listEnvironmentRevisionTags(orgName, projName, envName);
    if (!tags || !tags.tags) {
        throw new Error(`Expected tags for environment ${projName}/${envName}`);
    }

    for (const tag of tags?.tags) {
        console.log(`Tag ${tag.name} at revision ${tag.revision}`);
    }
}

(async ()=>{
    await main();
})();
Copy

Documentation

Was this page helpful?

PulumiUP May 6, 2025. Register Now.