Settrade API Setup (Thai SET Stock Market)
Connect KhunQuant to your Thai stock brokerage account via the Settrade Open API — allowing portfolio tracking and order execution for SET-listed securities.
Overview
Settrade API is more complex than crypto exchange APIs because it involves two separate systems:
- Your broker account — the actual brokerage firm where you hold your Thai stocks (e.g., Pi Securities, Bualuang Securities, Phillip Securities)
- Settrade Developer Portal — where you register your application and generate API credentials
Both are required. This guide walks through both steps.
Unlike crypto exchanges, stock market withdrawal (cash transfer out) is NOT available via the Settrade API — the API only supports trading. However, still configure permissions carefully as trade access is real money.
Prerequisites
Before you start, confirm you have:
- An active trading account with a Settrade-connected broker
- Your broker's trading PIN (6-digit PIN used to confirm orders)
- Your broker ID — a 3-digit code from the Settrade Broker List (e.g.
023for InnovestX) - Your account number from your broker
Not all brokers support the Settrade Open API, and support may differ between Equity and TFEX (Derivatives) trading.
To check which brokers support Open API for Equity, TFEX, or both — visit the official Settrade broker comparison page:
settrade.com/th/services-and-tools/brokers
Look for the "Settrade Open API สำหรับหุ้น" and "Settrade Open API สำหรับ TFEX" columns in the broker table. A blue checkmark (✔) means the broker supports that feature.
For the full broker list with 3-digit broker codes, see developer.settrade.com/open-api/document/broker-list.
Part 1 — Register on Settrade Developer Portal
Step 1 — Create a Developer Account
- Go to developer.settrade.com
- Click [Sign Up] / [Register]
- Complete registration with your email address
Step 2 — Create a New Application
- Log in to the developer portal
- Click [My Applications] → [Create Application]
- Fill in the form:
- Application Name:
KhunQuant(or any descriptive name) - Description: Your personal portfolio assistant
- Application Type: Select Individual (not business)
- Application Name:
- Click Submit
Step 3 — Save Your Application Credentials
After creation, the portal shows:
- App Login ID — your application identifier
- App Secret — ⚠️ Shown only once
Copy both immediately. The App Secret cannot be recovered — if lost, you must create a new application.
You receive two different credentials from Settrade:
- App Secret — a token shown once during app creation (save it, but KhunQuant doesn't use this directly)
- Private Key — you generate this yourself locally (see Part 2 below)
Part 2 — Generate Your Cryptographic Key Pair
Settrade uses asymmetric cryptography (ECDSA P-256) for authentication — instead of a simple password, you generate a key pair on your own machine and upload only the public key to Settrade.
Step 4 — Generate the Key Pair
Open your terminal and run these commands:
# Generate ECDSA P-256 private key (PKCS#8 format)
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out settrade_private.pem
# Extract the public key
openssl pkey -in settrade_private.pem -pubout -out settrade_public.pem
# Convert private key to base64 (single line, for .security.yml)
openssl pkey -in settrade_private.pem -outform DER | base64 | tr -d '\n' > settrade_private_b64.txt
After running these commands, you will have three files:
settrade_private.pem— keep this private, never share itsettrade_public.pem— you will upload this to Settradesettrade_private_b64.txt— the base64 version you paste into KhunQuant
Step 5 — Upload Your Public Key to Settrade
- Go back to developer.settrade.com
- Open your application
- Find the "Public Key" section
- Open
settrade_public.pemin a text editor, copy all the contents - Paste it into the Settrade public key field
- Click Save
Step 6 — Link to Your Broker Account
In the developer portal, link your application to your broker:
- Go to [My Applications] → select your app
- Find [Broker Integration] or [Account Linking]
- Enter your broker details:
- Broker ID: a 3-digit code from the Settrade Broker List (e.g.
023for InnovestX) - Account Number: your trading account number
- Broker ID: a 3-digit code from the Settrade Broker List (e.g.
- Complete verification — you may need to confirm via your broker's app or email
Part 3 — Add to KhunQuant
Settrade credentials are split between two places: secrets go through the web UI, and non-sensitive broker details go in config.json.
Step 7 — Add Secrets via Web UI (Recommended)
Open KhunQuant's portfolio settings in your browser:
http://localhost:18800/portfolios/settrade
Fill in the three secret fields:
- App Login ID — from the developer portal (this is your
api_key) - Private Key (base64) — the contents of
settrade_private_b64.txt(runcat settrade_private_b64.txtto get it) - Trading PIN — your 6-digit PIN from your broker
Click Save. KhunQuant automatically encrypts all three values with enc:// prefix.
If your .security.yml already uses encrypted fields (enc:// prefix), pasting plain credentials directly into the file will cause an authentication error at startup. The web UI handles encryption automatically and avoids this problem. The base64 private key is especially long — pasting it into the web UI is much less error-prone than manual YAML editing.
Alternative: Manual .security.yml edit (advanced users only)
exchanges:
settrade:
accounts:
- api_key: "your_app_login_id" # App Login ID from developer portal
secret: "base64_encoded_private_key" # Contents of settrade_private_b64.txt
pin: "your_6_digit_trading_pin" # Trading PIN from your broker
Step 8 — Add Broker Details to config.json
This step cannot be done through the web UI — broker_id, app_code, and account_no are non-sensitive configuration that lives in config.json. Open ~/.khunquant/config.json:
{
"exchanges": {
"settrade": {
"enabled": true,
"accounts": [
{
"broker_id": "023",
"app_code": "ALGO_EQ",
"account_no": "1234567"
}
]
}
}
}
Replace the values with your actual broker details:
- broker_id: your broker's 3-digit code from the Settrade Broker List (e.g.
"023"for InnovestX) - app_code:
"ALGO_EQ"for Equity trading,"ALGO"for Derivatives trading - account_no: your brokerage account number
Step 9 — Verify Setup
Restart KhunQuant. Try:
- "ดูพอร์ตหุ้น SET ของฉัน"
- "What are my Thai stocks?"
- "Show my SET portfolio"
- "What's the price of PTT?"
Security Best Practices
- Delete the .pem files after converting to base64 and adding to KhunQuant. The private key is now safely stored in
.security.yml. - Never share your private key base64 string — it gives complete trading access to your account
- Use Credential Encryption — see Credential Encryption to encrypt the values in
.security.ymlso they're not stored in plaintext
Troubleshooting
| Problem | Solution |
|---|---|
| "Authentication failed" | Check that your App Login ID and base64 private key are correct. Ensure the public key was uploaded to Settrade |
| "Invalid broker account" | Verify broker_id, app_code, and account_no in config.json — these must match exactly what Settrade shows |
| "PIN rejected" | Confirm the trading PIN — this is the 6-digit PIN you use to confirm orders at your broker |
| Public key mismatch | Regenerate the key pair (Steps 4–5) and re-upload the public key |
| Key pair generation fails | Make sure openssl is installed: run openssl version in terminal. On Mac: brew install openssl |
Related
- Exchange Overview — hub page and security.yml reference
- Credential Encryption — encrypt your private key at rest