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, marketType="STOCK")

Resource API

from dnse import 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")

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

    order = client.orders.place(PlaceOrderRequest(
        account_no="0003979888",
        symbol="HPG",
        side="NB",        # NB = buy, NS = sell
        order_type="LO",  # limit order
        quantity=100,
        price=27000.0,
    ))

    active = client.orders.list("0003979888", marketType="STOCK", orderCategory="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
    sec = client.market.security_info("HPG")
    print(sec.ceiling_price, sec.floor_price)

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.2.0.tar.gz (108.2 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.2.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dnse-0.2.0.tar.gz
Algorithm Hash digest
SHA256 57582ddf2e9c583a7fd9f127b1114d5dc0037e3e6bb0ea6deebb44d0f97ee2ad
MD5 696c265ee8f18bdd979b61cdc81c78df
BLAKE2b-256 b1e13193214c53638776c5c581e064445c3265533111cc26824cb3b6672eb3a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for dnse-0.2.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: dnse-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.7 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23fd999095b8431d7f13cc9566c054adbfd5a760f0a2dab903499b4a87270256
MD5 b7ac41ed5b733d7634475b98e9993f71
BLAKE2b-256 ccf57e43df66dddb1debc17c1ba2e747c4490666cdfcbc8bcf0d476ad3490c77

See more details on using hashes here.

Provenance

The following attestation bundles were made for dnse-0.2.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