# onPairListDevices, TypeScript and SDK3

**URL:** <https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754>\
**Category:** Developers\
**Tags:** homey-pro\
**Created:** [February 22, 2023, 3:59pm UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754 "2023-02-22T15:59:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 22, 2023, 3:59pm UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/1 "2023-02-22T15:59:47Z")

</div>

Hi,

I’m new to developing for Homey. I have experiance coding in c# and have a basis understanding of javascript and typescript. I’m however stuck in adding a driver for my app. I’m planing to develop a SmartThings integration.

I created a new project and added a driver. The implementation of my driver.ts looks like the following:  
import Homey from ‘homey’;  
import fetch, { Headers } from ‘node-fetch’;

```auto
class MyDriver extends Homey.Driver {

 async onInit() {
   this.log('MyDriver has been initialized');
 }
 async onPairListDevices() {

   //TODO: Place variables in settings
   var url = "https://api.smartthings.com/v1/devices?capability=airConditionerMode";
   var token = "<api key here>";
   this.log(url);

   var requestHeaders = new Headers();
   requestHeaders.append("Authorization", "Bearer " + token);
   requestHeaders.append("Accept", "application/json");
   requestHeaders.append("Content-Type", "application/json");

   var deviceList: any = [];
   this.log(deviceList);

   await fetch(url, { method: 'Get', headers: requestHeaders })
     .then(response => response.json())
     .then(json => {

       json.items.forEach((item: { label: any; deviceId: any; }) => {
         var jsonData = {
           'name': item.label,
           'data': {
             'id': item.deviceId
           }
         };
         this.log(jsonData);
         deviceList.push(jsonData);
       });

     })
     .catch(error => this.log(error));

   this.log(deviceList);
   return deviceList;
 }
}

```

module.exports = MyDriver;

All this.log’s are displayed, however the returned device list is empty. The api is returning data. The line this.log(jsonData) is returning data, after this.log(deviceList) is called. It’s probably an await or somethin like that. Does someone seeing what I’m doing wrong, or has a wodking example, calling an external API.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [February 22, 2023, 4:06pm UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/2 "2023-02-22T16:06:15Z")

</div>

So the last `this.log(deviceList)` is also showing the correct data? What does the data look like, can you give an example?

---

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [February 22, 2023, 10:30pm UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/3 "2023-02-22T22:30:42Z")

</div>

Is the await in combination with the .then really working?  
I would assume, that logging inside then is working, but still async.

If using await, shouldn’t the fetch promise return the result like  
let result = await fetch(…);  
let data = await result.json();

Then loop over the JSON and build the device list.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [February 23, 2023, 1:36am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/4 "2023-02-23T01:36:19Z")

</div>

> [@ats](#):
>
> or has a wodking example, calling an external API.

Marcel has a nice example (imho):

> [@How update variable using API](https://community.homey.app/t/how-update-variable-using-api/76496/2):
>
> Here some links that will help [Link1](https://community.homey.app/t/weatherdata-in-variables-openweather-and-meteoserver-with-homeyscript/36890)
> 
> [Link2](https://community.homey.app/t/homeyscript-retrieving-json-weatherdata-with-continue-and-async-function-gives-a-perpetual-loop/36706)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [February 23, 2023, 6:36am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/5 "2023-02-23T06:36:07Z")

</div>

> [@RonnyW](#):
>
> Is the await in combination with the .then really working?

Yes, it is. A `.then()` chain ultimately returns a Promise, and that’s the one being `await`’ed on. Quick PoC that you can run in Node.js locally:

```auto
void async function() {
  let productList = [];
  await fetch('https://dummyjson.com/products')
    .then(response => response.json())
    .then(json => {
      for (const product of json.products) {
        productList.push({
          id: product.id,
          title: product.title
        });
      }
    });
  console.log(productList);
}();

```

---

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 23, 2023, 10:01am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/6 "2023-02-23T10:01:05Z")

</div>

Thanks for your reply. The returned element is an empty array.

---

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 23, 2023, 10:14am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/7 "2023-02-23T10:14:04Z")

</div>

Thanks for your reply, that is not working.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [February 23, 2023, 10:17am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/8 "2023-02-23T10:17:02Z")

</div>

Can you elaborate on “not working” a bit more? And point towards the full code of your app.

---

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 23, 2023, 10:31am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/9 "2023-02-23T10:31:21Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 23, 2023, 10:34am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/10 "2023-02-23T10:34:53Z")

</div>

Hi Robert,  
Thanks again. Today I ran the same script as before, and it executed as expected. Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 23, 2023, 10:35am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/11 "2023-02-23T10:35:50Z")

</div>

Turned out the code was working. Somehow the app was not updated.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [February 23, 2023, 8:00pm UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/12 "2023-02-23T20:00:47Z")

</div>

Just a thought, I think it is nicer to give Robert the ‘solution’ credit instead of yourself 😉

---

<div class="post-metadata">

**Author:** ![ats](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ats/32/65197_2.png) [@ats](https://community.homey.app/u/ats)\
**Post date:** [February 26, 2023, 11:02am UTC](https://community.homey.app/t/onpairlistdevices-typescript-and-sdk3/76754/13 "2023-02-26T11:02:49Z")

</div>

Hi @Peter_Kawa ,  
Of course @robertklep deserves credits, as does anyone that has responded. And I can give him te solution, however my originl code was working and can be used by someone else for their implementation. How will it come clear to them, without reading the threat? Do you’ve got any suggestions?
