# Problem with authenticate method for custom node

**URL:** <https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309>\
**Category:** Questions\
**Tags:** credentials, core, data-transformation, deployment, node\
**Created:** [July 19, 2023, 12:55pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309 "2023-07-19T12:55:59Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 19, 2023, 12:56pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/1 "2023-07-19T12:56:00Z")

</div>

## Describe the problem/error/question

I am working on a custom node for Also Marketplace. Here is a link for [Swagger Documentation](https://app.swaggerhub.com/apis/Marketplace_SimpleAPI/Marketplace_SimpleAPI/1.0.0#/Authenticate/post_GetSessionToken). Downloadable Postman collection is more up to date.

Basically to retrieve the access token you have to send a POST request with username & password and then use that token in Headers for the operations.

## What is the error message (if any)?

500 - "`Sender` No session token was provided with the request! Please call Authenticate and pass SessionToken.

If I print the sessionToken from credentials, it seems to be generated and also works if used in Postman for any request. This error appears when Authenticate: ‘token’ is not sent in header for the request.

So my guess is authenticate method doesn’t send the sessionToken correctly for my operations. It is a valid token tho.

## Please share your workflow

I cannot share a repo, but this is my credentials file:

```auto
import {
	IAuthenticateGeneric,
	ICredentialDataDecryptedObject,
	ICredentialTestRequest,
	ICredentialType,
	IHttpRequestHelper,
	INodeProperties,
} from 'n8n-workflow';

export class AlsoMarketPlaceApi implements ICredentialType {
	name = 'alsoMarketPlaceApi';
	displayName = 'Also Marketplace API';
	properties: INodeProperties[] = [
		{
			displayName: 'Username',
			name: 'username',
			type: 'string',
			default: '',
			required: true,
		},
		{
			displayName: 'Password',
			name: 'password',
			type: 'string',
			typeOptions: {
				password: true,
			},
			default: '',
			required: true,
		},
		{
			displayName: 'Base URL',
			name: 'baseURL',
			type: 'string',
			default: '',
			required: true,
		},
		{
			displayName: 'Session Token',
			name: 'sessionToken',
			type: 'hidden',
			typeOptions: {
				expirable: true,
			},
			default: '',
		},
	];

	async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) {
		// make reques to get access token

		const url = `${credentials.baseURL}/GetSessionToken`;
		const access_token = await this.helpers.httpRequest({
			method: 'POST',
			url: url,
			headers: {
			},
			body: {
				username: credentials.username,
				password: credentials.password
			},
		});

		console.log('access_token: ')
		console.log(access_token)
		return { sessionToken: access_token };
	}

	authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
			headers: {
				Authenticate: '={{$credentials.sessionToken}}'
			},
		},
	};

	test: ICredentialTestRequest = {
		request: {
			method: 'POST',
			baseURL: '={{$credentials.baseURL}}',
			url: '/GetMarketplaces',
		},
	};

}

```

This is the node.ts file with 1 operation:

```auto
import {
	IExecuteFunctions,
} from 'n8n-core';

import {
	IHttpRequestOptions,
	INodeExecutionData,
	INodeType,
	INodeTypeDescription,
} from 'n8n-workflow';

export class Alsomarketplace implements INodeType {
	description: INodeTypeDescription = {

		displayName: 'Also Marketplace',
name: 'alsomarketplace',
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
icon: 'file:also-marketplace.png',
group: ['transform'],
version: 1,
subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}',
description: 'Consume Also Marketplace API',
defaults: {
	name: 'Also Marketplace',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
	{
		name: 'alsoMarketPlaceApi',
		required: true,
	},
],

		properties: [

			{
				displayName: 'Resource',
				name: 'resource',
				type: 'options',
				options: [
					{
						name: 'Marketplace',
						value: 'marketplace',
					},
				],
				default: 'marketplace',
				noDataExpression: true,
				required: true
			},

			{
				displayName: 'Operation',
				name: 'operation',
				type: 'options',
				displayOptions: {
					show: {
						resource: [
							'marketplace',
						],
					},
				},
				options: [
					{
						name: 'Get Many',
						value: 'getMany',
						description: 'Get many marketplaces',
						action: 'Gets company owned marketplaces',
					},
				],
				default: 'getMany',
				noDataExpression: true,
			}

		],
	};

	async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {

const items = this.getInputData();
let responseData;
const returnData = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;

const credentials = await this.getCredentials('alsoMarketPlaceApi');

for (let i = 0; i < items.length; i++) {
	if (resource === 'marketplace') {
		if (operation === 'getMany') {

			const options: IHttpRequestOptions = {
				headers: {
					'Accept': 'application/json'
				},
				method: 'POST',
				body: {},
				url: `${credentials.baseURL}/GetMarketplaces`,
				json: true,
			};

			console.log(credentials)
			responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'alsoMarketPlaceApi', options);
			returnData.push(responseData);
		}
	}
}

return [this.helpers.returnJsonArray(returnData)];

	}
}

```

## Share the output returned by the last node

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/4/d/4dca612dac6bd2cecb8c0e1af6458dc7518fbe5c.png)

Here is the form of the request in Postman that is working for get many marketplaces:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/1/4/145ffa7fc417cfce847d68a4252b548a1b77a400.png)

