⚙ Operations Hub

Wakira
Participant

Custom Code workflow - Call External API

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

 

5 Replies 5
RVerma4
Member

Custom Code workflow - Call External API

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 

0 Upvotes
PamCotton
Community Manager
Community Manager

Custom Code workflow - Call External API

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


Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success.
Don't miss this opportunity to connect and grow—reserve your spot today!

Wakira
Participant

Custom Code workflow - Call External API

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):

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}');
    });
};

 

0 Upvotes
stefen
Key Advisor | Partner
Key Advisor | Partner

Custom Code workflow - Call External API

@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:


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);
        }
    });
Stefen Phelps, Community Champion, Kelp Web Developer
0 Upvotes
Wakira
Participant

Custom Code workflow - Call External API

 @LMartinez  @bryce-corey   @stefen @Pam Do you have any guidance or help on how I can resolve this?

thanks

0 Upvotes