# Calling an API from Homey Script

**URL:** <https://community.homey.app/t/calling-an-api-from-homey-script/106230>\
**Category:** Questions & Help\
**Tags:** homey-pro\
**Created:** [March 14, 2024, 2:54am UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230 "2024-03-14T02:54:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Bittner](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_bittner/32/16698_2.png) [@Peter\_Bittner](https://community.homey.app/u/Peter_Bittner)\
**Post date:** [March 14, 2024, 2:54am UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230/1 "2024-03-14T02:54:52Z")

</div>

Hi, I want to call an API from Amber Electric (Australian electricity retailer who sells at wholesale prices) from Homey Script. I know the API works as I can test it in Ambers Github space, and get a result returned. They gave me a script to try it with but I get an error when I test it.

Here is the script:

```auto
const apiKey = 'psk_f31a4b0b3ee67b8db63067a7d0385bd0'
const siteId = '01H92N0GC70BAZHM48P2C52J3M'

const response = await fetch(`https://api.amber.com.au/v1/sites/${siteId}/prices/current`, { 
  headers: { 
    'Authorization': `Bearer ${apiKey}` 
  } 
}
const prices = await response.json()
const generalPrice = prices.find((price) => price.channelType === 'general')?.perKwh
const feedInPrice = prices.find((price) => price.channelType === 'feedIn')?.perKwh

```

Here is the error:  
❌ Script Error  
⚠ Get Current FIT.js:8  
}  
^

SyntaxError: missing ) after argument list  
at new Script (node:vm:94:7)  
at HomeyScriptApp.runScript (/app/app.js:483:23)  
at process.processTicksAndRejections (node:internal/process/task\_queues:95:5)  
at async Object.runScript (/app/api.js:30:22)

Here is a copy of part of the response when calling the API:

 ![Screenshot 2024-03-14 at 1.53.13 pm](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/f/c/fc35a6b601992bf9d855ffb8875cc2251fc8d01e.png)

---

<div class="post-metadata">

**Author:** ![Jonathan\_Draper](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jonathan_draper/32/33978_2.png) [@Jonathan\_Draper](https://community.homey.app/u/Jonathan_Draper)\
**Post date:** [March 14, 2024, 7:31am UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230/6 "2024-03-14T07:31:56Z")

</div>

I think it’s just like the syntax error says - the open bracket ( after fetch doesn’t have a matching close bracket?

So I think the script should read:

```auto
const apiKey = 'psk_f31a4b0b3ee67b8db63067a7d0385bd0'
const siteId = '01H92N0GC70BAZHM48P2C52J3M'

const response = await fetch(`https://api.amber.com.au/v1/sites/${siteId}/prices/current`, { 
  headers: { 
    'Authorization': `Bearer ${apiKey}` 
  } 
});
const prices = await response.json()
const generalPrice = prices.find((price) => price.channelType === 'general')?.perKwh
const feedInPrice = prices.find((price) => price.channelType === 'feedIn')?.perKwh

```

You then need to add something to the script to do something with the responses so you can see the output.

---

<div class="post-metadata">

**Author:** ![diapolon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/diapolon/32/82612_2.png) [@diapolon](https://community.homey.app/u/diapolon)\
**Post date:** [March 14, 2024, 10:23am UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230/7 "2024-03-14T10:23:50Z")

</div>

```auto
const response = await fetch(`https://api.amber.com.au/v1/sites/${siteId}/prices/current`, { 
  headers: { 
    'Authorization': `Bearer ${apiKey}` 
  } 
})

```

is missed a **“)”**

---

<div class="post-metadata">

**Author:** ![Peter\_Bittner](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_bittner/32/16698_2.png) [@Peter\_Bittner](https://community.homey.app/u/Peter_Bittner)\
**Post date:** [March 14, 2024, 10:54am UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230/8 "2024-03-14T10:54:19Z")

</div>

Thanks. Initially I put the ) in the wrong place, but when I added ); in the place you indicated it worked.  
Now, as you alluded to I get Return Undefined.  
I added a log function to display generalPrice and feedInPrice and they now display.  
I want to run this from a flow and put the responses into variables.  
If you have some advise to share on that it would be appreciated, otherwise I will search the forum and try and work it out, then come back to you if I can’t.

Thank you all again. This community is magic.

---

<div class="post-metadata">

**Author:** ![diapolon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/diapolon/32/82612_2.png) [@diapolon](https://community.homey.app/u/diapolon)\
**Post date:** [March 14, 2024, 11:00am UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230/9 "2024-03-14T11:00:45Z")

</div>

add

```auto
return true 

```

and you don’t have undefined

Use log() function for debugging

```auto
log(variable_to_print)

```

---

<div class="post-metadata">

**Author:** ![Jonathan\_Draper](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jonathan_draper/32/33978_2.png) [@Jonathan\_Draper](https://community.homey.app/u/Jonathan_Draper)\
**Post date:** [March 14, 2024, 12:41pm UTC](https://community.homey.app/t/calling-an-api-from-homey-script/106230/10 "2024-03-14T12:41:08Z")

</div>

> [@How to read and Set the logic variables with HomeyScript?](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520):
>
> Hi, I am writing a script to set my screens on a certain percentage. Therefore I need to set/read the new 2.0 logic variables from Homey Script. Does anybody know how to do this? Kind regards, Marcel
