> For the complete documentation index, see [llms.txt](https://docs.n8n.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/configuration-examples/set-a-custom-encryption-key.md). Agents can also search and read this documentation through its [MCP server](https://docs.n8n.io/~gitbook/mcp).

# Set a custom encryption key

Set a custom encryption key for n8n to securely encrypt credentials.

n8n creates a random encryption key automatically on the first launch and saves it in the `~/.n8n` folder. n8n uses that key to encrypt the credentials before they get saved to the database. If the key isn't yet in the settings file, you can set it using an environment variable, so that n8n uses your custom key instead of generating a new one.

In [queue mode](https://docs.n8n.io/deploy/host-n8n/configure-n8n/scaling/enable-queue-mode.md), you must specify the encryption key environment variable for all workers.

```bash
export N8N_ENCRYPTION_KEY=<SOME RANDOM STRING>
```

Refer to [Environment variables reference](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/deployment.md) for more information on this variable.

## Protect the encryption key

For production instances:

* **Generate a strong key.** For example, run `openssl rand -hex 32`.
* **Use the same key on every n8n process** that connects to the same database: main instances, workers, and webhook processors.
* **Use a different key in each environment.** Don't share a key between development, staging, and production.
* **Back up the key in a different location from the instance**, such as a secrets manager. If you lose the key, n8n can't decrypt your stored credentials and you must re-create them.
* **Don't change the key on an existing database.** n8n can't re-encrypt existing data with a new instance key, and it won't start with a different key. To rotate the data encryption key, use [encryption key rotation](https://docs.n8n.io/deploy/host-n8n/configure-n8n/security/rotate-encryption-keys.md) instead. If the instance key itself is compromised, [migrate to a new database and instance](https://docs.n8n.io/deploy/host-n8n/configure-n8n/security/rotate-encryption-keys.md#replace-a-compromised-instance-encryption-key).

## Related resources

* [Configuration examples](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/configuration-examples.md)
