Sectors API
Query IDX and SGX financial market data through the Sectors REST API.
Full API docs: https://sectors.app/api
Constraints
- - ONLY make HTTP requests to
https://api.sectors.app/v1. Never call any other domain, database, or external service. - All endpoints are
GET requests returning JSON. - Never hardcode or guess an API key. Always read it from the
SECTORS_API_KEY environment variable. - If
SECTORS_API_KEY is not set, prompt the user to set it: export SECTORS_API_KEY="your-api-key-here" or run the setup check script at scripts/check_setup.py.
Setup
1. Set the API key
The API key must be available as the SECTORS_API_KEY environment variable.
CODEBLOCK0
For agent-specific configuration:
- - Claude Code: INLINECODE7
- OpenCode: Set in
~/.config/opencode/config.json under INLINECODE9 - Cursor: Settings > Features > Environment Variables
2. Install the dependency
CODEBLOCK1
3. Verify setup (optional)
CODEBLOCK2
Making requests
CODEBLOCK3
The Authorization header takes the raw API key. Do NOT prefix it with Bearer.
Endpoint decision table
Pick the right endpoint based on what the user needs:
Market structure
| User wants | Endpoint | Required params |
|---|
| List all subsectors | INLINECODE12 | none |
| List all industries |
GET /industries/ | none |
| List all subindustries |
GET /subindustries/ | none |
| SGX sector list |
GET /sgx/sectors/ | none |
Company discovery
| User wants | Endpoint | Required params |
|---|
| Companies in a subsector | INLINECODE16 | INLINECODE17 |
| Companies in a subindustry |
GET /companies/?sub_industry={sub_industry} |
sub_industry |
| Companies in a stock index |
GET /index/{index}/ |
index |
| Companies with segment data |
GET /companies/list_companies_with_segments/ | none |
| SGX companies by sector |
GET /sgx/companies/?sector={sector} |
sector |
Company details
| User wants | Endpoint | Required params |
|---|
| Full company report (IDX) | INLINECODE25 | INLINECODE26 |
| SGX company report |
GET /sgx/company/report/{ticker} |
ticker |
| Listing performance |
GET /listing-performance/{ticker}/ |
ticker |
| Quarterly financial dates |
GET /company/get_quarterly_financial_dates/{ticker}/ |
ticker |
| Quarterly financials |
GET /financials/quarterly/{ticker}/ |
ticker |
| Company segments |
GET /company/get-segments/{ticker}/ |
ticker |
Market data
| User wants | Endpoint | Required params |
|---|
| Daily stock price | INLINECODE37 | INLINECODE38 |
| Index daily data |
GET /index-daily/{index_code}/ |
index_code |
| Index summary |
GET /index/{index}/ |
index |
| IDX total market cap |
GET /idx-total/ | none |
Rankings and screening
| User wants | Endpoint | Required params |
|---|
| Top gainers/losers | INLINECODE44 | none (all optional) |
| Top companies by metric |
GET /companies/top/ | none (all optional) |
| Top growth companies |
GET /companies/top-growth/ | none (all optional) |
| Most traded stocks |
GET /most-traded/ | none (all optional) |
| SGX top companies |
GET /sgx/companies/top/ | none (all optional) |
For full parameter lists and response schemas, see:
Common patterns
Fetch a company report
CODEBLOCK4
Available sections: overview, valuation, future, peers, financials, dividend, management, ownership. Use all or omit for everything.
Get daily stock prices in a date range
CODEBLOCK5
Find top gainers and losers
CODEBLOCK6
List companies in an index
CODEBLOCK7
SGX company report
CODEBLOCK8
Ticker normalization
| Market | Rule | Example |
|---|
| IDX | Uppercase, strip .JK suffix | INLINECODE59 -> INLINECODE60 |
| SGX |
Uppercase, strip
.SI suffix |
d05.si ->
D05 |
Always normalize before passing to an endpoint.
Gotchas
- 1. Auth header format: Use
Authorization: <raw_key>. NOT Bearer <key>. NOT Authorization: Bearer <key>.
- 2. Date format: Always
YYYY-MM-DD. Example: 2025-06-15.
- 3. Date range limit: The
/most-traded/ endpoint requires start and end dates within 90 days of each other.
- 4. Kebab-case for subsectors and sectors: Use
banks, financing-service, consumer-defensive. Not camelCase or snake_case.
- 5. Nested response structure: Ranking endpoints (
top-changes, top, top-growth) return objects keyed by classification, then by period. Always navigate both levels.
CODEBLOCK9
- 6. Market cap units: IDX values are in billion IDR (
min_mcap_billion). SGX values are in million SGD (min_mcap_million).
- 7. Default values matter: Many optional params default to
"all" or specific values (e.g. n_stock defaults to 5, min_mcap_billion defaults to 5000). Be explicit when you need different behavior.
- 8. Index codes: IDX index daily data uses lowercase codes:
ihsg, lq45, idx30. Company-by-index uses the same codes.
- 9. Quarterly financials
approx flag: When approx=true, the API returns the closest available quarter if an exact match for report_date is not found.
- 10. Company report sections param: Only appended to the URL when not
"all". If you want all sections, omit the sections parameter entirely.
Available IDX indices
INLINECODE89 , idx30, idxbumn20, idxesgl, idxg30, idxhidiv20, idxq30, idxv30, jii70, kompas100, lq45, sminfra18, srikehati, economic30, INLINECODE103
Top companies classifications
IDX (/companies/top/): dividend_yield, total_dividend, revenue, earnings, market_cap, pb, pe, INLINECODE112
IDX growth (/companies/top-growth/): top_earnings_growth_gainers, top_earnings_growth_losers, top_revenue_growth_gainers, INLINECODE117
IDX movers (/companies/top-changes/): top_gainers, INLINECODE120
SGX (/sgx/companies/top/): dividend_yield, revenue, earnings, market_cap, INLINECODE126
Error handling
Always check the response status:
CODEBLOCK10
Sectors API
通过Sectors REST API查询IDX和SGX金融市场数据。
完整API文档: https://sectors.app/api
约束条件
- - 仅向https://api.sectors.app/v1发送HTTP请求。不得调用任何其他域名、数据库或外部服务。
- 所有端点均为返回JSON的GET请求。
- 切勿硬编码或猜测API密钥。始终从SECTORSAPIKEY环境变量中读取。
- 如果未设置SECTORSAPIKEY,提示用户进行设置:export SECTORSAPIKEY=your-api-key-here或运行位于scripts/check_setup.py的设置检查脚本。
设置
1. 设置API密钥
API密钥必须作为SECTORSAPIKEY环境变量提供。
bash
选项A:在当前shell中设置
export SECTORS
APIKEY=your-api-key-here
选项B:添加到shell配置文件(~/.bashrc, ~/.zshrc)以持久化
echo export SECTORS
APIKEY=your-api-key-here >> ~/.bashrc
选项C:在项目根目录使用.env文件(参见.env.example)
针对特定代理的配置:
- - Claude Code:claude config set env SECTORSAPIKEY your-api-key-here
- OpenCode:在~/.config/opencode/config.json的env下设置
- Cursor:设置 > 功能 > 环境变量
2. 安装依赖
bash
pip install requests
3. 验证设置(可选)
bash
python scripts/check_setup.py
发起请求
python
import os
import requests
APIKEY = os.environ[SECTORSAPI_KEY]
BASE_URL = https://api.sectors.app/v1
headers = {Authorization: API_KEY}
response = requests.get(f{BASE_URL}/subsectors/, headers=headers)
data = response.json()
Authorization头使用原始API密钥。不要添加Bearer前缀。
端点决策表
根据用户需求选择正确的端点:
市场结构
| 用户需求 | 端点 | 必需参数 |
|---|
| 列出所有子行业 | GET /subsectors/ | 无 |
| 列出所有行业 |
GET /industries/ | 无 |
| 列出所有子行业细分 | GET /subindustries/ | 无 |
| SGX行业列表 | GET /sgx/sectors/ | 无 |
公司发现
| 用户需求 | 端点 | 必需参数 |
|---|
| 子行业中的公司 | GET /companies/?subsector={subsector} | subsector |
| 子行业细分中的公司 |
GET /companies/?subindustry={sub
industry} | subindustry |
| 股票指数中的公司 | GET /index/{index}/ | index |
| 具有细分数据的公司 | GET /companies/list
companieswith_segments/ | 无 |
| 按行业划分的SGX公司 | GET /sgx/companies/?sector={sector} | sector |
公司详情
| 用户需求 | 端点 | 必需参数 |
|---|
| 完整公司报告(IDX) | GET /company/report/{ticker}/ | ticker |
| SGX公司报告 |
GET /sgx/company/report/{ticker} | ticker |
| 上市表现 | GET /listing-performance/{ticker}/ | ticker |
| 季度财务日期 | GET /company/get
quarterlyfinancial_dates/{ticker}/ | ticker |
| 季度财务数据 | GET /financials/quarterly/{ticker}/ | ticker |
| 公司细分 | GET /company/get-segments/{ticker}/ | ticker |
市场数据
| 用户需求 | 端点 | 必需参数 |
|---|
| 每日股票价格 | GET /daily/{ticker} | ticker |
| 指数每日数据 |
GET /index-daily/{index
code}/ | indexcode |
| 指数摘要 | GET /index/{index}/ | index |
| IDX总市值 | GET /idx-total/ | 无 |
排名与筛选
| 用户需求 | 端点 | 必需参数 |
|---|
| 涨幅最大/跌幅最大 | GET /companies/top-changes/ | 无(均为可选) |
| 按指标排名的顶级公司 |
GET /companies/top/ | 无(均为可选) |
| 增长最快的公司 | GET /companies/top-growth/ | 无(均为可选) |
| 交易量最大的股票 | GET /most-traded/ | 无(均为可选) |
| SGX顶级公司 | GET /sgx/companies/top/ | 无(均为可选) |
有关完整参数列表和响应模式,请参见:
常见模式
获取公司报告
python
import os
import requests
APIKEY = os.environ[SECTORSAPI_KEY]
BASE_URL = https://api.sectors.app/v1
headers = {Authorization: API_KEY}
ticker = BBCA
params = {sections: overview,valuation,financials}
resp = requests.get(f{BASE_URL}/company/report/{ticker}/, headers=headers, params=params)
report = resp.json()
print(report[company_name])
print(report[overview][market_cap])
可用板块:overview, valuation, future, peers, financials, dividend, management, ownership。使用all或省略以获取所有内容。
获取日期范围内的每日股票价格
python
import os
import requests
APIKEY = os.environ[SECTORSAPI_KEY]
BASE_URL = https://api.sectors.app/v1
headers = {Authorization: API_KEY}
ticker = BBRI.JK
标准化:大写,去除.JK后缀
clean = ticker.upper().replace(.JK, )
params = {start: 2025-01-01, end: 2025-01-31}
resp = requests.get(f{BASE_URL}/daily/{clean}, headers=headers, params=params)
prices = resp.json()
for day in prices:
print(day[date], day[close], day[volume])
查找涨幅最大和跌幅最大的股票
python
import os
import requests
APIKEY = os.environ[SECTORSAPI_KEY]
BASE_URL = https://api.sectors.app/v1
headers = {Authorization: API_KEY}
params = {
classifications: topgainers,toplosers,
periods: 7d,30d,
n_stock: 5,
minmcapbillion: 5000,
}
resp = requests.get(f{BASE_URL}/companies/top-changes/, headers=headers, params=params)
movers = resp.json()
for stock in movers[top_gainers][7d]:
print(stock[symbol], stock[price_change])
列出指数中的公司
python
import os
import requests
APIKEY = os.environ[SECTORSAPI_KEY]
BASE_URL = https://api.sectors.app/v1
headers = {Authorization: API_KEY}
可用指数:lq45, idx30, kompas100, jii70, idxhidiv20, srikehati等
resp = requests.get(f{BASE_URL}/index/lq45/, headers=headers)
companies = resp.json()
for c in companies:
print(c[symbol], c[company_name])
SGX公司报告
python
import os
import requests
APIKEY = os.environ[SECTORSAPI_KEY]
BASE_URL = https://api.sectors.app/v1
headers = {Authorization: API_KEY}
ticker = D05 # 星展集团
resp = requests.get(f{BASE_URL}/sgx/company/report/{ticker}, headers=headers)
report = resp.json()
print(report[name])
print(report[valuation][pe])
print(report[financials][gross_margin])
股票代码标准化
| 市场 | 规则 | 示例 |
|---|
| IDX | 大写,去除.JK后缀 | bbca.jk -> BBCA |
| SGX |
大写,去除.SI后缀