# How to do ${opt:stage, "dev"} in a serverless.ts (js) file?

**URL:** <https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113>\
**Category:** Serverless Framework\
**Created:** [July 26, 2020, 10:19am UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113 "2020-07-26T10:19:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtin79](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mtin79/32/4437_2.png) [@mtin79](https://forum.serverless.com/u/mtin79)\
**Post date:** [July 26, 2020, 10:19am UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113/1 "2020-07-26T10:19:14Z")

</div>

hi there,  
just wondering if there are examples of full blown serverless.ts template files. on how to use/reference cli option and environment variables etc.  
thanks for help!

---

<div class="post-metadata">

**Author:** ![alexvdvalk](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/alexvdvalk/32/4449_2.png) [@alexvdvalk](https://forum.serverless.com/u/alexvdvalk)\
**Post date:** [July 28, 2020, 7:26am UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113/2 "2020-07-28T07:26:53Z")

</div>

Hi. I’ve noticed that the serverless.ts files are very new, so I don’t think there will be many examples. I have just tested your example though, and you can simply wrap it in quotes like this:

```
environment: {
test: "${opt:stage}"
}

```

Because it’s a ts file, you can also declare all your options in strings above the full object and then just reference them below, like this:

```
const myCustomStage = "dev";
const dbName = "mydatabase";

const serverlessConfiguration: Serverless = {
  provider: {
     stage: myCustomStage,
  },
};

```

You can test your config with the print command. This will interpret your config object and print it to the console.

`sls print`

---

<div class="post-metadata">

**Author:** ![mtin79](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mtin79/32/4437_2.png) [@mtin79](https://forum.serverless.com/u/mtin79)\
**Post date:** [July 28, 2020, 7:44am UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113/3 "2020-07-28T07:44:34Z")

</div>

Hi @alexvdvalk, thank your very much for your help and examples!

---

<div class="post-metadata">

**Author:** ![cbschuld](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/cbschuld/32/4464_2.png) [@cbschuld](https://forum.serverless.com/u/cbschuld)\
**Post date:** [August 6, 2020, 12:35am UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113/4 "2020-08-06T00:35:23Z")

</div>

Furthering if you are moving from the yml syntax and you make use of the custom section just as Alex has described you can do this sort of thing as well:

example custom section in the `serverless.ts`:

```auto
    custom: {
        cognito: {
            dev: {
                pool: 'us-east-1_HWLRunTyiU',
                client: 'Ap94PUjRJYALbRfjC5Q6Qr'
            },
            prod: {
                pool: 'us-east-1_66tqBehcL4x',
                client: 'Vy2qEgGb3H4yA6pKEf5F1r'
            },
.
.
.

```

You can access those variables just as Alex described with this:

```auto
environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      COGNITO_POOL: "${self:custom.cognito.${opt:stage}.pool}",
      COGNITO_CLIENT: "${self:custom.cognito.${opt:stage}.client}",
    },

```

---

<div class="post-metadata">

**Author:** ![jrobens](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@jrobens](https://forum.serverless.com/u/jrobens)\
**Post date:** [July 13, 2021, 8:34am UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113/5 "2021-07-13T08:34:15Z")

</div>

I could not get “${env.DOMAIN\_NAME}” `${env.DOMAIN_NAME}` “${env.process.DOMAIN\_NAME}” etc to work.

I could get env.process.DOMAIN to work by doing

```auto

import * as dotenv from "dotenv";
dotenv.config({path: __dirname + '/.env'});

```

```auto
domainName: process.env.DOMAIN,

```

Even though I have useDotenv: true,

---

<div class="post-metadata">

**Author:** ![yehonadav](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/yehonadav/32/6208_2.png) [@yehonadav](https://forum.serverless.com/u/yehonadav)\
**Post date:** [October 30, 2021, 1:46pm UTC](https://forum.serverless.com/t/how-to-do-opt-stage-dev-in-a-serverless-ts-js-file/12113/6 "2021-10-30T13:46:35Z")

</div>

you can set the stage in an environment variable and then use it in the serverless.ts.

example for serverless-offline:  
package.json

“scripts”: {  
“start:offline”: “set STAGE=dev&& serverless offline start”,  
},

serverless.ts

const stage = process.env.STAGE!;  
console.log({stage});

const serverlessConfiguration: Serverless = {  
…