Can someone help me with this as project is stopped due to this error? I also noticed the sessionToken is not re-generated between requests. How exactly is that expirable: true working?

If I get the sessionToken from credentials and attach it to header in node.ts file, the request works but this is not ideal as we want to reuse the connection for http request module.

Thank you in advance, let me know if there is any info I should provide!

## Information on your n8n setup

- **n8n version: 0.236.2**
- **Running n8n via (Docker, npm, n8n cloud, desktop app): npm**
- **Operating system: MAC**

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 19, 2023, 2:11pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/4 "2023-07-19T14:11:08Z")

</div>

Tested with a hardcoded header inside authenticate and same issue. It doesn’t seem to work as it’s supposed to in my code

---

<div class="post-metadata">

**Author:** ![EmeraldHerald](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/emeraldherald/32/35355_2.png) [@EmeraldHerald](https://community.n8n.io/u/EmeraldHerald)\
**Post date:** [July 19, 2023, 3:45pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/5 "2023-07-19T15:45:53Z")

</div>

Hi @alexnemes - sorry you’re having trouble with this! Maybe our resident node-builder @marcus might have some insight on how to help? 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 20, 2023, 7:14am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/6 "2023-07-20T07:14:43Z")

</div>

Even if I try the connection like this with a valid session token in Authenticate header that works in Postman, still the authenticate method doesn’t perform:

```auto
import {
	IAuthenticateGeneric,
	ICredentialType,
	INodeProperties,
} from 'n8n-workflow';

export class AlsoMarketPlaceApi implements ICredentialType {
	name = 'alsoMarketPlaceApi';
	displayName = 'Also Marketplace API';
	properties: INodeProperties[] = [
		{
			displayName: 'Base URL',
			name: 'baseURL',
			type: 'string',
			default: '',
			required: true,
		},
	];

	authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
			headers: {
				'Authenticate': 'xyz'
			},
		},
	};

}

```

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/5/0/5069e284cfb078cc3c93deb5c7873d148383da33.png)

I am using n8n’s latest version 0.236.2

---

<div class="post-metadata">

**Author:** ![marcus](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/marcus/32/15989_2.png) [@marcus](https://community.n8n.io/u/marcus)\
**Post date:** [July 21, 2023, 8:23am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/7 "2023-07-21T08:23:41Z")

</div>

Hi @alexnemes,  
I tried your simplified credential and it’s definetly setting the Authenticate header.

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/4/5/451de87b6a58ce4642941c0b96136e12aa0cb0c4.png)

According to the swagger doc the Authenticate Header needs to have a `CCPSessionId` prefix.

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/3/5/354e1091451d4350dec527f800fdf521cc385044.png)

So maybe your credential’s authenticate method needs to look like this

```auto
authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
			headers: {
				Authenticate: '=CCPSessionId {{$credentials.sessionToken}}'
			},
		},
	};

```

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 24, 2023, 7:38am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/8 "2023-07-24T07:38:21Z")

</div>

Hi @marcus thanks for looking into it.

I tried and still same error. I know they require CCPSessionId in Swagger doc but in their Postman collection it works even without it (they use there authentication type API Key and key Authenticate). If I print the sessionToken, it is valid. If I try it in Postman, it works.:

![image](https://europe1.discourse-cdn.com/n8n/original/3X/6/c/6c3f75987f4fb3f7caa47f7025534fc646da2d21.png)

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/5/0/5026f5d67061f1241712ee08f1d26e25dcebbf0e.png)

Anyway, added CCPSessionId in authenticate method and still same result. Can I somehow print the whole request in n8n to see exactly the header attached?

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/4/b/4bdab2d5d4f02ed617ffa03e5c4efb44d0c6c4d8.png)

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/9/5/95a24700cf28e7f93cd367609cbc55923377dd9e.png)

