Sandbox Not Working in Python SDK Execution

I am Trying to simulate my algorithm in Sandbox mode, i am trying to place a multi order and since sandbox is available for this multi order API.
As you can see i have made different logging lines to see whats happening, i went out of my way of hard coding the token also but it didnt work. somehow this initilization is not working for me and when i try to print the configuration.sandbox flag also, i get False output.

if SANDBOX_MODE:
        configuration = upstox_client.Configuration(sandbox=True)
        #Tried Manually Setting this flag            
        #configuration.sandbox = True
        configuration.access_token = SANDBOX_ACCESS_TOKEN
        logger.notice(f"Entry Order using Sandbox Token: {SANDBOX_ACCESS_TOKEN}")
else:
        configuration = upstox_client.Configuration()
        configuration.access_token = self.access_token
        logger.notice("Entry Order using Live Token")

logger.notice(f"Configuration Token used: {configuration.access_token}")
logger.notice(f"Configuration SANDBOX: {configuration.sandbox}")

order_api_instance = upstox_client.OrderApi(upstox_client.ApiClient(configuration))

try:
     logger.notice(f"Placing Hedge-Multi Order for {position_type} Position")
     entry_order_response = order_api_instance.place_multi_order(entry_order_body)
     logger.notice(entry_order_response)

logs:

2025-05-11 20:10:40 NOTICE : Configuration SANDBOX: False

Critical Error!! Exception when calling OrderApi->place_order: b’{ā€œstatusā€:ā€œerrorā€,ā€œerrorsā€:[{ā€œerrorCodeā€:ā€œUDAPI100050ā€,ā€œmessageā€:ā€œInvalid token used to access APIā€,ā€œpropertyPathā€:null,ā€œinvalidValueā€:null,ā€œerror_codeā€:ā€œUDAPI100050ā€,ā€œproperty_pathā€:null,ā€œinvalid_valueā€:null}]}’

More Information on the Issue:

The Below Code Demonstrates the Issue:

When we create a simple Configuration() object i.e no Sandbox Required. We get a object with no sandbox. The problem is in the same program if we create another object with the sandbox Flag, although the object created is a different one with a different id. The Sandbox Flag still remains false. I have pasted the output of the program below to refer.

Please look into this issue because during a complete test, we need to use Sandbox API and Live API also as not all have Sandbox Enabled and this mix of calls can lead to this issue.

from upstox_client import Configuration

# Create first configuration (sandbox)
config1 = Configuration()
config1.access_token = "sandbox_token_1"


# Create second configuration (sandbox)
config2 = Configuration(sandbox=True)
config2.access_token = "sandbox_token_2"

# Log object IDs and tokens
print(f"Config 1 ID: {id(config1)}, Sandbox: {config1.sandbox}, Token: {config1.access_token}")
print(f"Config 2 ID: {id(config2)}, Sandbox: {config2.sandbox}, Token: {config2.access_token}")

#Check if same object or not
if id(config1) == id(config2):
    print("Same Configuration object reused!")
else:
    print("Different Configuration objects created.")

Output:

Config 1 ID: 2200397190224, Sandbox: False, Token: sandbox_token_1
Config 2 ID: 2200397190544, Sandbox: False, Token: sandbox_token_2
Different Configuration objects created.

Hi @Raghav_35590666, you are correct. The SDK was designed such that a program can operate exclusively in either live or sandbox mode. Currently, it is not possible to have both modes running concurrently within a single instance.

This design decision was implemented due to compliance obligations. If a user operating a program in sandbox mode were to inadvertently switch to live mode, it could potentially lead to financial losses.

As a solution, it is necessary to have two distinct programs running to utilize live and sandbox modes separately.

Thank you.

Hi @Ketan, Thank You for the swift reply. I now understand the compliance requirement that may have led to the following design choice and why such precautions were taken.

I will try to work on other ways to test my application.

  • Thank You to you & the Team Behind the API
3 Likes