跳转至

凭证文件#

凭证文件定义了节点的授权方法。此文件中的设置影响 n8n 在凭证模态中显示的内容,并且必须反映您连接的服务的身份验证要求。

在凭证文件中,您可以使用所有 n8n UI 元素。n8n 使用加密密钥对使用凭证存储的数据进行加密。

凭证文件的结构#

凭证文件遵循以下基本结构:

  1. 导入语句
  2. 为凭证创建一个类
  3. 在类中,定义控制节点身份验证的属性。

概要结构#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {
	IAuthenticateGeneric,
	ICredentialTestRequest,
	ICredentialType,
	INodeProperties,
} from 'n8n-workflow';

export class ExampleNode implements ICredentialType {
	name = 'exampleNodeApi';
	displayName = 'Example Node API';
	documentationUrl = '';
	properties: INodeProperties[] = [
		{
			displayName: 'API Key',
			name: 'apiKey',
			type: 'string',
			default: '',
		},
	];
	authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
    		// Can be body, header, qs or auth
			qs: {
        		// Use the value from `apiKey` above
				'api_key': '={{$credentials.apiKey}}'
			}

		},
	};
	test: ICredentialTestRequest = {
		request: {
			baseURL: '={{$credentials?.domain}}',
			url: '/bearer',
		},
	};
}

Parameters#

name#

String. The internal name of the object. Used to reference it from other places in the node.

displayName#

String. The name n8n uses in the GUI.

documentationUrl#

String. URL to your credentials documentation.

properties#

Each object contains:

  • displayName: the name n8n uses in the GUI.
  • name: the internal name of the object. Used to reference it from other places in the node.
  • type: the data type expected, such as string.
  • default: the URL that n8n should use to test credentials.

authenticate#

  • authenticate: Object. Contains objects that tell n8n how to inject the authentication data as part of the API request.

type#

String. If you're using an authentication method that sends data in the header, body, or query string, set this to 'generic'.

properties#

Object. Defines the authentication methods. Options are:

  • body: Object. Sends authentication data in the request body. Can contain nested objects.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		body: {
    			username: '={{$credentials.username}}',
    			password: '={{$credentials.password}}',
    		},
    	},
    };
    

  • header: Object. Send authentication data in the request header.

    1
    2
    3
    4
    5
    6
    7
    8
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		header: {
    			Authorization: '=Bearer {{$credentials.authToken}}',
    		},
    	},
    };
    

  • qs: Object. Stands for "query string." Send authentication data in the request query string.

    1
    2
    3
    4
    5
    6
    7
    8
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		qs: {
    			token: '={{$credentials.token}}',
    		},
    	},
    };
    

  • auth: Object. Used for Basic Auth. Requires username and password as the key names.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    authenticate: IAuthenticateGeneric = {
    	type: 'generic',
    	properties: {
    		auth: {
    			username: '={{$credentials.username}}',
    			password: '={{$credentials.password}}',
    		},
    	},
    };
    

test#

Provide a request object containing a URL and authentication type that n8n can use to test the credential.

1
2
3
4
5
6
test: ICredentialTestRequest = {
		request: {
			baseURL: '={{$credentials?.domain}}',
			url: '/bearer',
		},
	};
此页面是否
💬 微信

🚀 与作者交流

关注公众号
n8n实战笔记公众号
n8n实战笔记
📚 教程 💡 案例 🔧 技巧
添加微信
添加作者微信
1对1 专业指导
⚡ 快答 🎯 定制 🚀 支持