# Error while using custom code trying to format US phone numbers

**URL:** <https://community.hubspot.com/t/error-while-using-custom-code-trying-to-format-us-phone-numbers/54132>\
**Category:** Data Hub\
**Tags:** data-quality-automation\
**Created:** [March 30, 2022, 9:43pm UTC](https://community.hubspot.com/t/error-while-using-custom-code-trying-to-format-us-phone-numbers/54132 "2022-03-30T21:43:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yenma\_H](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/yenma_h/32/5125_2.png) [@Yenma\_H](https://community.hubspot.com/u/Yenma_H)\
**Post date:** [March 30, 2022, 9:43pm UTC](https://community.hubspot.com/t/error-while-using-custom-code-trying-to-format-us-phone-numbers/54132/1 "2022-03-30T21:43:06Z")

</div>

Hello! I could use your help. I am trying to use the custom code to update US phone numbers. I’m trying to use this code: [https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/format\_phone.js](https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/format_phone.js)

Unfortunately I’m getting an authentication issue, even though I have the proper access to the API key. I’m wondering if I’m missing something.

I would appreciate any insights or guidance. Thank you so much!

Here is the error that I’m receiving:

`2022-03-30T21:30:42.009Z	ERROR	Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"HttpError: HTTP request failed","reason":{"errorType":"HttpError","errorMessage":"HTTP request failed","response":{"statusCode":401,"body":{"status":"error","message":"Authentication credentials not found. This API supports both API Key and OAuth 2.0 authentication and you can find more details at https://developers.hubspot.com/docs/methods/auth/oauth-overview","correlationId":"8e8cb121-ac32-4e5a-95fd-e571699295ab","category":"INVALID_AUTHENTICATION"},"headers":{"date":"Wed, 30 Mar 2022 21:30:41 GMT","content-type":"application/json;charset=utf-8","content-length":"316","connection":"close","cf-ray":"6f43f2abbc2a6f98-IAD","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"Accept-Encoding","cf-cache-status":"DYNAMIC","access-control-allow-credentials":"false","expect-ct":"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"","x-hubspot-correlation-id":"8e8cb121-ac32-4e5a-95fd-e571699295ab","x-trace":"2B800DF8395E017FB7199AB6DCE66490193D6717FB000000000000000000","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=mddbyZg3j7FEMPQ2LdNrCqU1PRzsp0nj3E6LbmL6ovnBHOgq8nedgXyPe%2BMzrFpuZdastWD9doWHCtG6JdOXLMiYW8kqt6Edo%2BZ8FLgpSAOCCltfHAX47mBXYLBffrOn\"}],\"group\":\"cf-nel\",\"max_age\":604800}","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"api.hubapi.com","port":null,"hostname":"api.hubapi.com","hash":null,"search":"?properties=phone","query":"properties=phone","pathname":"/crm/v3/objects/contacts/2926151","path":"/crm/v3/objects/contacts/2926151?properties=phone","href":"https://api.hubapi.com/crm/v3/objects/contacts/2926151?properties=phone"},"method":"GET","headers":{"User-Agent":"hubspot-api-client-nodejs; 3.4.1","Accept":"application/json"}}},"body":{},"statusCode":401,"name":"HttpError","stack":["HttpError: HTTP request failed"," at Request._callback (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/contacts/api/basicApi.js:318:40)"," at Request.self.callback (/opt/nodejs/node_modules/request/request.js:185:22)"," at Request.emit (events.js:314:20)"," at Request.<anonymous> (/opt/nodejs/node_modules/request/request.js:1154:10)"," at Request.emit (events.js:314:20)"," at IncomingMessage.<anonymous> (/opt/nodejs/node_modules/request/request.js:1076:12)"," at Object.onceWrapper (events.js:420:28)"," at IncomingMessage.emit (events.js:326:22)"," at endReadableNT (_stream_readable.js:1241:12)"," at processTicksAndRejections (internal/process/task_queues.js:84:21)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: HttpError: HTTP request failed"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}`

---

<div class="post-metadata">

**Author:** ![LMeert](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lmeert/32/103847_2.png) [@LMeert](https://community.hubspot.com/u/LMeert)\
**Post date:** [April 5, 2022, 12:40pm UTC](https://community.hubspot.com/t/error-while-using-custom-code-trying-to-format-us-phone-numbers/54132/2 "2022-04-05T12:40:58Z")

</div>

Hi [@Yenma\_H](https://community.hubspot.com/t5/user/viewprofilepage/user-id/222506),

I tried to run the script on my end and I had the same authentication issue.  
Here’s how to troubleshoot :

- Make sure you add the API key as a secret on your node

 ![LMeert_0-1649162308152.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/1/11414c757e283e4757f6278d910e043508fbced0.png)

- Also, you will have to change the name of the variable in the code line 13 :  
apiKey : process.end.HAPI\_Key

And you should be good to go !  
If this helped, consider marking this as a solution 🙂  
Have a good day,

Ludwig

---

<div class="post-metadata">

**Author:** ![Yenma\_H](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/yenma_h/32/5125_2.png) [@Yenma\_H](https://community.hubspot.com/u/Yenma_H)\
**Post date:** [April 5, 2022, 2:05pm UTC](https://community.hubspot.com/t/error-while-using-custom-code-trying-to-format-us-phone-numbers/54132/3 "2022-04-05T14:05:24Z")

</div>

Thank you so much! I did end up figuring it out after some trial and error. This was the solution I used. I appreciate your help!
