0

I'm trying to connect to the XTS Market Data API using the xts_api_client's async client (xts_connect_async) in Python. Here's the code I'm using:

from xts_api_client.xts_connect_async import XTSConnect
from dotenv import load_dotenv
import os
import asyncio

load_dotenv()

async def main():
    market_data_API_key = os.getenv("market_data_API_key")
    market_data_API_secret = os.getenv("market_data_API_secret")
    API_source = os.getenv("API_source")
    API_root = os.getenv("API_root")

    connect = XTSConnect(market_data_API_key, market_data_API_secret, API_source, API_root)
    response = await connect.marketdata_login()
    print(response)

asyncio.run(main())

When I run this using either UV (Astral) or standard pip environment, I consistently get this error:

PS U:\xts> uv run .\xts.py
Traceback (most recent call last):
  File "U:\xts\.venv\Lib\site-packages\httpx\_transports\default.py", line 101, in map_httpcore_exceptions
    yield
  File "U:\xts\.venv\Lib\site-packages\httpx\_transports\default.py", line 394, in handle_async_request
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 207, in handle_async_request
    raise UnsupportedProtocol(
        "Request URL is missing an 'http://' or 'https://' protocol."
    )
httpcore.UnsupportedProtocol: Request URL is missing an 'http://' or 'https://' protocol.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "U:\xts\.venv\Lib\site-packages\xts_api_client\xts_connect_async.py", line 633, in marketdata_login
    response = await self._post("market.login", params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\xts_api_client\xts_connect_async.py", line 805, in _post
    return await self._request(route, "POST", params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\xts_api_client\xts_connect_async.py", line 841, in _request
    raise e
  File "U:\xts\.venv\Lib\site-packages\xts_api_client\xts_connect_async.py", line 836, in _request
    r = await self.reqsession.request(method = method, url = url,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<2 lines>...
                                headers=headers)
                                ^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\httpx\_client.py", line 1540, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\httpx\_client.py", line 1629, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
    )
    ^
  File "U:\xts\.venv\Lib\site-packages\httpx\_client.py", line 1657, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
    )
    ^
  File "U:\xts\.venv\Lib\site-packages\httpx\_client.py", line 1694, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\httpx\_client.py", line 1730, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\httpx\_transports\default.py", line 393, in handle_async_request
    with map_httpcore_exceptions():
         ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\parul.kakade\AppData\Local\Programs\Python\Python313\Lib\contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    ~~~~~~~~~~~~~~^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\httpx\_transports\default.py", line 118, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.UnsupportedProtocol: Request URL is missing an 'http://' or 'https://' protocol.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "U:\xts\xts.py", line 16, in <module>
    asyncio.run(main())
    ~~~~~~~~~~~^^^^^^^^
  File "C:\Users\parul.kakade\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "C:\Users\parul.kakade\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\parul.kakade\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "U:\xts\xts.py", line 14, in main
    response = await connect.marketdata_login()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "U:\xts\.venv\Lib\site-packages\xts_api_client\xts_connect_async.py", line 640, in marketdata_login
    return response['description']
           ^^^^^^^^
UnboundLocalError: cannot access local variable 'response' where it is not associated with a value`

What I’ve Tried I expected the marketdata_login() method from XTSConnect to successfully authenticate and return a JSON response with login status. I confirmed that all the required environment variables (API key, secret, source, root) are set correctly and contain valid values — especially the API_root, which starts with https://.

Here’s what I tried:

Printing all environment variables before creating the XTSConnect instance — they are all populated as expected.

Hardcoding the values instead of using os.getenv(), just to rule out .env loading issues.

Running the script in both pip and uv environments.

Trying different versions of xts-api-client, httpcore, and httpx.

Using Trio instead of async

Adding debug prints inside the library code (where possible) to trace the request URL.

Using the debugger in VSCode — surprisingly, it works consistently when run in debug mode.

I was expecting a consistent login response every time, but instead, I get unpredictable behavior depending on the environment.

Questions Why is this error occurring intermittently, why behavior is different in debugger and python environment? Is there a race condition or lazy loading behavior in XTSConnect or httpx that I might be missing?

How can I ensure API_root is correctly formatted and used every time?

1
  • The lack of proper code formatting makes it difficult for readers to help you. Please edit your question to format inline code properly and to use a code block for any code or error messages you have included. Commented Jun 25 at 6:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.