diff --git a/packages/nodes-base/credentials/TwilioApi.credentials.ts b/packages/nodes-base/credentials/TwilioApi.credentials.ts index 8fb77cc29e..1e2ee45fdf 100644 --- a/packages/nodes-base/credentials/TwilioApi.credentials.ts +++ b/packages/nodes-base/credentials/TwilioApi.credentials.ts @@ -9,6 +9,22 @@ export class TwilioApi implements ICredentialType { displayName = 'Twilio API'; documentationUrl = 'twilio'; properties = [ + { + displayName: 'Auth Type', + name: 'authType', + type: 'options' as NodePropertyTypes, + default: 'authToken', + options: [ + { + name: 'Auth Token', + value: 'authToken', + }, + { + name: 'API Key', + value: 'apiKey', + }, + ], + }, { displayName: 'Account SID', name: 'accountSid', @@ -20,6 +36,42 @@ export class TwilioApi implements ICredentialType { name: 'authToken', type: 'string' as NodePropertyTypes, default: '', + displayOptions: { + show: { + authType: [ + 'authToken', + ], + }, + }, + }, + { + displayName: 'API Key SID', + name: 'apiKeySid', + type: 'string' as NodePropertyTypes, + default: '', + displayOptions: { + show: { + authType: [ + 'apiKey', + ], + }, + }, + }, + { + displayName: 'API Key Secret', + name: 'apiKeySecret', + type: 'string' as NodePropertyTypes, + typeOptions: { + password: true, + }, + default: '', + displayOptions: { + show: { + authType: [ + 'apiKey', + ], + }, + }, }, ]; } diff --git a/packages/nodes-base/nodes/Twilio/GenericFunctions.ts b/packages/nodes-base/nodes/Twilio/GenericFunctions.ts index e5eaf606e0..3532030f3c 100644 --- a/packages/nodes-base/nodes/Twilio/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Twilio/GenericFunctions.ts @@ -7,6 +7,10 @@ import { IDataObject, NodeApiError, NodeOperationError, } from 'n8n-workflow'; +import { + OptionsWithUri, +} from 'request'; + /** * Make an API request to Twilio * @@ -17,7 +21,14 @@ import { * @returns {Promise} */ export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('twilioApi'); + const credentials = this.getCredentials('twilioApi') as { + accountSid: string; + authType: 'authToken' | 'apiKey'; + authToken: string; + apiKeySid: string; + apiKeySecret: string; + }; + if (credentials === undefined) { throw new NodeOperationError(this.getNode(), 'No credentials got returned!'); } @@ -26,18 +37,26 @@ export async function twilioApiRequest(this: IHookFunctions | IExecuteFunctions, query = {}; } - const options = { + const options: OptionsWithUri = { method, form: body, qs: query, uri: `https://api.twilio.com/2010-04-01/Accounts/${credentials.accountSid}${endpoint}`, - auth: { - user: credentials.accountSid as string, - pass: credentials.authToken as string, - }, json: true, }; + if (credentials.authType === 'apiKey') { + options.auth = { + user: credentials.apiKeySid, + password: credentials.apiKeySecret, + }; + } else if (credentials.authType === 'authToken') { + options.auth = { + user: credentials.accountSid, + pass: credentials.authToken, + }; + } + try { return await this.helpers.request(options); } catch (error) {