# App secrets

Source: https://developers.swell.is/apps/app-secrets

App secrets keep your own credentials, such as an OAuth client secret or a partner API key, out of your code. You set them once with Swell CLI, and every function of your app reads them as `req.appSecrets`, in every store that installs the app.

## App secret or secret setting

An app secret holds a credential of yours, the developer's. A merchant's own credential, such as the token they get when they connect an account, goes in a `secret` field of your app's [settings](https://developers.swell.is/apps/settings).

|  | App secret | secret settings field |
| --- | --- | --- |
| Holds | Your credential, the same in every store | One merchant's credential, per installation |
| Set by | You, with swell app secret set | The merchant in the dashboard, or your function |
| Readable by | Only your app's functions | Your functions, the store's staff, and other apps that can read settings |
| Example | TIKTOK_APP_SECRET | The merchant's TikTok access token |

To get a merchant's token with OAuth, see [OAuth](https://developers.swell.is/apps/oauth).

A `secret` field's value belongs to one store and environment. It survives a reinstall unless the merchant clears the app's settings when uninstalling.

## Setting secrets

Run these commands in your app's folder once the app has been pushed with `swell app push`:

```bash
swell app secret set TIKTOK_APP_SECRET                      # asks for the value, masked
printf %s "$VALUE" | swell app secret set TIKTOK_APP_SECRET  # or reads it from a pipe
swell app secret list                                        # names, and when and by whom each was set
swell app secret unset TIKTOK_APP_SECRET
```

- **The value is never an argument.** It stays out of your shell history. A value read from a pipe loses one trailing newline, such as the one `echo` adds. An empty value is refused.
- **No value is shown again.** `swell app secret list` shows each name, with when and by whom it was last set. No API returns a value.
- **An overwrite says so.** Setting a name that already has a value answers **Updated**, with when and by whom the old value was set.
- **No new version.** A change reaches every store within a minute, without a push or a release.

## Reading secrets in functions

```typescript
export default async function (req: SwellRequest) {
  const { TIKTOK_APP_ID, TIKTOK_APP_SECRET } = req.appSecrets;
  if (!TIKTOK_APP_ID || !TIKTOK_APP_SECRET) {
    throw new SwellError('Set TIKTOK_APP_ID and TIKTOK_APP_SECRET with swell app secret set', {
      status: 500,
    });
  }

  // Call TikTok with them
}
```

- **Every function gets them:** routes, model event and schedule functions, cron functions and actions. Workflows don't. A secret that isn't set is `undefined`, so check it before use, as above.
- **Read-only.** `req.appSecrets` is a frozen object of strings.
- **Kept out of the request.** Secrets aren't in `req.headers` or `req.originalRequest`, so logging or forwarding the request doesn't expose them. Don't log them, return them, or copy them into records or settings.
- **Tests set their own values.** The `createMockRequest()` helper that `swell create tests` adds takes `{ appSecrets: { TIKTOK_APP_SECRET: 'test' } }`.
- **Types** come with `@swell/app-types` 1.4.1 or later.

## Who shares them

One set of secrets belongs to one app: the app your folder pushes to.

- **Every installation gets the same values.** That covers your development store, the test and live environments, and every store that installs the app. There is no separate set for testing, so a change made while testing reaches live stores within a minute.
- **Push access is secret access.** Anyone who can push the app can set, list and remove its secrets, and can deploy a function that reads them.
- **A copy is another app.** The same code pushed from another store becomes another app, with no secrets until they're set there.
- **Deleting the app deletes its secrets.** A forced delete that leaves the app installed in stores keeps them, so those installations keep working.

## Local development

Functions that run on your machine under `swell app dev` get an empty `req.appSecrets`.

- **Model event and schedule functions and actions run locally, without secrets.** Try code in them that needs a secret after `swell app push`.
- **Cron functions keep running on Swell, with secrets.** Swell's scheduler always runs the deployed code, even while `swell app dev` runs.
- **Routes called through the store keep running on Swell, with secrets.** That includes [OAuth](https://developers.swell.is/apps/oauth) callbacks.
- **Settings read the same locally,** including a merchant's token in a `secret` field.
- **A reminder at start.** When the app has secrets, `swell app dev` names them as it starts.

## Limits

|  | Limit |
| --- | --- |
| Name | 1 to 64 capital letters, digits and underscores, starting with a letter |
| Value | A string up to 4,096 bytes (UTF-8) |
| Per app | Up to 32 secrets, with 8,192 bytes of values in total |
| A change takes effect | Within a minute, in every store |

Available in Swell CLI 2.9.43 and later. Functions get `req.appSecrets` once they're pushed with that version.
