Skip to main content

Python SDK for SAHMK API with typed models, CLI, and market index scoping.

Project description

SAHMK Python SDK

A lightweight Python client for the SAHMK Developer API — real-time and historical Saudi stock market (Tadawul) data.

Features

  • Real-time quotes — live prices for 350+ Tadawul stocks
  • Batch quotes — up to 50 stocks in a single request
  • Historical data — daily/weekly/monthly OHLCV with custom date ranges
  • Market overview — index-scoped market data (TASI/NOMU), gainers, losers, volume/value leaders, sectors
  • Company info — fundamentals, technicals, valuation, analyst consensus (by plan)
  • Financials — income statements, balance sheets, cash flow
  • Dividends — history, yield, upcoming payments
  • Events — AI-generated stock event summaries
  • WebSocket streaming — real-time price updates pushed to you (Pro+ plan)

Installation

pip install sahmk

Or clone this repo for local development:

git clone https://github.com/sahmk-sa/sahmk-python.git
cd sahmk-python
pip install -r requirements.txt

Quick Start

from sahmk import SahmkClient

client = SahmkClient("your_api_key")

# Get a stock quote
quote = client.quote("2222")
print(f"{quote['name_en']}: {quote['price']} SAR ({quote['change_percent']}%)")

# Get market summary
market = client.market_summary()
print(f"TASI: {market['index_value']} ({market['index_change_percent']}%)")

# Scope market endpoints by index (NOMUC alias is accepted)
nomu = client.gainers(limit=5, index="NOMUC")
print(f"Index: {nomu.index}, delayed: {nomu.is_delayed}")

# Batch quotes (Starter+ plan)
result = client.quotes(["2222", "1120", "4191"])
for q in result["quotes"]:
    print(f"{q['symbol']}: {q['price']}")

CLI Quick Start

The package also installs a CLI for instant testing:

export SAHMK_API_KEY="your_api_key"
sahmk quote 2222
sahmk market gainers --limit 5
sahmk market summary --index NOMU
sahmk historical 2222 --from 2026-01-01 --to 2026-01-28
sahmk company 2222
sahmk financials 2222
sahmk dividends 2222
sahmk events --symbol 2222 --limit 5
sahmk stream 2222,1120

You can also pass the key directly:

sahmk quote 2222 --api-key your_api_key

Typed Responses

All client methods return typed objects with IDE autocompletion — while preserving full backwards compatibility with dict-style access:

quote = client.quote("2222")

# New: typed attribute access
print(quote.price)
print(quote.liquidity.net_value)

# Still works: dict-style access (backwards compatible)
print(quote["price"])
print(quote.get("volume"))

Access the original API response dict via .raw:

raw_dict = quote.raw

Market Index Scoping

Market endpoints support optional index scoping:

  • Accepted values: TASI, NOMU
  • Alias: NOMUC (normalized to NOMU)
  • Omitted index remains backward compatible (server defaults to TASI)
summary = client.market_summary(index="NOMUC")
print(summary.index)       # NOMU
print(summary.is_delayed)  # True/False by plan entitlement
sahmk market summary --index NOMU
sahmk market gainers --limit 10 --index NOMUC

Retries and Rate Limits

The client automatically retries transient failures (429 rate-limit and 5xx server errors) with exponential backoff:

# Defaults: 3 retries, 0.5s backoff factor (0.5s, 1s, 2s delays)
client = SahmkClient("your_api_key")

# Customize retry behavior
client = SahmkClient("your_api_key", retries=5, backoff_factor=1.0)

# Disable retries entirely
client = SahmkClient("your_api_key", retries=0)

Rate limit errors include metadata from the API:

from sahmk import SahmkRateLimitError

try:
    quote = client.quote("2222")
except SahmkRateLimitError as e:
    print(f"Rate limited. Retry after: {e.retry_after}s")
    print(f"Remaining: {e.rate_remaining}/{e.rate_limit}")

