> ## Documentation Index
> Fetch the complete documentation index at: https://namespace.so/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Secrets

> Give a Devbox API keys and tokens without putting them in code, prompts, or spec files.

> [!NOTE]
>
> Every Devbox can use [Namespace Secrets](https://namespace.so/docs/architecture/storage/secrets.md), the encrypted vault shared across your workspace. The [`nsc` CLI](https://namespace.so/docs/reference/cli/installation), and the vault client in the TS SDK manage it.

## Store a secret

Each command returns the secret's ID, such as `sec_abcde12345`, which you use in the next steps.

```bash CLI theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
nsc vault add --from_file token.txt --description "API token"
```

```typescript TS SDK theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { createVaultClient } from "@namespacelabs/sdk/api/vault";
import { loadDefaults } from "@namespacelabs/sdk/auth";

const { vault } = createVaultClient({ tokenSource: loadDefaults });

const { objectId } = await vault.createObject({
  description: "API token",
  value: "YOUR_TOKEN",
});
```

## Use a secret in a Devbox

The secret becomes an environment variable in the Devbox.

```bash CLI theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
devbox create --from - <<'EOF'
name: my-devbox
image: builtin:default
size: m
env:
  - name: API_TOKEN
    from_secret_id: sec_abcde12345
EOF
```

```typescript TS SDK theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const devbox = await client.devboxes.create({
  name: "my-devbox",
  imageName: "builtin:default",
  size: "m",
  environment: { API_TOKEN: { secretId: "sec_abcde12345" } },
});
```

The Devbox reads secret values when it starts. After you update a secret, stop and start the Devbox to use the new value. Restarting a service isn't enough, including for secrets set on the service. To give every Devbox from a [Blueprint](https://namespace.so/docs/devbox/blueprint.md#additional-configuration) the same secret, set it in the Blueprint.

## Use a secret in a service

```typescript TS SDK theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
await devbox.services.create({
  name: "worker",
  command: "node",
  args: ["worker.js"],
  environment: { API_TOKEN: { secretId: "sec_abcde12345" } },
});
```

## Related

* [Services](https://namespace.so/docs/devbox/services.md) to keep a process running on a Devbox.
* Reference: [`nsc vault add`](https://namespace.so/docs/reference/cli/vault-add), [spec file](https://namespace.so/docs/reference/devbox-cli/spec-file.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.