# Write Analysis to Get API call From the weather app and display the information on a widget

**URL:** https://community.tago.io/t/write-analysis-to-get-api-call-from-the-weather-app-and-display-the-information-on-a-widget/1567
**Category:** Analysis and Actions
**Created:** [April 11, 2024, 6:48am UTC](https://community.tago.io/t/write-analysis-to-get-api-call-from-the-weather-app-and-display-the-information-on-a-widget/1567 "2024-04-11T06:48:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![romeroj](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@romeroj](https://community.tago.io/u/romeroj)
#### Post date: [April 11, 2024, 6:48am UTC](https://community.tago.io/t/write-analysis-to-get-api-call-from-the-weather-app-and-display-the-information-on-a-widget/1567/1 "2024-04-11T06:48:41Z")

</div>

Hello! I am having a hard time writing the code for the analysis in extracting the API information from the weather app. I know I will need to store the data in the bucket, and have the API Key for the weather data, but knowing the parameters of which type of SDK I would need is confusing.

---

<div class="post-metadata">

### Author: ![octaviosanchez.alumn](https://avatars.discourse-cdn.com/v4/letter/o/ec9cab/32.png) [@octaviosanchez.alumn](https://community.tago.io/u/octaviosanchez.alumn)
#### Post date: [July 1, 2024, 6:39am UTC](https://community.tago.io/t/write-analysis-to-get-api-call-from-the-weather-app-and-display-the-information-on-a-widget/1567/2 "2024-07-01T06:39:49Z")

</div>

Did you have any solution?

---

<div class="post-metadata">

### Author: ![julihermes.melo](https://yyz2.discourse-cdn.com/flex034/user_avatar/community.tago.io/julihermes.melo/32/1625_2.png) [@julihermes.melo](https://community.tago.io/u/julihermes.melo)
#### Post date: [July 1, 2024, 2:42pm UTC](https://community.tago.io/t/write-analysis-to-get-api-call-from-the-weather-app-and-display-the-information-on-a-widget/1567/3 "2024-07-01T14:42:04Z")

</div>

I did exactly this on a customer.

My analysis look like this:

```auto
const { Analysis, Device, Utils } = require('@tago-io/sdk'); const axios = require('axios'); async function ImportDayForecast(context) { const envVars = Utils.envToJson(context.environment); if (!envVars.device_token) return context.log('Missing device_token environment variable'); const device = new Device({ token: envVars.device_token }); const options = { url: 'Put your API URL here', method: 'GET', }; try { const result = await axios(options); // Some validations, this need to be done by you if (!result.data.hasOwnProperty('data')) return console.log(`No property "data" in API response.`); if (result.data.data.length === 0) return console.log(`Property "data" empty in API response.`); // This code below delete old data, because I dont want save history, if you want to save history, dont use this code await device.deleteData({ variable: 'day_forecast', qty: 10000, }); // Here I customize the response to my application // I omit some irrelevant code in [...] const forecasts = result.data.data.reverse().map((item) => { const forecast = item; [...] return forecast; }); let count = 0; // Here I save new data. // I using metadata to save some Data storage, if you dont use this technique, just save in normal way // I omit some irrelevant code [...] for await (const forecast of forecasts) { try { await device.sendData({ variable: 'day_forecast', value: '', metadata: { temperature: `Min: ${forecast.temperature.min}°C - Max: ${forecast.temperature.max}°C`, humidity: `Min: ${forecast.humidity.min}% - Max: ${forecast.humidity.max}%`, [...] }, }); count += 1; } catch (error) { context.log(`Error when inserting: (${forecast.date_br})`, error); } } context.log(`Successfully Inserted ${count} rows.`); } catch (error) { return context.log(`Error: ${error}`); } } module.exports = new Analysis(ImportDayForecast);

```

I comment the file to explain.

With the data saved in a bucket, you can do anything in dashboards.

Hope this help you.
