# API libraries Node.js search returning an error (Workflow: Custom Code Action)

**URL:** <https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449>\
**Category:** APIs & Integrations\
**Tags:** timeline-and-extensions-apis\
**Created:** [October 24, 2023, 12:47am UTC](https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449 "2023-10-24T00:47:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EDiaz6](https://avatars.discourse-cdn.com/v4/letter/e/aeb1de/32.png) [@EDiaz6](https://community.hubspot.com/u/EDiaz6)\
**Post date:** [October 24, 2023, 12:47am UTC](https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449/1 "2023-10-24T00:47:04Z")

</div>

In a Workflow, I’m trying to get all the contacts with an email account as an example to see how it works, however, I’m getting this error:

`TypeError: Cannot read properties of undefined (reading 'makeRequestContext')`

here’s my code:

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {

 const hubspotClient = new hubspot.Client({
 accessToken: process.env.WFInternalAPIToken
 });
 const email = event.inputFields['email'];
 let phone;
 try {
 const ApiResponse = await hubspotClient.crm.contacts.searchApi.doSearch('v3', {
 filterGroups: [
 {
 filters: [
 {
 propertyName: 'email',
 operator: 'EQ',
 value: email,
 },
 ],
 },
 ],
 });

 let contacts = ApiResponse.body.results;
 phone=contacts[0].properties.phone;
 } catch (err) {
 console.error(err);

 throw err;
 }

 callback({
 outputFields: {
 email: email,
 phone: phone
 }
 });
}

```

I’m hoping to use it with custom objects once this works, so if someone can help me out to see whats going on I would be forever grateful!

---

<div class="post-metadata">

**Author:** ![Maciek-Magic](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Maciek-Magic](https://community.hubspot.com/u/Maciek-Magic)\
**Post date:** [October 24, 2023, 9:00am UTC](https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449/2 "2023-10-24T09:00:24Z")

</div>

You are missing below code line at the begining of your script

`const hubspot = require('@hubspot/api-client');`

and authorization

```lia-code-sample
 const hubspotClient = new hubspot.Client({
 accessToken: process.env.SECRET_NAME
 });

```

it should look like that

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {

 const hubspotClient = new hubspot.Client({
 accessToken: process.env.SECRET_NAME
 }); 

const email = event.inputFields['email'];
 let phone;
 try {
 const ApiResponse = await hubspotClient.crm.contacts.searchApi.doSearch('v3', {
 filterGroups: [
 {
 filters: [
 {
 propertyName: 'email',
 operator: 'EQ',
 value: email,
 },
 ],
 },
 ],
 });

 let contacts = ApiResponse.body.results;
 phone=contacts[0].properties.phone;
 } catch (err) {
 console.error(err);

 throw err;
 }
}

```

Please refer to templates availavble here

> **[Workflows | Custom Code Actions - HubSpot docs](https://developers.hubspot.com/docs/api-reference/legacy/automation/workflow-actions/custom-code-actions)**
>
> Instructions for using custom code actions in workflows

If you are new to Custom Code step in Workflows and don’t know how to authorise please have a look on how to create Private Apps and how to use Tokens in Cusom Codes

[https://community.hubspot.com/t5/9881-Operations-Hub/Custom-Code-Get-firstname-amp-lastname-from-email-address/m-p/859925/highlight/true#M1829](https://community.hubspot.com/t5/9881-Operations-Hub/Custom-Code-Get-firstname-amp-lastname-from-email-address/m-p/859925/highlight/true#M1829)

---

<div class="post-metadata">

**Author:** ![EDiaz6](https://avatars.discourse-cdn.com/v4/letter/e/aeb1de/32.png) [@EDiaz6](https://community.hubspot.com/u/EDiaz6)\
**Post date:** [October 24, 2023, 4:51pm UTC](https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449/3 "2023-10-24T16:51:12Z")

</div>

Hi! Thanks for your reply, I do have those lines, just updated my post to show the full code, I used as a base the one you mentioned and was able to successfully retrieve values using the getbyId method, but what I want is to succesfully use the search method (code above), that’s whats giving me the error.  
My private app access token also has the custom objects scope

---

<div class="post-metadata">

**Author:** ![Maciek-Magic](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Maciek-Magic](https://community.hubspot.com/u/Maciek-Magic)\
**Post date:** [October 25, 2023, 4:04am UTC](https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449/4 "2023-10-25T04:04:09Z")

</div>

Hi,  
I made it work but with use of Axios library. It’s more readable for me and easier to troubleshoot.

```lia-code-sample
const axios = require('axios');
exports.main = async (event, callback) => {
	const email = event.inputFields['email'];
	let data = JSON.stringify({
		"filterGroups": [{
			"filters": [{
				"propertyName": "email",
				"operator": "EQ",
				"value": email
			}]
		}]
	});
	let config = {
		method: 'post',
		maxBodyLength: Infinity,
		url: 'https://api.hubapi.com/crm/v3/objects/contacts/search',
		headers: {
			'Content-Type': 'application/json',
			'Authorization': 'Bearer ' + process.env.SECRET_NAME
		},
		data: data
	};
	axios.request(config).then((response) => {
		console.log(response.data.results[0].properties.phone)
	}).catch((error) => {
		console.log(error);
	});
}

```

---

<div class="post-metadata">

**Author:** ![EDiaz6](https://avatars.discourse-cdn.com/v4/letter/e/aeb1de/32.png) [@EDiaz6](https://community.hubspot.com/u/EDiaz6)\
**Post date:** [October 25, 2023, 6:19pm UTC](https://community.hubspot.com/t/api-libraries-node-js-search-returning-an-error-workflow-custom-code-action/92449/5 "2023-10-25T18:19:59Z")

</div>

Awesome! whou rocks? you rock!! Thanks a lot!! now I can finally move on 😄
