# Custom Code workflow - Call External API

**URL:** <https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254>\
**Category:** Data Hub\
**Tags:** workflow-extension\
**Created:** [April 19, 2022, 7:46pm UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254 "2022-04-19T19:46:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wakira](https://avatars.discourse-cdn.com/v4/letter/w/cc9497/32.png) [@Wakira](https://community.hubspot.com/u/Wakira)\
**Post date:** [April 19, 2022, 7:46pm UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254/1 "2022-04-19T19:46:47Z")

</div>

Hi ,

I try call a external api(third party) in mi workflow using custom code, but its not working.

I get this message:

“ERROR Unhandled Promise Rejection”

Would it be possible to make an external call for this functionality?  
Thanks

---

<div class="post-metadata">

**Author:** ![PamCotton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pamcotton/32/47916_2.png) [@PamCotton](https://community.hubspot.com/u/PamCotton)\
**Post date:** [April 20, 2022, 6:21pm UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254/2 "2022-04-20T18:21:44Z")

</div>

Hello @Wakira could you please provide us with more details on your custom code without any private information? This way we will be able to investigate more about it and provide you with the next steps.

I would also like to invite our top experts @LMartinez7 @bryce-corey @stefen any recommendations to @Wakira?

Thank you,

Pam

---

<div class="post-metadata">

**Author:** ![Wakira](https://avatars.discourse-cdn.com/v4/letter/w/cc9497/32.png) [@Wakira](https://community.hubspot.com/u/Wakira)\
**Post date:** [April 20, 2022, 6:28pm UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254/3 "2022-04-20T18:28:26Z")

</div>

HI @PamCotton , thanks for yout attention.  
The process is basically, we use a ticket as a control to monitor the delivery of one of our products, in this ticket there is a property that is the tracking code that needs to be consulted constantly in the carrier’s api and update the ticket status property.  
**I tried something similar to this (with changed values):**

```lia-code-sample
const axios = require('axios')

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

 const headers = {
 'Authorization': 'Token 1234',
 'Content-Type': 'application/json'
 };

 const requestBody = {
 "data": {
 "df": 
 { "nf" : "11222", 
 "tpDocumento":1 
 } 
}
 }

 axios
 .post(
 'www.APICarrier.com/api/tracking',
 requestBody,
 { headers: headers }
 )
 .then(response => {
 console.log('Response from API: ${response.body}');
 });
};

```

---

<div class="post-metadata">

**Author:** ![Wakira](https://avatars.discourse-cdn.com/v4/letter/w/cc9497/32.png) [@Wakira](https://community.hubspot.com/u/Wakira)\
**Post date:** [April 28, 2022, 1:34pm UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254/4 "2022-04-28T13:34:39Z")

</div>

[@LMartinez](https://community.hubspot.com/t5/user/viewprofilepage/user-id/187447)@bryce-corey@stefen @Pam Do you have any guidance or help on how I can resolve this?  
thanks

---

<div class="post-metadata">

**Author:** ![stefen](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stefen/32/29274_2.png) [@stefen](https://community.hubspot.com/u/stefen)\
**Post date:** [September 6, 2022, 12:58pm UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254/5 "2022-09-06T12:58:42Z")

</div>

@Wakira sounds like you need to see what the actual error is. Typically a “unhandled promise rejection” means you aren’t handling a failed response. To do that you should be able to update your axios function to something like this:

```lia-code-sample
axios
 .post(
 'www.APICarrier.com/api/tracking',
 requestBody,
 { headers: headers }
 )
 .then(response => {
 console.log('Response from API: ${response.body}');
 }).catch(function (error) {
 if (error.response) {
 // Request made and server responded
 console.log(error.response.data);
 console.log(error.response.status);
 console.log(error.response.headers);
 } else if (error.request) {
 // The request was made but no response was received
 console.log(error.request);
 } else {
 // Something happened in setting up the request that triggered an Error
 console.log('Error', error.message);
 }
 });

```

---

<div class="post-metadata">

**Author:** ![RVerma4](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@RVerma4](https://community.hubspot.com/u/RVerma4)\
**Post date:** [September 5, 2022, 8:34am UTC](https://community.hubspot.com/t/custom-code-workflow-call-external-api/55254/6 "2022-09-05T08:34:17Z")

</div>

Deal assiciated with company andcompany associated with contact then company contact should associate with deal how I can do this with custom code in workflows @Wakira @stefen
