Skip to main content

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.

← Back to Exchange Overview


Overview

Settrade API is more complex than crypto exchange APIs because it involves two separate systems:

  1. Your broker account — the actual brokerage firm where you hold your Thai stocks (e.g., Pi Securities, Bualuang Securities, Phillip Securities)
  2. Settrade Developer Portal — where you register your application and generate API credentials

Both are required. This guide walks through both steps.

Withdrawal Risk is Lower for Settrade

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. 023 for InnovestX)
  • Your account number from your broker
Which Brokers Support Settrade Open API?

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

  1. Go to developer.settrade.com
  2. Click [Sign Up] / [Register]
  3. Complete registration with your email address

Step 2 — Create a New Application

  1. Log in to the developer portal
  2. Click [My Applications][Create Application]
  3. Fill in the form:
    • Application Name: KhunQuant (or any descriptive name)
    • Description: Your personal portfolio assistant
    • Application Type: Select Individual (not business)
  4. 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.

App Secret vs Private Key

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 it
  • settrade_public.pem — you will upload this to Settrade
  • settrade_private_b64.txt — the base64 version you paste into KhunQuant

Step 5 — Upload Your Public Key to Settrade

  1. Go back to developer.settrade.com
  2. Open your application
  3. Find the "Public Key" section
  4. Open settrade_public.pem in a text editor, copy all the contents
  5. Paste it into the Settrade public key field
  6. Click Save

In the developer portal, link your application to your broker:

  1. Go to [My Applications] → select your app
  2. Find [Broker Integration] or [Account Linking]
  3. Enter your broker details:
    • Broker ID: a 3-digit code from the Settrade Broker List (e.g. 023 for InnovestX)
    • Account Number: your trading account number
  4. 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.

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 (run cat settrade_private_b64.txt to get it)
  • Trading PIN — your 6-digit PIN from your broker

Click Save. KhunQuant automatically encrypts all three values with enc:// prefix.

Why Not Edit the File Directly?

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.yml so they're not stored in plaintext

Troubleshooting

ProblemSolution
"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 mismatchRegenerate the key pair (Steps 4–5) and re-upload the public key
Key pair generation failsMake sure openssl is installed: run openssl version in terminal. On Mac: brew install openssl