Get Your API Key

  1. Sign up at sahmk.sa/developers
  2. Verify your email
  3. Go to Dashboard → API Keys → Create Key
  4. Copy your key (starts with shmk_live_ or shmk_test_)

Plans

Plan Price Requests/Day WebSocket
Free 0 SAR 100 -
Starter 149 SAR/mo 5,000 -
Pro 499 SAR/mo 50,000 Yes
Enterprise Contact us Unlimited Yes

Examples

Check the examples/ directory:

File Description
quote.py Get a single stock quote
batch_quotes.py Fetch multiple stocks at once
historical.py Historical price data
market_summary.py Market overview and movers
websocket_stream.py Real-time WebSocket streaming (Pro+)

API Reference

Base URL: https://app.sahmk.sa/api/v1

Endpoint Plan Description
GET /quote/{symbol}/ Free Stock quote
GET /quotes/?symbols=... Starter+ Batch quotes (up to 50)
GET /historical/{symbol}/ Starter+ Historical OHLCV data
GET /market/summary/ Free Market overview & TASI index
GET /market/gainers/ Free Top gainers
GET /market/losers/ Free Top losers
GET /market/volume/ Free Volume leaders
GET /market/value/ Free Value leaders
GET /market/sectors/ Free Sector performance
GET /company/{symbol}/ Free+ Company info (tiered by plan)
GET /financials/{symbol}/ Starter+ Financial statements
GET /dividends/{symbol}/ Starter+ Dividend history & yield
GET /events/ Pro+ AI-generated stock events

All endpoints require the X-API-Key header.

Full docs: sahmk.sa/developers/docs

Changelog: CHANGELOG.md

Roadmap: ROADMAP.md

WebSocket Streaming (Pro+)

import asyncio
from sahmk import SahmkClient

client = SahmkClient("your_api_key")

async def on_quote(msg):
    symbol = msg["symbol"]
    price = msg["data"]["price"]
    print(f"{symbol}: {price}")

asyncio.run(client.stream(["2222", "1120"], on_quote=on_quote))

Auto-Reconnect

The streaming client automatically reconnects on disconnect with exponential backoff. All symbols are resubscribed after reconnection.

async def on_disconnect(reason):
    print(f"Disconnected: {reason}")

async def on_reconnect(attempt):
    print(f"Reconnecting (attempt #{attempt})...")

await client.stream(
    ["2222", "1120"],
    on_quote=on_quote,
    on_disconnect=on_disconnect,
    on_reconnect=on_reconnect,
    # max_reconnect_attempts=0 means unlimited (default)
    # Set to -1 to disable reconnect entirely
)

Connection URL: wss://app.sahmk.sa/ws/v1/stocks/?api_key=YOUR_KEY

License

MIT — see LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sahmk-0.6.2.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sahmk-0.6.2-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file sahmk-0.6.2.tar.gz.

File metadata

  • Download URL: sahmk-0.6.2.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sahmk-0.6.2.tar.gz
Algorithm Hash digest
SHA256 5dc736c099ca4e40481ad9565118fa32c3a022c389513f9df8eaf750251868e1
MD5 9ebc5fe901a369b81e0e378e1511438d
BLAKE2b-256 f4987f5af57708f38185d6f0498b826e694dd9139424c38ec1169a653b44460f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sahmk-0.6.2.tar.gz:

Publisher: publish-pypi.yml on sahmk-sa/sahmk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sahmk-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: sahmk-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sahmk-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 adb04e0e266876a89243e33a5466bfa5d336f9e1f13727e4dbb3858d28db7cd7
MD5 a3863eb40eda071dbb1ce8eb78cbbeda
BLAKE2b-256 b92c2b85d94bb8d98b73a0808d802b45a1705bd0719b521c6e7c87870b883151

See more details on using hashes here.

Provenance

The following attestation bundles were made for sahmk-0.6.2-py3-none-any.whl:

Publisher: publish-pypi.yml on sahmk-sa/sahmk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page