Skip to main content

Python SDK for the DNSE Open API

Project description

dnse

Python SDK for the DNSE Open API. Supports sync and async HTTP via httpx, HMAC-SHA256 authentication, Pydantic v2 response models, and strict typing.

Installation

pip install dnse

Quickstart

Authentication

from dnse import DnseClient

with DnseClient(api_key="your-api-key", api_secret="your-api-secret") as client:
    # Step 1: request OTP email
    client.registration.send_otp()

    # Step 2: verify OTP → sets trading token on client
    client.registration.verify_otp("123456")

    # Step 3: use resources
    accounts = client.accounts.list()
    orders = client.orders.list(accounts.accounts[0].id, market_type="STOCK", order_category="NORMAL")

Resource API

from dnse import BoardId, DnseClient, PlaceOrderRequest

with DnseClient(api_key="k", api_secret="s") as client:
    # Accounts
    accts = client.accounts.list()
    balances = client.accounts.balances("0003979888")
    packages = client.accounts.loan_packages("0003979888", market_type="STOCK", symbol="HPG")

    # Orders (trading token required for mutations)
    client.registration.verify_otp("123456")

    # Get security info for valid price range
    secs = client.market.security_info("HPG", board_id=BoardId.ROUND_LOT)
    sec = secs[0]
    print(sec.ceiling_price, sec.floor_price)

    order = client.orders.place(PlaceOrderRequest(
        account_no="0003979888",
        symbol="HPG",
        side="NB",        # NB = buy, NS = sell
        order_type="LO",  # limit order
        quantity=100,
        price=sec.floor_price,  # use floor_price to ensure valid price range
    ))

    active = client.orders.list("0003979888", market_type="STOCK", order_category="NORMAL")
    history = client.orders.history("0003979888", **{"from": "2026-01-01", "to": "2026-03-01"})
    client.orders.cancel("0003979888", order.id or 0)

    # Deals
    deals = client.deals.list("0003979888", market_type="STOCK")

Async

from dnse import AsyncDnseClient

async with AsyncDnseClient(api_key="k", api_secret="s") as client:
    await client.registration.verify_otp("123456")
    orders = await client.orders.list("0003979888", marketType="STOCK")

Error Handling

from dnse import DnseClient, DnseAuthError, DnseRateLimitError, DnseSessionExpiredError, DnseAPIError

with DnseClient(api_key="k", api_secret="s") as client:
    try:
        client.orders.list("0003979888")
    except DnseSessionExpiredError:
        # Trading token expired — re-verify OTP
        client.registration.verify_otp(input("OTP: "))
    except DnseAuthError:
        print("Authentication failed")
    except DnseRateLimitError as e:
        print(f"Rate limited. Retry after: {e.retry_after}s")
    except DnseAPIError as e:
        print(f"API error {e.status_code}: {e.body}")

Configuration

Parameter Default Description
api_key "" API key from DNSE portal
api_secret "" API secret for HMAC signing
base_url https://openapi.dnse.com.vn API base URL
timeout 30.0 Request timeout (seconds)
date_header "date" Date header name ("date" or "x-aux-date")

Development

uv sync
uv run pytest
uv run ruff check .
uv run pyright

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

dnse-0.3.0.tar.gz (144.8 kB view details)

Uploaded Source

Built Distribution

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

dnse-0.3.0-py3-none-any.whl (32.8 kB view details)

Uploaded Python 3

File details

Details for the file dnse-0.3.0.tar.gz.

File metadata

  • Download URL: dnse-0.3.0.tar.gz
  • Upload date:
  • Size: 144.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dnse-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e4e7f5a083fa4b07151f4cfb1bc887c2c34a5009ec2f2bf55884ad5ecc393abb
MD5 e6dd8ac592dc2ca7418a1c0ed9420ae1
BLAKE2b-256 df158b9dbcb96b6c25f34208346412744944451e50aed6d572252725fb2fe3cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for dnse-0.3.0.tar.gz:

Publisher: release.yml on dnse-tech/dnse-py

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

File details

Details for the file dnse-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: dnse-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 32.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dnse-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35e843cd3fab5f1263ceb056f19b43c1d540daf36871e8e95c6ea3e9d075a7f5
MD5 8315d06d2f71dce9d28d99e48050d05c
BLAKE2b-256 07a91735e575b9bc0bef8f89978dc27df5b7acf7233047fd94dd3dc143fa08b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for dnse-0.3.0-py3-none-any.whl:

Publisher: release.yml on dnse-tech/dnse-py

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