Python library to interface with the myTNB API (Tenaga Nasional Berhad)
Project description
python-mytnb
A Python library to interface with the myTNB (Tenaga Nasional Berhad) API for electricity account management and usage monitoring in Malaysia.
Installation
uv add python-mytnb
Or with pip:
pip install python-mytnb
Quick Start
import asyncio
from mytnb import MyTNBClient
async def main():
client = await MyTNBClient.login("user@example.com", "your-password")
async with client:
# Auto-discover linked accounts
accounts = await client.get_customer_accounts()
for acc in accounts:
print(f"{acc.account_number} — {acc.owner_name} (SMR: {acc.is_smart_meter})")
# Get smart meter usage & billing history
usage = await client.get_account_usage_smart("220123456789")
# Monthly billing breakdown
for month in usage.by_month.months:
print(f"{month.month} {month.year}: {month.usage_total} kWh — RM {month.amount_total}")
# Bill history & due amount
history = await client.get_bill_history("220123456789")
due = await client.get_account_due_amount("220123456789")
asyncio.run(main())
CLI
Pass credentials directly:
mytnb --email user@example.com --password yourpass usage 220123456789
Or via environment variables:
export MYTNB_EMAIL=user@example.com
export MYTNB_PASSWORD=yourpass
mytnb usage 220123456789
All commands:
mytnb login # Test login, show user info
mytnb accounts # List all linked accounts (auto-discovery)
mytnb accounts --json # Account list as JSON
mytnb usage <account> # Monthly usage & billing summary
mytnb usage --daily <account> # Daily usage breakdown
mytnb usage --json <account> # Full usage data as JSON
mytnb current-usage <account> # Simplified current usage summary
mytnb due-amount <account> # Outstanding balance
mytnb bill-history <account> # Payment history
Global options: --debug for full tracebacks, --version.
Use a config file instead of flags (--config <path>, mytnb.json, or ~/.config/mytnb/config.json):
mytnb init-config # Generate a starter config file
API Architecture
myTNB uses two API backends, both handled transparently by this library:
| Backend | Domain | Auth | Used for |
|---|---|---|---|
| REST | api.mytnb.com.my |
JWT + API key | Bill eligibility, eligibility icons |
| AWS Gateway | api.mytnb.com.my/core/api |
Encrypted payloads (AES-256-CBC + RSA-OAEP) | Account listing (auto-discovery) |
| Legacy ASMX | mytnbapp.tnb.com.my |
Encrypted payloads (AES-256-CBC + RSA-OAEP) | Usage data, billing, services |
Request encryption for the ASMX API is automatic — just pass plaintext parameters.
Data Models
| Model | Description |
|---|---|
CustomerAccount |
Linked account: number, owner, address, SMR status |
AccountUsage |
Full usage response: metrics, monthly and daily data |
UsageMetric |
Current/average usage (kWh) |
CostMetric |
Current/projected cost (RM) |
BillingMonth |
Monthly billing record with tariff blocks |
DailyUsage |
Daily consumption and cost |
TariffBlock |
Tariff pricing block details |
SMRAccount |
Smart Meter Reading eligibility status |
BREligibility |
Bill rendering opt-in status |
Geographic Restrictions
The myTNB API only accepts connections from Malaysian IP addresses and blocks most VPN services. If you get a GeoBlockedError (HTTP 403), make sure you are connecting from a Malaysian network without a VPN.
Error Handling
from mytnb.exceptions import MyTNBError, APIError, AuthenticationError, GeoBlockedError
try:
client = await MyTNBClient.login("user@example.com", "password")
async with client:
usage = await client.get_account_usage_smart("220123456789")
except GeoBlockedError:
print("Blocked — connect from a Malaysian IP without VPN")
except AuthenticationError:
print("Invalid email or password")
except APIError as e:
print(f"API error {e.error_code}: {e.display_message}")
except MyTNBError as e:
print(f"Error: {e}")
Development
uv sync --extra dev
uv run pytest
uv run pylint src/mytnb/
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_mytnb-0.2.0.tar.gz.
File metadata
- Download URL: python_mytnb-0.2.0.tar.gz
- Upload date:
- Size: 65.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcba7ddab999242d9afc18dd3c986c282cf4c5b786988f2935cce1431200d463
|
|
| MD5 |
d981b011e25242057173df3f60329677
|
|
| BLAKE2b-256 |
c52b7203a5fdf0c4ab7db71279fb9202cc78fbc63f03641c2321cb2baef8c7f2
|
Provenance
The following attestation bundles were made for python_mytnb-0.2.0.tar.gz:
Publisher:
publish.yml on danieyal/python-mytnb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_mytnb-0.2.0.tar.gz -
Subject digest:
fcba7ddab999242d9afc18dd3c986c282cf4c5b786988f2935cce1431200d463 - Sigstore transparency entry: 2007897724
- Sigstore integration time:
-
Permalink:
danieyal/python-mytnb@b291915a9ac9ecc22c10aabaaa81bf45ad79412a -
Branch / Tag:
refs/tags/v.0.2.0 - Owner: https://github.com/danieyal
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b291915a9ac9ecc22c10aabaaa81bf45ad79412a -
Trigger Event:
release
-
Statement type:
File details
Details for the file python_mytnb-0.2.0-py3-none-any.whl.
File metadata
- Download URL: python_mytnb-0.2.0-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9c178db9cb42616fc7f194d08b48b73ef4e42ec78c68714480f844291b7535b
|
|
| MD5 |
aa396c0daebd85471858b49558b80092
|
|
| BLAKE2b-256 |
6391e3a0e6f35e09a03055b4e5500ad998c18c50fc06663e3f8b7817f1799727
|
Provenance
The following attestation bundles were made for python_mytnb-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on danieyal/python-mytnb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_mytnb-0.2.0-py3-none-any.whl -
Subject digest:
a9c178db9cb42616fc7f194d08b48b73ef4e42ec78c68714480f844291b7535b - Sigstore transparency entry: 2007897878
- Sigstore integration time:
-
Permalink:
danieyal/python-mytnb@b291915a9ac9ecc22c10aabaaa81bf45ad79412a -
Branch / Tag:
refs/tags/v.0.2.0 - Owner: https://github.com/danieyal
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b291915a9ac9ecc22c10aabaaa81bf45ad79412a -
Trigger Event:
release
-
Statement type: