{"id":3584,"date":"2023-04-12T15:39:33","date_gmt":"2023-04-12T15:39:33","guid":{"rendered":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/"},"modified":"2023-04-12T15:39:33","modified_gmt":"2023-04-12T15:39:33","slug":"gather-data-from-an-api-using-the-script-source-node","status":"publish","type":"post","link":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/","title":{"rendered":"Gather data from an API using the Script source node"},"content":{"rendered":"<p>The following article outlines how Euclid Studio can be used to request data from an API and process it so it can be used in the system. This requires some knowledge of Python coding that is used within the Script source node and how APIs function.<\/p>\n<p>APIs usually have associated documentation that describes how to access them. Most APIs have an associated Authentication or API Key that is needed to query the information.<\/p>\n<p>This example uses the OpenWeather API to get historical weather information for a specific day and location. To be able to access this specific API, only a free OpenWeather account is needed. Once an account is made, an API Key is provided to the account. More information on OpenWeather APis can be found here: <a href=\"https:\/\/openweathermap.org\/api\">https:\/\/openweathermap.org\/api<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-1843\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg\" alt=\"\" width=\"326\" height=\"304\" \/><\/p>\n<p>In Euclid Studio, add a Script source node to the flow. Once added, open the node, change the Script Type to Python, and click Rebuild.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1833 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP1-min.jpg\" alt=\"\" width=\"624\" height=\"214\" \/><\/p>\n<p>With the node set to use a Python script, the python code can be entered. This code will vary for each API, as the data received from each would differ. Refer to an API\u2019s documentation for more information. No matter the API used, there are key parts to the code that are needed. The full example code is available at the end of the document.<\/p>\n<p>At the top of the code, it is required to import the needed packages for the script to use. The requests, json, and pandas packages are required for this to work. In my example, I am also using the datetime package to include a needed date conversion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1834 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP2-min.jpg\" alt=\"\" width=\"148\" height=\"78\" \/><\/p>\n<p>Directly below this, the APIKEY, LAT, and LON variables are set. The LAT and LON in the example are the Latitude and Longitude that will be used in the API request to determine what location of weather data to request. The APIKEY variable is populated with the personal APIKEY associated with an OpenWeather account. These variables will be passed into the API request, and could be replaced with Euclid Studio parameters if desired. This information is obfuscated in the full code at the end of this document.<\/p>\n<p>Then the main function is defined. In the main function, a \u201cyesterday_unix_timestamp\u201d variable is set based on the current day, as the OpenWeather API required a unix timestamp to determine what day of data to return.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1835 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP3-min.jpg\" alt=\"\" width=\"411\" height=\"105\" \/><\/p>\n<p>Then, the requests package is used to send a GET request to the OpenWeather API. In this request, there are variables used to populate the information that the API needs to determine what data to send as a result. To ensure the result usable by python, the json package is used to convert the JSON result into a python object.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1836 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP4-min.jpg\" alt=\"\" width=\"624\" height=\"24\" \/><\/p>\n<p>The JSON result returns a lot of information. To keep this example simple, only two pieces of information from the result are going to be used. These are the Date in local time, and the Temperature at that time. To prepare to store this data, empty lists are created to store the date and temperature.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1837 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP5-min.jpg\" alt=\"\" width=\"429\" height=\"56\" \/><\/p>\n<p>Now these lists need to be populated by the information returned from the API request. Finding the exact information can be difficult based on the structure of the data that the API returns. In this example, the JSON returns a list that contains weather information for each hour of the requested day. Knowing this, the code should look at the information for each hour and then add that to the currently empty lists. For the date_local list, the returned unix timestamp is first converted to a human readable string before adding it. The temperature is added to the list without alteration.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1838 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP6-min.jpg\" alt=\"\" width=\"624\" height=\"57\" \/><\/p>\n<p>Now that the lists are populated with 24 hours of data, they need to be combined into a python dictionary.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1839 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP7-min.jpg\" alt=\"\" width=\"276\" height=\"83\" \/><\/p>\n<p>Then, using the pandas package, the dictionary is transformed into a dataframe with defined columns. This dataframe is then exported in a csv file in the desired location.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1840 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP8-min.jpg\" alt=\"\" width=\"613\" height=\"80\" \/><\/p>\n<p>At the very end of the code, the main() function is called.<\/p>\n<p>When previewing the results of the code the expected 24 rows of data are returned. Don\u2019t worry about the high temperatures, the API provides the information in Kelvin. The hours also show information from different days due to timezone offsets, as the requested data is provided using UTC.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1841 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP9-min.jpg\" alt=\"\" width=\"772\" height=\"290\" \/><\/p>\n<p>Now that the desired information is being output to a csv file, it can be used as needed with a Flat File source node.<\/p>\n<p>Aiming for simplicity in this explanation, the example lacks error catching that should be present in a production script. Some common errors that should be checked for and considered in the script include the following:<\/p>\n<ul>\n<li>Ensure the API request returns the correct response code<\/li>\n<li>Check the dataframe for NULL values if not supported<\/li>\n<li>Ensure list values are unique where needed<\/li>\n<li>Ensure timestamps return valid values<\/li>\n<li>Ensure dataframe values are valid<\/li>\n<\/ul>\n<p>Full Example Code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1842 alignnone\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/AP10-min.jpg\" alt=\"\" width=\"624\" height=\"348\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following article outlines how Euclid Studio can be used to request data from an API and process it so it can be used in the system. This requires some knowledge of Python coding that is used within the Script source node and how APIs function. APIs usually have associated documentation that describes how to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27],"tags":[33],"class_list":["post-3584","post","type-post","status-publish","format-standard","hentry","category-quebit-value","tag-euclid-studio"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Gather data from an API using the Script source node - QueBIT<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Gather data from an API using the Script source node - QueBIT\" \/>\n<meta property=\"og:description\" content=\"The following article outlines how Euclid Studio can be used to request data from an API and process it so it can be used in the system. This requires some knowledge of Python coding that is used within the Script source node and how APIs function. APIs usually have associated documentation that describes how to&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/\" \/>\n<meta property=\"og:site_name\" content=\"QueBIT\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-12T15:39:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg\" \/>\n<meta name=\"author\" content=\"agoddard\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"agoddard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/\"},\"author\":{\"name\":\"agoddard\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\"},\"headline\":\"Gather data from an API using the Script source node\",\"datePublished\":\"2023-04-12T15:39:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/\"},\"wordCount\":825,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/1AP-min.jpg\",\"keywords\":[\"Euclid Studio\"],\"articleSection\":[\"QueBIT Value\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/\",\"name\":\"Gather data from an API using the Script source node - QueBIT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/1AP-min.jpg\",\"datePublished\":\"2023-04-12T15:39:33+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#primaryimage\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/1AP-min.jpg\",\"contentUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/1AP-min.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/gather-data-from-an-api-using-the-script-source-node\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Gather data from an API using the Script source node\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#website\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/\",\"name\":\"QueBIT\",\"description\":\"QueBIT\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\",\"name\":\"agoddard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"caption\":\"agoddard\"},\"sameAs\":[\"https:\\\/\\\/quebit.com\\\/askquebit\"],\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/author\\\/agoddard\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Gather data from an API using the Script source node - QueBIT","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/","og_locale":"en_US","og_type":"article","og_title":"Gather data from an API using the Script source node - QueBIT","og_description":"The following article outlines how Euclid Studio can be used to request data from an API and process it so it can be used in the system. This requires some knowledge of Python coding that is used within the Script source node and how APIs function. APIs usually have associated documentation that describes how to&hellip;","og_url":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/","og_site_name":"QueBIT","article_published_time":"2023-04-12T15:39:33+00:00","og_image":[{"url":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg","type":"","width":"","height":""}],"author":"agoddard","twitter_card":"summary_large_image","twitter_misc":{"Written by":"agoddard","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#article","isPartOf":{"@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/"},"author":{"name":"agoddard","@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084"},"headline":"Gather data from an API using the Script source node","datePublished":"2023-04-12T15:39:33+00:00","mainEntityOfPage":{"@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/"},"wordCount":825,"commentCount":0,"image":{"@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#primaryimage"},"thumbnailUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg","keywords":["Euclid Studio"],"articleSection":["QueBIT Value"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/","url":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/","name":"Gather data from an API using the Script source node - QueBIT","isPartOf":{"@id":"https:\/\/quebit.com\/askquebit\/#website"},"primaryImageOfPage":{"@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#primaryimage"},"image":{"@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#primaryimage"},"thumbnailUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg","datePublished":"2023-04-12T15:39:33+00:00","author":{"@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084"},"breadcrumb":{"@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#primaryimage","url":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg","contentUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2021\/10\/1AP-min.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/quebit.com\/askquebit\/gather-data-from-an-api-using-the-script-source-node\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/quebit.com\/askquebit\/"},{"@type":"ListItem","position":2,"name":"Gather data from an API using the Script source node"}]},{"@type":"WebSite","@id":"https:\/\/quebit.com\/askquebit\/#website","url":"https:\/\/quebit.com\/askquebit\/","name":"QueBIT","description":"QueBIT","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/quebit.com\/askquebit\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084","name":"agoddard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","caption":"agoddard"},"sameAs":["https:\/\/quebit.com\/askquebit"],"url":"https:\/\/quebit.com\/askquebit\/author\/agoddard\/"}]}},"_links":{"self":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/comments?post=3584"}],"version-history":[{"count":0,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3584\/revisions"}],"wp:attachment":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/media?parent=3584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/categories?post=3584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/tags?post=3584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}