Thank you!

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 24, 2023, 8:37am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/9 "2023-07-24T08:37:05Z")

</div>

> [@marcus](#):
>
> `=CCPSessionId {{$credentials.sessionToken}}`

Apparently it works if I use requestWithAuthentication instead of httpRequestWithAuthentication for my operation.

Also, the test request says invalid credentials but same operation marketplace: getMany works in node.

Thanks a lot for your help, it is solved now 😃

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 24, 2023, 9:29am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/10 "2023-07-24T09:29:21Z")

</div>

@marcus I noticed a strange behavior, for this get many operation, it says result has 1 item although the array definitely has more:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/8/c/8c9dd80c605699bc5ae0ef94e19335c75eaf6162.png)

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 24, 2023, 9:38am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/11 "2023-07-24T09:38:21Z")

</div>

I also get this error if I try the app’s connection with http request node. It also seems to not be working even if I select no auth and then pass a valid token in header with the CCPSessionId prefix:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/9/b/9bf80a0a80713b9553a5f5d3a9b1fabe63710e84.png)

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 24, 2023, 9:40am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/12 "2023-07-24T09:40:20Z")

</div>

![image](https://europe1.discourse-cdn.com/n8n/original/3X/b/8/b873330358ea5743337474f73b382fab4d804399.png)

---

<div class="post-metadata">

**Author:** ![marcus](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/marcus/32/15989_2.png) [@marcus](https://community.n8n.io/u/marcus)\
**Post date:** [July 24, 2023, 11:31am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/13 "2023-07-24T11:31:24Z")

</div>

Hi @alexnemes,  
that looks very strange indeed. What does the output show in the `Table `view.

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 24, 2023, 1:07pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/14 "2023-07-24T13:07:29Z")

</div>

@marcus looks like this:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/7/7/773f829d5c981534d21f7fb6a20e5ec17c256e32.png)

---

<div class="post-metadata">

**Author:** ![marcus](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/marcus/32/15989_2.png) [@marcus](https://community.n8n.io/u/marcus)\
**Post date:** [July 27, 2023, 2:21pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/15 "2023-07-27T14:21:01Z")

</div>

Are you perhaps returning an array of arrays in your node code?

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 27, 2023, 3:40pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/16 "2023-07-27T15:40:44Z")

</div>

Hi @marcus yes, indeed. This is how I fixed it:

![image](https://europe1.discourse-cdn.com/n8n/optimized/3X/b/f/bf390aba6f27d439ddc5ff5b604071f760599875_2_690x47.png)

Is there a better approach to handle this? For example in cases when the operation will throw error, this function would prevent output from getting the actual api error response object

---

<div class="post-metadata">

**Author:** ![marcus](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/marcus/32/15989_2.png) [@marcus](https://community.n8n.io/u/marcus)\
**Post date:** [July 28, 2023, 9:59am UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/17 "2023-07-28T09:59:16Z")

</div>

Hi @alexnemes,  
whenever your `requestWithAuthentication` call throws an error it should automatically be visible as an error in your execution view. Is that not happening with your custom node implementation?

---

<div class="post-metadata">

**Author:** ![alexnemes](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@alexnemes](https://community.n8n.io/u/alexnemes)\
**Post date:** [July 28, 2023, 1:06pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/18 "2023-07-28T13:06:32Z")

</div>

If I use the responseDataArr above (so I expect the successful response is an array and I want to push each element in returnData) and there is an error for the operation, this will be the output instead of the actual error from the request (in this case I intentionally formed a bad URL):

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/d/4/d4815d2c9835f6dea95548cba54f1f37c41e46b9.png)

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/c/c/cc7a33f624faab92be0d84049fc31b244760cbf5.png)

The correct error message would be printed if I use responseData as any, and not the responseDataArr expecting an array:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/c/c/cc6a8acb5a076ca8b79dfca6f7b80abed90b1b47.png)

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/a/4/a4c32cd295b9b1bb22be3b5120a07e41b5636cbb.png)

Hope this makes sense. I want to know if I can somehow consider the array response when there is a success response or else, push the json object to returnData, which is the error for the request.

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [October 26, 2023, 1:06pm UTC](https://community.n8n.io/t/problem-with-authenticate-method-for-custom-node/28309/19 "2023-10-26T13:06:40Z")

</div>

Closed on October 26, 2023, 1:06pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
