Credential Management
Relevant source files
The following files were used as context for generating this wiki page:
- packages/cli/src/credentials/credentials.service.ts
- packages/core/src/encryption/cipher.ts
- packages/db/src/entities/credentials-entity.ts
- packages/nodes-base/credentials/HttpBasicAuth.credentials.ts
- packages/nodes-base/nodes/HttpRequest/HttpRequest.node.json
- packages/nodes-base/nodes/Github/Github.node.json
- packages/nodes-base/nodes/Webhook/Webhook.node.json
Credential Management in n8n is a core system responsible for securely storing, encrypting, and providing access to sensitive data like API keys, tokens, and login information. This system allows n8n nodes to authenticate with external services without exposing secrets within the workflow JSON. Credentials are encrypted at rest and are only decrypted when a workflow execution requires them.
The system is composed of several key parts: a database entity for storing credential data, an encryption service for securing the data, a standardized interface for defining different credential types, and a command-line interface (CLI) for managing credentials. Each n8n node that requires authentication is associated with a specific credential type.
Credential Data Model
At the core of credential management is the CredentialsEntity, which defines the database schema for storing credentials. Each record represents a single set of credentials created by a user.
Sources: packages/db/src/entities/credentials-entity.ts
Entity Relationship Diagram
This diagram shows the structure of the credentials_entity table in the database.
Fields
The following table details the columns in the credentials_entity table.
| Field | Type | Constraints | Description |
|---|---|---|---|
id | varchar(36) | Primary Key | Unique identifier for the credential. |
name | varchar | Not Null | User-defined name for the credential. |
data | json | Not Null | Encrypted JSON object containing the actual credential values (e.g., API key). |
type | varchar | Not Null | The type of the credential, linking it to a specific credential definition. |
createdAt | datetime | Not Null | Timestamp of when the credential was created. |
updatedAt | datetime | Not Null | Timestamp of the last update to the credential. |
Sources: packages/db/src/entities/credentials-entity.ts:13-32
Credential Encryption
To ensure security, all credential data is encrypted before being stored in the database. The system uses the AES-256-GCM algorithm for encryption and decryption. An encryption key must be defined in the n8n configuration for this process to work.
Sources: packages/core/src/encryption/cipher.ts
Encryption/Decryption Flow
The following diagram illustrates the process of encrypting and decrypting credential data.
The core logic is handled by the encrypt and decrypt functions within cipher.ts. The encrypt function takes a string, generates a random 16-byte initialization vector (IV), and uses crypto.createCipheriv to produce an encrypted hex string concatenated with the IV. The decrypt function reverses this process.
Credential Type Definition
Each type of credential (e.g., HTTP Basic Auth, OAuth2, API Key) is defined by a class that implements the ICredentialType interface. These definitions specify the properties the credential requires, such as input fields for the user to fill out.
As an example, HttpBasicAuth.credentials.ts defines a credential type for Basic Authentication.
Sources: packages/nodes-base/credentials/HttpBasicAuth.credentials.ts
HttpBasicAuth Example
The HttpBasicAuthApi class defines the name, display name, and properties for this credential type.
| Property | Value | Description |
|---|---|---|
name | 'httpBasicAuth' | The internal identifier for this credential type. |
displayName | 'Basic Auth' | The user-facing name displayed in the n8n UI. |
properties | ICredentialField[] | An array of objects defining the input fields required, such as user and password. Both are string types, with password being of type password. |
Sources: packages/nodes-base/credentials/HttpBasicAuth.credentials.ts:7-25
Node and Credential Association
n8n nodes that interact with external services declare which credential types they support. This is specified in the node's .node.json definition file within the resources object. This allows the n8n UI to show the correct documentation and link to the appropriate credential creation flow.
For example, the HttpRequest.node.json does not specify a credentialDocumentation link because it can use various authentication methods configured directly in the node, rather than a single dedicated credential type. However, many other nodes do.
Sources: packages/nodes-base/nodes/Github/Github.node.json, packages/nodes-base/nodes/HttpRequest/HttpRequest.node.json
Example: Github.node.json
The GitHub node's definition file specifies a link to its credential documentation.
{
"node": "n8n-nodes-base.github",
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/github/"
}
]
}
}
This entry informs the UI where to direct users for help on setting up GitHub credentials. The actual credential type used by the node (e.g., githubApi or githubOAuth2Api) is defined in its corresponding .node.ts file.
Sources: packages/nodes-base/nodes/Github/Github.node.json:6-10
CLI for Credential Management
n8n provides a command-line interface (CLI) for administrative tasks related to credentials, primarily for backup and migration purposes. The CredentialsService handles the logic for exporting and importing credentials.
Sources: packages/cli/src/credentials/credentials.service.ts
Commands
| Command | Description |
|---|---|
export:credentials | Exports all credentials from the database to a JSON file. An optional --backup-key can be provided to encrypt the exported file. |
import:credentials | Imports credentials from a JSON file into the database. If the file is encrypted, the --backup-key used for export must be provided. |
CLI Flow
This diagram shows the workflow for exporting and importing credentials via the CLI.
Summary
The Credential Management system in n8n is a robust feature designed for security and ease of use. It abstracts the complexity of handling sensitive tokens and keys by providing a unified interface for nodes. Credentials are created once, stored securely with AES-256-GCM encryption, and reused across multiple workflows. The system's modular design, with distinct credential types and CLI management tools, makes it a scalable and maintainable solution for authentication within the n8n ecosystem.