Dear Upstox API Support Team,
I hope this message finds you well.
I am a developer actively testing a trading application in your Sandbox environment using the official upstox-python-sdk. I am facing a persistent and critical issue where all order placement calls are being rejected with a 401 Unauthorized error, and I have exhausted all possible client-side troubleshooting steps.
Summary of the Issue:
-
My script successfully connects to the Sandbox environment using the permanent access_token generated from my developer console. The initial client configuration is successful.
-
However, when the script attempts to place an order using the place_order function, the API server rejects the request with the specific error code UDAPI100050: Invalid token used to access API.
-
This is happening during live market hours.
My Sandbox App Details:
-
API Key (Client ID): af0ab2a6-a8d4-4984-a24a-cdf5eb057d2c
-
Redirect URI: https://127.0.0.1
-
User ID / UCC: [Your UCC ID, e.g., 761432]
Troubleshooting Steps Already Taken:
I have diligently followed all standard procedures to resolve this, without success:
-
Token Regeneration: I have regenerated the Sandbox access_token multiple times, most recently today, to ensure it is fresh and valid. The token’s expiry is well in the future. The error persists even with a brand new token.
-
Credential Verification: I have meticulously verified that the api_key and api_secret in my configuration match the Sandbox App in my console.
-
Environment Confirmation: My script is correctly configured to connect to the Sandbox host (https://api-sandbox.upstox.com) by setting ENVIRONMENT = “SANDBOX”.
-
SDK Version: I am using a recent version of the upstox-python-sdk, and the code structure for placing orders appears to be correct as per the library’s requirements.
Code Snippet for Order Placement:
For your reference, this is the core function call that is failing. The client object is successfully authenticated and configured before this call is made.
codePython
def place_order(client, instrument, transaction_type, order_type="MARKET", price=0):
"""A generalized function to place buy or sell orders."""
if not client or not instrument:
logging.error("Cannot place order, client or instrument is missing.")
return None
order_api = upstox_client.OrderApi(client)
try:
# This is the call that receives the 401 Unauthorized error
api_response = order_api.place_order(
body=upstox_client.PlaceOrderRequest(
quantity=int(instrument['lot_size']),
product="I",
validity="DAY",
price=price,
order_type=order_type,
instrument_token=instrument['instrument_key'],
transaction_type=transaction_type,
disclosed_quantity=0,
trigger_price=0,
is_amo=False
),
api_version="v2"
)
logging.info(f"{transaction_type} order placed for {instrument['trading_symbol']}. Order ID: {api_response.data.order_id}")
return api_response.data.order_id
except ApiException as e:
logging.error(f"Failed to place {transaction_type} order for {instrument['trading_symbol']}: {e}")
return None
Given that the initial client configuration succeeds but subsequent order calls fail with an “Invalid token” error, this strongly suggests a server-side issue, possibly related to the provisioning of my Sandbox account or a permissions mismatch on your backend for the order placement endpoints.
Could you please investigate the configuration and permissions for my Sandbox app and account to identify why a seemingly valid token is being rejected by the order placement API?
I have attached my full script, the latest error log, and a screenshot of my app configuration for your reference.
Thank you for your time and assistance in resolving this critical issue.
Best regards,
Vijay