> For the complete documentation index, see [llms.txt](https://docs.thewalletcrew.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.thewalletcrew.io/developers-guides/scripting/modules/secrets.md).

# twc/secrets

Read a tenant's secrets from a script with getSecret from the twc/secrets module - key rules, versions, caching, missing and expired secrets.

Scripts regularly need a credential: the password of a CRM account, a client secret, a signing key. Writing such a value in a script or in a YAML file exposes it to everyone who can read the configuration and keeps it in the configuration history. The `twc/secrets` module solves this: a secret is stored once for the tenant, and a script reads it at run time with `getSecret`. Scripts that call external systems should use it for every credential, and should never contain one.

<details>

<summary><strong>Real-world examples</strong></summary>

* A script reads the API key of a marketing platform and sends it in an `Authorization` header.
* An OAuth 2.0 client secret is read once per handler and passed to the `Authentication` option of `fetch`.
* A shared signing key is read before a payload is signed with `CryptoJS.HmacSHA256`.

</details>

## Import and signature

```js
import { getSecret } from "twc/secrets";

const value = await getSecret("CRM-API-KEY");
```

| Function         | Parameters    | Returns                                                                           |
| ---------------- | ------------- | --------------------------------------------------------------------------------- |
| `getSecret(key)` | `key`: string | A promise of the secret value as a string, or `null` when no usable value exists. |

The import is written at the top of the script file, before the `export default function`. The old name `neo/secrets` still works and is described on the page [Obsolete neostore names](https://docs.thewalletcrew.io/developers-guides/scripting/modules/obsolete-neo-names.md). The prefix `neostore` is the platform's former name and is kept in technical identifiers.

## Keys

A key is a string of 2 to 32 characters, made of letters, digits and hyphens only. Keys are case-sensitive: `CRM-API-KEY` and `crm-api-key` are two different secrets. A key that does not follow these rules makes `getSecret` throw an error that explains which rule is broken, so a typo in the name of the secret fails visibly while a well-formed name that does not exist returns `null`.

Secrets belong to one tenant. A script can read only the secrets of the tenant it runs for, and the same key can hold different values in different tenants. Secrets are created and updated in the admin console by a user whose role can manage secrets, or with the Secret API; see [API keys and secrets](https://docs.thewalletcrew.io/configure/advanced-configuration/platform/api-keys-and-secrets.md) for the procedure. A secret value is never returned by the admin console after it is saved, which is the reason a script is the right place to use it.

## Versions, activation and expiry

A secret can have several versions, for example when a credential is rotated. `getSecret` returns the most recently updated version that is currently usable. A version is usable when it is enabled, has not expired and its activation date, when one was set, has passed. A version with an empty value is skipped. A rotation can therefore be prepared in advance: a new version with a future activation date takes over by itself at that moment, and scripts need no change.

When no version is usable, `getSecret` returns `null`. This covers a key that was never created, a secret whose versions are all disabled or expired, and a secret whose new version is not active yet. The script decides what a missing credential means; ignoring `null` produces a request without credentials that the remote system rejects.

## Caching

The result of a lookup, `null` included, is kept for up to 10 minutes. The cache is shorter when a version is about to become active or to expire within that time, so that a scheduled rotation takes effect at the planned moment. Calling `getSecret` in each handler is therefore inexpensive. The delay also means that a secret saved in the admin console may take a few minutes to be seen by scripts, and that a key created after a first failed lookup can keep answering `null` for a few minutes.

## Example

The handler below reads the key when it runs, fails softly when the secret is absent, and calls the remote system with the value.

```js
import { getSecret } from "twc/secrets";

export default function (context) {
  context.register("runtime.scriptable.customerPipelineElement.crmSync", {
    CreateOrUpdate: async (entity) => {
      const apiKey = await getSecret("CRM-API-KEY");
      if (!apiKey) {
        console.error("Secret CRM-API-KEY is missing, contact not synchronized");
        return;
      }

      await fetch("https://api.example.com/v1/contacts", {
        Method: "PUT",
        Headers: { "X-Api-Key": apiKey },
        Body: { email: entity.email, firstName: entity.firstName }
      });
    }
  });
}
```

## Checking the result

A script is tested by running the flow that calls it and by checking the outcome on the remote system. When the remote system answers `401` or `403`, the first suspects are a missing secret (the script receives `null`), a key typed with a different case, and a version that is not active yet. Logging whether the value is present, never the value itself, is enough to tell them apart.

## FAQ

<details>

<summary><strong>Can a secret be used directly in the YAML configuration?</strong></summary>

Not by reference: YAML files have no syntax that reads a secret. Some connectors read their own credential from the tenant's secrets under a fixed name, as described on their setup pages. Elsewhere, a script reads the secret with `getSecret` and builds the request. Do not write a credential in a YAML file.

</details>

<details>

<summary><strong>Where can `getSecret` be used?</strong></summary>

In any handler of a script that is a file under `server/scripts/`, including the extension hooks. It is not available to the body of a [benefit code generator](https://docs.thewalletcrew.io/developers-guides/scripting/endpoints/benefit-code-generator.md), which cannot contain `import` statements.

</details>

<details>

<summary><strong>Can a script write or delete a secret?</strong></summary>

No. The module only reads. Secrets are managed in the admin console or with the Secret API.

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.thewalletcrew.io/developers-guides/scripting/modules/secrets.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
