Skip to content

Python Documentation

The following documentation explains how to use the TEJ Python API to access the TEJ database.

Installation

Simply install tejapi using pip.

Install the Python package

pip install tejapi 

Authentication

After installation, include the following lines at the beginning of your script

Authentication

import tejapi
tejapi.ApiConfig.api_key = "YOURAPIKEY"    

Global Parameters

Use the tejapi.ApiConfig.ignoretz parameter to determine whether datetime fields should include timezone information (default is False, meaning timezone is shown).
Set it to True to ignore timezones.

Global Parameters

import tejapi
tejapi.ApiConfig.api_key = "YOURAPIKEY"
tejapi.ApiConfig.ignoretz = True    

Retrieve Usage Information

Use tejapi.ApiConfig.info() to retrieve various usage details.

tejapi.ApiConfig.info()

import tejapi
tejapi.ApiConfig.api_key = "YOURAPIKEY"
info = tejapi.ApiConfig.info() 

Response Information Description

Parameter Description
key Your API key
startDate API key activation date
endDate API key expiration date
reqDayLimit Daily request limit
rowsDayLimit Daily row limit
rowsMonthLimit Monthly row limit
todayReqCount Requests made today `
todayRows Rows used today
monthRows Rows used this month
user User information
ParameterDescription
userIdUser ID
nameUser name
shortNameUser short name
subscriptionStartDateSubscription start date
subscriptionEndDateSubscription end date
tablesList of accessible tables

Data Query

The following section explains how to use the TEJ Python API to query data.

Usage

Take the unadjusted stock price (daily) for listed (OTC) companies, TWN/APRCD, as an example. You can directly use the tejapi.get() method to retrieve the data, as shown in the example below.

API Data Retrieval Example for Stock Prices(TWN/APRCD)

data = tejapi.get('TWN/APRCD')

Tip

If no filter conditions are provided, all data will be retrieved. It is recommended to apply appropriate filters as explained below.

Data Filtering

If you need to filter by fields (e.g., only retrieve the Taiwan Weighted Index (Y9999)), simply add a filter condition such as coid='Y9999':

Single Column Filtering

data = tejapi.get('TWN/APRCD',coid='Y9999')

To retrieve two indices, Y9999 and Y9998, use a sequence condition:

Multiple Value Selection

data = tejapi.get('TWN/APRCD',coid=['Y9999','Y9998'])

The system limits a single request to a maximum of 10,000 records. You may use paginate=TRUE to automatically fetch data in multiple batches:

Pagination for Large Data Sets

data = tejapi.get('TWN/APRCD',coid='Y9999', paginate=True)

Tip

The system limits each request to a maximum of 10,000 records. You can use the paginate=TRUE parameter to retrieve data in batches, but the total number of records is capped at 1,000,000. Please use appropriate filters to reduce data volume.

Filtering across multiple columns is supported.

Multiple Column Filtering Syntax

data = tejapi.get('TWN/APRCD',coid='Y9999', mdate='2018-01-20'
,paginate=True)

Column Filtering

To retrieve only a single column, use the columns parameter in opts. For example, to retrieve just the opening price (open_d):

Retrieve the opening price column (open_d)

data = tejapi.get('TWN/APRCD',opts={'columns':'open_d'}) 

For multiple columns (e.g., mdate, open_d):

[ ' mdate ' , ' open_d ' ]

data = tejapi.get('TWN/APRCD',opts={'columns':['mdate','open_d']})

Data Filtering Operations

You can filter data by value ranges.

For example, to retrieve data with a trade date after 2018-01-01:

Retrieving data where the transaction date is greater than 2018-01-01

data = tejapi.get('TWN/APRCD',coid='Y9999', mdate={'gt':'2018-01-01'},
paginate=True)

To retrieve data with a trade date between 2018-01-01 and 2018-02-01:

Retrieving data where the transaction date is greater than 2018-01-01 and less than 2018-02-01

data = tejapi.get('TWN/APRCD',coid='Y9999',
mdate={'gt':'2018-01-01','lt':'2018-02-01'}, paginate=True)
Filter Operator Description Example
= The field is equal to the specified value or string coid='Y9999'
gt The field is greater than the specified value mdate={'gt': '2018-01-01'}
lt The field is less than the specified value mdate={'lt': '2018-01-01'}
gte The field is greater than or equal to the specified value mdate={'gte': '2018-01-01'}
lte The field is less than or equal to the specified value mdate={'lte': '2018-01-01'}
ne The field is not equal to the specified value mdate={'ne': '2018-01-01'}

Query Parameters

Parameter Required Description Example
columns No Specify the fields to retrieve. Use commas to separate multiple field names. opts={'columns': ['coid', 'mdate', 'val']}
per_page No Number of records to retrieve per request (maximum is 10,000). Total records must not exceed 1,000,000. opts={'per_page': 100}
sort No Specify the sorting field and order using the format [field].[order]. Sorting order can be ascending (asc) or descending (desc). Default is ascending. opts={'sort': 'mdate.desc'}
pivot No Pivot table feature, transforms values into columns. Currently only supported for financial-related tables. opts={'pivot': True}

Other Parameters

Parameter Required Description Example
chinese_column_name No Display column names in Chinese. chinese_column_name=True

Table Information

The following documentation explains how to retrieve table metadata.

Usage

TWN/APRCD

import tejapi
tejapi.ApiConfig.api_key = "YOURAPIKEY"
table_info = tejapi.table_info('TWN/APRCD')

Response Information Description

Parameter Description
dbCode Database code
tableCode Table code
name Table name
description Table description
primaryKey List of primary keys in the table
filters Available filter fields for the table
columns Table column details
ParameterDescription
nameColumn code
cnameColumn name (Chinese)
descriptionColumn description
typeData type
unitUnit
pivot Whether pivoting is supported
status Table status
ParameterDescription
statusStatus
refreshed_atLatest Data Format Modification Time

Search Tables

The following documentation explains how to search for tables using keywords.

Usage

tejapi.search_table

import tejapi
tejapi.ApiConfig.api_key = "YOURAPIKEY"
search = tejapi.search_table("keyword")

The response is an array representing multiple results.Each item contains the following information:

Parameter Description
matchType Match type
  • TABLE_MATCH: Keyword is found in the table name or description
  • COLUMN_MATCH: Keyword is found in the column name or description
  • TABLE_MATCH & COLUMN_MATCH: Keyword is found in both the table and column names or descriptions
tableId Table ID
tableName Table name
tableDesc Table description
columns Table column information
ParameterDescription
nameColumn code
cnameColumn name (Chinese)
descriptionColumn description
typeData type
unitUnit