# Never successful making an HTTP request

**URL:** <https://community.fibery.io/t/never-successful-making-an-http-request/6081>\
**Category:** API & Programming\
**Created:** [April 14, 2024, 7:53pm UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081 "2024-04-14T19:53:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Stinson](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/adam_stinson/32/9150_2.png) [@Adam\_Stinson](https://community.fibery.io/u/Adam_Stinson)\
**Post date:** [April 14, 2024, 7:53pm UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081/1 "2024-04-14T19:53:46Z")

</div>

Multiple times I’ve tried to use the http service in an automation script, and I’ve never been able to get past unresolved promise error.

My script is just trying to create a lead in a outreach automation tool when the entity meets a certain criteria inside of Fibery.

Can someone help me debug this issue?

```auto
const http = context.getService('http');
const fibery = context.getService('fibery');

async function main() {
    // Extract necessary data from the triggering entity
    for (let i = 0; i < args.currentEntities.length; i++) {
        const companyEntity = await fibery.getEntityById('CRM/Companies', args.currentEntities[i]['Companies 1'], ['Name', 'Website']);
        const triggerEntity = args.currentEntities[i];
        const companyName = companyEntity['Name'];
        const companyWebsite = companyEntity['Website']
        const linkedinUrl = triggerEntity['Linkedin'];
        const x = triggerEntity['X'];

        console.log(companyEntity + ': ' + companyName);
        console.log(companyEntity + ': ' + companyWebsite);
        console.log(`${linkedinUrl}, ${x}`);

    //Prepare data for the LaGrowthMachine API request
        const lagrowthmachineData = {
            audience: 'VSAAS',
            companyName: companyName || '',
            companyUrl: companyWebsite || '',
            linkedinUrl: linkedinUrl,
            twitter: x,
            // Add more fields as necessary
        };

        // Make POST request to LaGrowthMachine API
        try {
            const response = await http.postAsync('https://apiv2.lagrowthmachine.com/flow/leads?apikey=Your-external-apikey', {
                body: lagrowthmachineData,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });

            console.log('Lead added to LaGrowthMachine successfully:', response);
        } catch (error) {
            console.error('Error adding lead to LaGrowthMachine:', error);
        }
    }
};

main();

```

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [April 14, 2024, 8:15pm UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081/2 "2024-04-14T20:15:54Z")

</div>

Is there a reason why you are declaring `main` and then calling it?  
You could just have the for loop followed by the POST request.

Anyway, I think this is not correct:

```auto
for (let i = 0; i < args.currentEntities.length; i++) {
        const companyEntity = await fibery.getEntityById('CRM/Companies', args.currentEntities[i]['Companies 1'], ['Name', 'Website']);
        const triggerEntity = args.currentEntities[i];

```

Assuming that the ‘company’ entity is linked to the ‘trigger’ entity via a to-one relation called `Companies 1`, then I think you would be better off doing this instead:

```auto
for (const triggerEntity of args.CurrentEntities) {
        const companyEntity = await fibery.getEntityById('CRM/Companies', triggerEntity['Companies 1']['Id'], ['Name', 'Website']);

```

If Companies 1 is a to-many relation, then you need to rethink what you’re trying to do, since there could be more than one company linked to a trigger entity.

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [April 14, 2024, 10:20pm UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081/3 "2024-04-14T22:20:42Z")

</div>

Simple solution - since `main()` is async, you need to call `await main()`

---

<div class="post-metadata">

**Author:** ![Adam\_Stinson](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/adam_stinson/32/9150_2.png) [@Adam\_Stinson](https://community.fibery.io/u/Adam_Stinson)\
**Post date:** [April 14, 2024, 10:27pm UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081/4 "2024-04-14T22:27:46Z")

</div>

Ahh thank you. Chalk it up to no being good at programming. Appreciate the insight.

---

<div class="post-metadata">

**Author:** ![Adam\_Stinson](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/adam_stinson/32/9150_2.png) [@Adam\_Stinson](https://community.fibery.io/u/Adam_Stinson)\
**Post date:** [April 14, 2024, 10:32pm UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081/6 "2024-04-14T22:32:14Z")

</div>

It is actually a to many relationship, though 99% of cases there is only 1 entity linked. In every case I’m using this script I would be fine with calling the 1 item in the array. So should I always reference the [0] when getting the entity?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [April 15, 2024, 8:50am UTC](https://community.fibery.io/t/never-successful-making-an-http-request/6081/7 "2024-04-15T08:50:51Z")

</div>

If you want to get info from a to-many relationship, you have to explicitly request it (it is not included in the entity by default) so you would probably need something like this:

```auto
for (const triggerEntity of args.CurrentEntities) {
    const triggerEntityPlus = await fibery.getEntityById(triggerEntity.type, triggerEntity.id, ['Companies 1']);
    const companyEntity = await fibery.getEntityById('CRM/Companies', triggerEntityPlus['Companies 1'][0]['Id'], ['Name', 'Website']);

```
