|
| 1 | +# Credentials |
| 2 | + |
| 3 | +Credentials are used to authenticate with external services and store |
| 4 | +sensitive values. They are encrypted at rest. |
| 5 | + |
| 6 | +## Credential class anatomy |
| 7 | +Credentials classes have: |
| 8 | +- `name` – machine name (used in nodes' `credentials` array) |
| 9 | +- `displayName` – human-readable label in the UI |
| 10 | +- `properties` – parameters (similar types to node properties) |
| 11 | +Sensitive properties should set `typeOptions.password = true` |
| 12 | + |
| 13 | +## Example |
| 14 | +Simplified WordPress example: |
| 15 | +```typescript |
| 16 | +export class WordpressApi implements ICredentialType { |
| 17 | + name = 'wordpressApi'; |
| 18 | + displayName = 'Wordpress API'; |
| 19 | + documentationUrl = 'wordpress'; |
| 20 | + |
| 21 | + properties: INodeProperties[] = [ |
| 22 | + { |
| 23 | + displayName: 'Username', |
| 24 | + name: 'username', |
| 25 | + type: 'string', |
| 26 | + default: '', |
| 27 | + }, |
| 28 | + { |
| 29 | + displayName: 'Password', |
| 30 | + name: 'password', |
| 31 | + type: 'string', |
| 32 | + typeOptions: { |
| 33 | + password: true, |
| 34 | + }, |
| 35 | + default: '', |
| 36 | + }, |
| 37 | + { |
| 38 | + displayName: 'Wordpress URL', |
| 39 | + name: 'url', |
| 40 | + type: 'string', |
| 41 | + default: '', |
| 42 | + placeholder: 'https://example.com', |
| 43 | + }, |
| 44 | + ]; |
| 45 | + |
| 46 | + authenticate: IAuthenticateGeneric = { |
| 47 | + type: 'generic', |
| 48 | + properties: { |
| 49 | + auth: { |
| 50 | + username: '={{$credentials.username}}', |
| 51 | + password: '={{$credentials.password}}', |
| 52 | + }, |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + test: ICredentialTestRequest = { |
| 57 | + request: { |
| 58 | + baseURL: '={{$credentials?.url}}/wp-json/wp/v2', |
| 59 | + url: '/users', |
| 60 | + method: 'GET', |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Notes |
| 67 | +- `test` describes how to check if credentials are valid to show a |
| 68 | + message to the user in the UI |
| 69 | + - Not strictly required, but strongly recommended. |
| 70 | +- `authenticate` describes how to modify requests for declarative-style |
| 71 | + nodes and the HTTP Request node. |
| 72 | + |
| 73 | +## Custom authenticate function |
| 74 | +You can also use a custom authenticate function: |
| 75 | +```typescript |
| 76 | +authenticate: IAuthenticate = async (credentials, requestOptions) => { |
| 77 | + const values = (credentials.headers as { values: Array<{ name: string; value: string }> }).values; |
| 78 | + |
| 79 | + const headers = values.reduce((acc, cur) => { |
| 80 | + acc[cur.name] = cur.value; |
| 81 | + return acc; |
| 82 | + }, {} as Record<string, string>); |
| 83 | + |
| 84 | + return { |
| 85 | + ...requestOptions, |
| 86 | + headers: { |
| 87 | + ...requestOptions.headers, |
| 88 | + ...headers, |
| 89 | + }, |
| 90 | + }; |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +## OAuth2 credentials |
| 95 | +For services using OAuth2: |
| 96 | +- You usually do not need to define `test` or `authenticate` explicitly. |
| 97 | +- Instead, create credentials that extend `oAuth2Api`: |
| 98 | +```typescript |
| 99 | +export class MyServiceOAuth2Api implements ICredentialType { |
| 100 | + name = 'myServiceOAuth2Api'; |
| 101 | + displayName = 'My Service OAuth2 API'; |
| 102 | + extends = ['oAuth2Api']; |
| 103 | + |
| 104 | + properties: INodeProperties[] = [ |
| 105 | + // Add only the extra properties your service needs, |
| 106 | + // e.g. scopes, custom URLs, etc. |
| 107 | + ]; |
| 108 | +} |
| 109 | +``` |
| 110 | +- The base `oAuth2Api` handles the generic OAuth2 flow. |
| 111 | +- When allowing users to specify scopes in a custom OAuth2 credential, |
| 112 | + make sure to follow n8n's internal rules (see n8n docs). |
| 113 | +- If you want to define scopes that the credentials will request, add a |
| 114 | + property with `name: 'scope'`, `type: 'hidden'` and `default` field |
| 115 | + that has your desired scopes |
0 commit comments