---
title: Store secrets
description: Give an environment's lunas API keys and passwords as secrets, read them with ctx.env, and change them without a redeploy.
related:
  - /develop/manage-environments
  - /sdk/ctx
  - /concepts/keys-and-tokens
---

Your lunas often need a key for another service, such as a payments provider. A key written into your code ends up in your repository and in every environment alike.

A secret is a named value that belongs to one environment. Your lunas read it at run time, and nobody can read it back.

## Set a secret

[`luna secrets set`](/cli/secrets) reads the value from standard input, never from the command line, so it stays out of your shell history.

```bash
luna secrets set PAYMENTS_KEY
```

In a terminal it asks for the value, which shows as you type. In a script, pipe the value in. One trailing line break is dropped.

```bash
printf '%s' "$PAYMENTS_KEY" | luna secrets set PAYMENTS_KEY
```

The secret goes to the environment the folder uses. Pass `--env production` to set it somewhere else.

## Read a secret in a handler

`ctx.env` holds the environment's secrets by name. Each value is a string.

```ts luna/lunas/shop.ts
import { defineLuna, t } from "luna";

export const Shop = defineLuna({
  name: "Shop",
  key: { id: t.string() },
  handlers: (h) => ({
    charge: h({ args: { amount: t.int() } }, async (ctx, { amount }) => {
      const key = ctx.env.PAYMENTS_KEY;
      if (!key) throw new Error("PAYMENTS_KEY is not set in this environment");
      const res = await fetch("https://payments.example.com/charges", {
        method: "POST",
        headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
        body: JSON.stringify({ amount }),
      });
      return res.ok;
    }),
  }),
});
```

A secret that is not set is missing from `ctx.env`, so check for it as above. Python lunas read `ctx.env` the same way, as [Write lunas in Python](/lunas/write-lunas-in-python) shows.

## When running lunas see a change

Setting or removing a secret makes a new version of the environment, with the same code. The command prints the version number.

Your lunas move to that version as they do after a deploy, so you do not need to deploy again. [The life of a luna](/concepts/the-life-of-a-luna) explains when each luna moves. In an environment with nothing deployed yet, the first deploy brings the secrets with it.

## Name a secret

A secret's name is capital letters, digits and underscores, up to 128 characters. It starts with a capital letter or an underscore, such as `PAYMENTS_KEY`.

A value is UTF-8 text of up to 64 KiB, and it cannot be empty.

## List and remove secrets

`luna secrets list` prints the names of the environment's secrets. Values are never shown, by this command or any other.

```bash
luna secrets list
```

`luna secrets unset` removes one, and running lunas stop seeing it in the new version.

```bash
luna secrets unset PAYMENTS_KEY
```

## Keep secrets apart per environment

Each environment has its own secrets, so production and a developer's environment can hold different keys. A new environment starts with none.

`luna env create --from` copies another environment's code and settings, but never its secrets. Set them again in the new environment.

```bash
luna secrets set PAYMENTS_KEY --env staging
```

A tenant added to an environment keeps secrets of its own too. Pass `--tenant <NAME>` to set one there, as [Add tenants](/platforms/add-tenants) explains.

## Next steps

- [Manage environments](/develop/manage-environments) shows how to make the environments that hold your secrets.
- [Deploy from CI](/develop/deploy-from-ci) sets secrets from a pipeline with an API token.
- [Reach the internet](/files-and-code/reach-the-internet) covers calling other services from a handler.
