{"id":16310,"date":"2021-09-14T02:32:42","date_gmt":"2021-09-13T18:32:42","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16310"},"modified":"2024-07-03T17:32:45","modified_gmt":"2024-07-03T09:32:45","slug":"the-difference-between-rest-api-and-tej-api","status":"publish","type":"insight","link":"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/","title":{"rendered":"The Difference between REST API and TEJ API"},"content":{"rendered":"\n<p>All roads lead to Rome<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_0PdLFuzExcNsyULa3.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo Creds:&nbsp;<a href=\"https:\/\/unsplash.com\/photos\/6rkJD0Uxois\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a12048ef346f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a12048ef346f\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Highlights\" >Highlights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#The_Editing_Environment_and_Modules_Required\" >The Editing Environment and Modules Required<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Database_Used\" >Database Used<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Main_Difference\" >Main Difference<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Case1_Obtain_Incomplete_Stock_Price\" >Case1: Obtain Incomplete Stock Price<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Method_A_REST_API\" >Method A: REST API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Method_B_TEJ_API\" >Method B: TEJ API<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Case2_Obtain_Complete_Stock_Price_Data\" >Case2: Obtain Complete Stock Price Data<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Method_A_REST_API-2\" >Method A: REST API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Method_B_TEJ_API-2\" >Method B: TEJ API<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Case3_Obtain_Complete_Financial_Data\" >Case3: Obtain Complete Financial Data<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Method_A_REST_API-3\" >Method A: REST API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Method_B_TEJ_API-3\" >Method B: TEJ API<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/the-difference-between-rest-api-and-tej-api\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"afe2\"><span class=\"ez-toc-section\" id=\"Highlights\"><\/span><strong>Highlights<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2606\u2606\u2606\u2606<\/li>\n\n\n\n<li>Obtain stock price and financial data in different ways<\/li>\n\n\n\n<li>Advice: This article aims to find out the difference and similarity between REST API and TEJ API in terms of data obtaining and data processing, in order to help readers choose the way to access to database according to personal preference. Before reading this article, I highly recommend readers to check both&nbsp;<a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/introduction-1-building-your-own-database-through-tej-api-493529d138da\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">\u3010Introduction(1)\u3011Building your own database through TEJ API<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/medium.com\/tej-api-%E9%87%91%E8%9E%8D%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90\/introduction-4-tej-rest-api-document-62604989797f\" target=\"_blank\" rel=\"noopener\">\u3010Introduction(4)\u3011TEJ Rest API Document<\/a>&nbsp;to grasp the overall idea of two methods.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4c27\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span><strong>Preface<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"ef4d\">TEJ offers different ways to access to TEJ database through programming languages, such as Python-based&nbsp;<a href=\"https:\/\/api.tej.com.tw\/document_rest.html\" rel=\"noreferrer noopener\" target=\"_blank\">REST API<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/api.tej.com.tw\/document_python.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a>, R-based&nbsp;<a href=\"https:\/\/api.tej.com.tw\/document_r.html\" rel=\"noreferrer noopener\" target=\"_blank\">R API<\/a>&nbsp;or NET-based&nbsp;<a href=\"https:\/\/api.tej.com.tw\/document_net.html\" rel=\"noreferrer noopener\" target=\"_blank\">.Net API<\/a>. Even though the mechanisms behind these are a bit different, the logic of getting data and the way to set input parameter share many similarities, which enable users to turn to different methods more flexibly. Therefore, this week, we are about to compare REST API to TEJ API and see what suits you the most !<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4b7f\"><span class=\"ez-toc-section\" id=\"The_Editing_Environment_and_Modules_Required\"><\/span><strong>The Editing Environment and Modules Required<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"d5f1\">Windows OS and Jupyter Notebook<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#Basic modules required <br>import pandas as pd<br>import matplotlib.pyplot as plt<br>import numpy as np#Modules required by REST API <br>import requests, json#Modules required by TEJ API <br>import tejapi<br>tejapi.ApiConfig.api_key = 'Your Key'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"443d\"><span class=\"ez-toc-section\" id=\"Database_Used\"><\/span><strong>Database Used<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=42\" rel=\"noreferrer noopener\" target=\"_blank\">Unadjusted Stock Price<\/a>: the code of the database is \u2018TWN\/APRCD\u2019, containing the unadjusted stock price of listed firms<\/li>\n\n\n\n<li><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=62\" rel=\"noreferrer noopener\" target=\"_blank\">Consolidated financial statements (cumulative)<\/a>: the code of the database is \u2018TWN\/AIM1A\u2019, covering cumulative financial data of all firms<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"83e1\"><span class=\"ez-toc-section\" id=\"Main_Difference\"><\/span><strong>Main Difference<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1pPN_-dBZ3mET00hbVm5gBQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a4b7\"><span class=\"ez-toc-section\" id=\"Case1_Obtain_Incomplete_Stock_Price\"><\/span><strong>Case1: Obtain Incomplete Stock Price<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"d7a7\"><span class=\"ez-toc-section\" id=\"Method_A_REST_API\"><\/span><strong>Method A: REST API<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modify the website link and load the content<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">key = \"&amp;api_key=\" + \"Your Key\" <br>database = 'TWN\/APRCD'                               <br>coid = \"&amp;coid=\" + \"2454,3034,2379\"                    <br>date_start = \"&amp;mdate.gte=\" + \"2000-01-01\"           <br>date_end = \"&amp;mdate.lte=\" + \"2020-12-31\"         <br>columns = \"&amp;opts.columns=\"+ \"coid,mdate,open_d,high_d,low_d,close_d\" <br>paginate = ''<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"0206\"><em>key: the key to database, please change \u2018Your Key\u2019 into the key you get after purchasing the database<\/em><\/p>\n\n\n\n<p id=\"ae6a\"><em>database: the code of database, here we choose Unadjusted Stock Price<\/em><\/p>\n\n\n\n<p id=\"2465\"><em>coid: the code of the stock, we select these three (2454\u30013034\u30012379) firms. If you rather choose all firms, assign empty string to coid&nbsp;<\/em><code><em>coid = ''<\/em><\/code><\/p>\n\n\n\n<p id=\"8df9\"><em>date_start: the starting date, if there\u2019s no limitation set on it, also assign empty string&nbsp;<\/em><code><em>date_start = ''<\/em><\/code><\/p>\n\n\n\n<p id=\"5289\"><em>date_end: the ending date, if there\u2019s no limitation set on it, also assign empty string&nbsp;<\/em><code><em>date_end = ''<\/em><\/code><\/p>\n\n\n\n<p id=\"ba24\"><em>columns: selected columns, the code of columns can be viewed in the column description of that database. If you want all columns, just assign empty string&nbsp;<\/em><code><em>columns = ''<\/em><\/code><\/p>\n\n\n\n<p id=\"ccb1\"><em>paginate: set empty string by default, and it\u2019ll be altered later on in loop<\/em><\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-preformatted\">url = \"<a href=\"https:\/\/api.tej.com.tw\/api\/datatables\/\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/api.tej.com.tw\/api\/datatables\/<\/a>\" + database + \".json?\" + coid + date_start + date_end + columns + key + paginate <br>rq = requests.get(url)<\/pre>\n\n\n\n<p id=\"59fa\">Then we attach those variables at the end of basic link to form a modified link. The&nbsp;<code>.json?<\/code>&nbsp;within the link represents the json output format. In the next step,&nbsp;<code>requests.get()<\/code>&nbsp;is used to load the contents of the website which are stored in&nbsp;<code>rq<\/code>. Finally, we display the content of&nbsp;<code>rq<\/code>&nbsp;with&nbsp;<code>json()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rq.json()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1Tecd5fP4J9cnau8HMhU59Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Select Chinese column names<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">column_info = rq.json()['datatable']['columns']<\/pre>\n\n\n\n<p id=\"b8a8\">Since from the above we know the data type of&nbsp;<code>rq.json()<\/code>&nbsp;is dictionary, which also contains several dictionaries. Therefore, we choose the second layer dictionary and get the column information with its corresponding key \u2018columns\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/13nlVSuecMjNKCmlj4o4hPQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">chinese_columns = [i['cname'] for i in column_info]<\/pre>\n\n\n\n<p id=\"d869\">So we can extract Chinese columns name based on column information structure<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1JPP4lgVGd6s8LX7I1Ok9ew.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Obtain stock price data and form a dataframe<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">data = rq.json()['datatable']['data']<\/pre>\n\n\n\n<p id=\"e35b\">Here we choose the key \u2018data\u2019 of the second layer dictionary, which contains stock price that we need<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1V1yOKnR2DO4iyqSZwfdlNA.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">data_table_a = pd.DataFrame(data, columns = chinese_columns)<\/pre>\n\n\n\n<p id=\"1ef1\">Form a DataFrame by combining stock price data and column names<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1slWG9X7WVxC8hB-6j38Pcg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"ec4a\">Because TEJ set the limitation on data amount of single access, which is exactly 10,000 amounts, the above result may not have all data we want<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">next_cursor_id = rq.json()['meta']['next_cursor_id']<br>print(next_cursor_id)<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1MLDRszM7SkW7JVHZ5wo0uA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"996c\">To make sure the completeness of data, we use the above way. If next_cursor_id is shown as a random code, meaning still some data is on the next page. But if it prints out&nbsp;<code>None<\/code>, indicating it\u2019s the last page of data pool. What\u2019s worth noting is that every time we call&nbsp;<code>requests.get()<\/code>,&nbsp;<strong>next_cursor_id will change accordingly, even we apply the same link<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7eab\"><span class=\"ez-toc-section\" id=\"Method_B_TEJ_API\"><\/span><strong>Method B: TEJ API<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">data_table_b = tejapi.get('TWN\/APRCD',<br>                          coid = ['2454','3034','2379'],<br>                          mdate = {'gte':'2000-01-01', 'lte':'2020-12-31'},<br>                          opts = {'columns':['coid','mdate','open_d','high_d','low_d','close_d']},<br>                          chinese_column_name = True)<\/pre>\n\n\n\n<p id=\"f4e2\">As for TEJ API, we use&nbsp;<code>tejapi.get()<\/code>&nbsp;function with several inputs. We can see the way to select data range is quite the same. But TEJ API can directly return a Dataframe with no need to combine data and column names.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/0ERBqSL-3JoGXIeri.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"62d2\"><span class=\"ez-toc-section\" id=\"Case2_Obtain_Complete_Stock_Price_Data\"><\/span><strong>Case2: Obtain Complete Stock Price Data<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"b9a3\"><span class=\"ez-toc-section\" id=\"Method_A_REST_API-2\"><\/span><strong>Method A: REST API<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">stock_price_a = pd.DataFrame()while True:<br>    #Reset url<br>    url = \"<a href=\"https:\/\/api.tej.com.tw\/api\/datatables\/\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/api.tej.com.tw\/api\/datatables\/<\/a>\" + database + \".json?\" + coid + date_start + date_end + columns + key + paginate<br>    <br>    #Load website<br>    rq = requests.get(url)   <br> <br>    #Select Chinese column <br>    column_info = rq.json()['datatable']['columns']<br>    chinese_columns = [i['cname'] for i in column_info]<br>    <br>    #Obtain data<br>    data = rq.json()['datatable']['data']<br>    <br>    #Form a dataframe<br>    data_table = pd.DataFrame(data, columns = chinese_columns)<br>    <br>    #Store in the container<br>    stock_price_a = stock_price_a.append(data_table).reset_index(drop=True)<br>    <br>    #Check if there's next page<br>    next_cursor_id = rq.json()['meta']['next_cursor_id']<br>    <br>    #Whether or not to escape the loop<br>    if next_cursor_id == None:<br>        break<br>    else:<br>        paginate = '&amp;opts.cursor_id=' + next_cursor_id<\/pre>\n\n\n\n<p id=\"58e2\">By continuing to use the variables built at the beginning, we firstly create an empty DataFrame&nbsp;<code>stock_price_a<\/code>&nbsp;as a container. Then enter into&nbsp;<code>while<\/code>&nbsp;loop and keep storing each page\u2019s data in&nbsp;<code>stock_price_a<\/code>&nbsp;until&nbsp;<code>next_cursor_id<\/code>&nbsp;become&nbsp;<code>None<\/code>, meaning there\u2019s no next page.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/0pdAfGCVpqY_LR39h.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5422\"><span class=\"ez-toc-section\" id=\"Method_B_TEJ_API-2\"><\/span><strong>Method B: TEJ API<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">stock_price_b = tejapi.get('TWN\/APRCD',<br>                          coid = ['2454','3034','2379'],<br>                          mdate = {'gte':'2000-01-01', 'lte':'2020-12-31'},<br>                          opts = {'columns':['coid','mdate','open_d','high_d','low_d','close_d']},<br>                          paginate = True,<br>                          chinese_column_name = True)<\/pre>\n\n\n\n<p id=\"4139\">With regards to TEJ API, we just need to add&nbsp;<code>paginate = True<\/code>&nbsp;to guarantee the completeness of data<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/0DsL-QjhPqRPUfSwc.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f5ac\"><span class=\"ez-toc-section\" id=\"Case3_Obtain_Complete_Financial_Data\"><\/span><strong>Case3: Obtain Complete Financial Data<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dde8\"><span class=\"ez-toc-section\" id=\"Method_A_REST_API-3\"><\/span><strong>Method A: REST API<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modify website link<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">key = \"&amp;api_key=\" + \"Your Key\"  <br>database = 'TWN\/AIM1A'                                <br>coid = \"&amp;coid=\" + \"2454,3034,2379\"                    <br>date_start = \"&amp;mdate.gte=\" + \"2000-01-01\"             <br>date_end = \"&amp;mdate.lte=\" + \"2020-12-31\"               <br>columns = \"&amp;opts.columns=\" + \"coid,mdate,R531,7210,R106,R504\" <br>pivot = \"&amp;opts.pivot=True\"                            <br>paginate = ''<\/pre>\n\n\n\n<p id=\"15e5\">Here we choose\u00a0<a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=62\" target=\"_blank\" rel=\"noreferrer noopener\">Consolidated financial statements (cumulative)<\/a>\u00a0database. The firms selected and time periods are the same as Case1 and Case2. It\u2019s worth noting that the codes of financial accounts should be looked up in advance to become the column information here, and we also add pivot variable to make Dataframe easier to deal with (To see more details, please refer to\u00a0<a href=\"https:\/\/medium.com\/tej-api-%E9%87%91%E8%9E%8D%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90\/introduction-6-obtain-financial-data-a152f472a58f\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">\u3010Introduction(6)\u3011- Obtai<\/a><a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/introduction-6-obtain-financial-data-a152f472a58f\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">n<\/a><a href=\"https:\/\/medium.com\/tej-api-%E9%87%91%E8%9E%8D%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90\/introduction-6-obtain-financial-data-a152f472a58f\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\"> Financial Data<\/a>) . The following data obtaining structure are much similar to Case2<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Obtain the whole data<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">finance_a = pd.DataFrame()<br>while True:<br>    #Reset url<br>    url = \"<a href=\"https:\/\/api.tej.com.tw\/api\/datatables\/\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/api.tej.com.tw\/api\/datatables\/<\/a>\" + database + \".json?\" + coid + date_start + date_end + columns + key + pivot + paginate<br>    <br>    #Load the website<br>    rq = requests.get(url)<br>    <br>    #Chinese column<br>    column_info = rq.json()['datatable']['columns']<br>    chinese_columns = [i['cname'] for i in column_info]<br>    <br>    #Financial data<br>    data = rq.json()['datatable']['data']<br>    <br>    #Form a dataframe<br>    data_table = pd.DataFrame(data, columns = chinese_columns)<br>    <br>    #Store in finance_a<br>    finance_a = finance_a.append(data_table).reset_index(drop=True)<br>    <br>    #Check if there's next page<br>    next_cursor_id = rq.json()['meta']['next_cursor_id']        #Condition to escape the loop<br>    if next_cursor_id == None:<br>        break<br>    else:<br>        paginate = '&amp;opts.cursor_id=' + next_cursor_id<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/0pclxgEloUBvppw3z.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"b6e6\"><span class=\"ez-toc-section\" id=\"Method_B_TEJ_API-3\"><\/span>Method B: TEJ API<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">finance_b = tejapi.get('TWN\/AIM1A',<br>                       coid = ['2454','3034','2379'],<br>                       mdate = {'gte':'2000-01-01', 'lte':'2020-12-31'},<br>                       opts = {'columns':['coid','mdate','R531','7210','R106','R504'],'pivot':True},<br>                       paginate = True,<br>                       chinese_column_name = True)<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/0mFxqYQvH9BHt3WQZ.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"742a\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"dc60\">With these three examples, we figure out no matter which method we choose, the procedures are all standard and easy to understand. Thus, we recommend readers to set up a function to avoid these repetitive works. To sum up, REST API provides web-crawl mechanism to access to database and its output format is json or xml, which both has its pros and cons, while TEJ API use module built by TEJ and has advantages of quick and efficient access to database. However, this article doesn\u2019t intend to decide which one is better, but aims to deepen readers\u2019 understanding of access to database. If readers are interested in certain databases, welcome to&nbsp;<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" target=\"_blank\" rel=\"noreferrer noopener\">E-shop<\/a>&nbsp;to choose the optimal plan for yourself !<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1271\"><span class=\"ez-toc-section\" id=\"Related_Link\"><\/span><strong>Related Link<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>All roads lead to Rome Highlights Preface TEJ offers different ways to access to TEJ database through programming languages, such as Python-based&nbsp;REST API&nbsp;and&nbsp;TEJ API, R-based&nbsp;R API&nbsp;or NET-based&nbsp;.Net API. Even though the mechanisms behind these are a bit different, the logic of getting data and the way to set input parameter share many similarities, which enable [&hellip;]<\/p>\n","protected":false},"featured_media":16311,"template":"","tags":[2583,2371,2628,2634,3006],"insight-category":[690,50],"class_list":["post-16310","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-finance","tag-python","tag-rest-api","tag-stock-price","tag-tejapi-beginners-guide","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/16310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":2,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/16310\/revisions"}],"predecessor-version":[{"id":24860,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/16310\/revisions\/24860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/media\/16311"}],"wp:attachment":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/media?parent=16310"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/tags?post=16310"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight-category?post=16310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}