How to automate CDSL authorisation

Hi, I am trying to automate the TPIN authorisation for which I am trying to use the /edisbulk/form API. I am able to get the HTML form but how to render it and submit the TPIN?

Trying to use Selenium with python for this use case but since this is HTML code, I am not able to render it. Any help around this would be highly appreciated

I have unescaped using the following code but rendering and moving ahead to fill the TPIN is not working

PS: Currently I don’t have any holding in my account because I am doing development using API and don’t want to misfire anything

def renderForm(isin):
    instance_ = WebDriver()
    driver = instance_.get()
    url = "https://api.dhan.co/edisbulk/form"

    payload = {
        "isin": isin,
        "exchange": "NSE",
        "segment": "EQ"
    }
    headers = {
        "Content-Type": "application/json",
        "access-token": os.getenv('ACCESS_TOKEN')
    }
    otp = getEmailOTP()
    print(otp)
    response = requests.request("POST", url, json=payload, headers=headers)
    escaped_html = (json.loads(response.text))['edisFormHtml']
    print(html.unescape(escaped_html))

Hi team/@PravinJ , any help on this aspect would be really appreciated.

I’m getting the form but the API docs do not mention how to enter the tpin. Looking forward to your reply

Hi @sv28

You need to unescape this html form. This will lead to you on a cdsl page.Thereafter input tpin and OTP recieved on registered mobile number.

Hope this helps.

Hi @Naman, I’ve written this snippet in my function to write the escaped HTML.

    with open('form.html', 'w') as f:
        f.write(html.unescape(escaped_html))

On opening it manually, I am getting the following error. Please let me know what should I do? Also, if you have some code already working for this API, it would be really helpful for me

Hi @Naman , did you had a chance to check it out ?

Hello @sv28

It will be helpful for us to troubleshoot if you can share the HTML code for this CDSL page loaded, so that we know it has all the information as intended in the API response.

In addition to this, can you confirm if you are passing array of ISIN in the variable isin as visible in your code.

Hi @Hardik , here is my code: import htmlimport jsonimport osimport requestsfrom dotenv import load_do - Pastebin.com

Link to HTML: <!DOCTYPE html> <html> <script>window.onload= function(){submit()};function - Pastebin.com

@Hardik - had a chance to look at these^?

https://pastebin.com/qTZx9HhS

Hey @sv28

Apologies for the late reply, I might have skipped the thread. I did check the CDSL authorisation flow on our open APIs and it is working fine. On the code that you shared, it seems that the HTML was missing some file components, might be because of the libraries used to automate this.

I will request you to try open this once without automating OTP flow. Also, we will soon be adding entire eDIS API flow as part of our Python library, for ease of integration.

Hi @Hardik , I tried but it doesn’t seem to work. Do we have any ETA for EDIS Python library integration? It would be a life saver.

@Hardik it would be great if you could help me close this. It’s a blocker for me. I’ve my entire algo system on dhan, and am expecting this to be automated as well.

Any piece of code which you used for testing this would be helpful for me. Also, ETA for the addition of edis API to the python library would help me prioritise accordingly.

Hey @sv28

Sure, we are just working on the release. You can expect the same by next week with eDIS Flow and couple of other additions.

@Hardik Has the python library been updated?

Hey @sv28

The team is working on it and testing some cases. We will be releasing this post budget, you can expect this by week end.

Hey @sv28

We just released Python Library V1.2. It is updated on pypi and Github. You can upgrade package using

pip install dhanhq --upgrade
1 Like

Thanks @Hardik !

Do we need to run this function on loop for all the ISIN, or can we pass on the list? Basically, I want to approve for all the stocks I have in my holding

Yes @sv28

You can use a loop for now to add ISIN. We will soon evaluate adding Bulk ISIN form generation as well.

@Hardik do we have any ETA for bulk ISIN? The individual looping is not practical considering I have ~40 holdings any given point in time.

Also, is there any sandbox environment where I can see my APIs executing? Before deploying into production, I want to ensure that there are no misses. Currently, there is no way to see if the orders are getting placed without putting in real money, which is risky

Hello @sv28

We are planning this with the next release of the Python library. Since we are parallelly working to release Market Feed APIs as well with the next build, it is difficult to provide you with an ETA here.

There is no such sandbox to test APIs, however, you can try api.dhan.co to call APIs straight from your browser.

Got it @Hardik

I think some sought of sandbox environment is needed so that we can test our code without putting real money at stake. A simpler solution would be to add an optional parameter in the request named Production. By default, it can be True, but users can make it False in their request. With this flag disabled, orders would be sent to Dhan but not to exchange i.e. orders would temporarily be available on UI with Test tag.

This would enable us to not put real hard-earned money at stake without knowing if our code is working perfectly fine. Currently, it’s a big problem for me because a simple bug on my end can cause disaster.

For example: Recently for some reason Buy order of the same instrument was being placed on loop for the same price. Luckily, I found it before deploying it. However, if I would have missed this, my entire capital would have been deployed in a single stock. This could have been avoided with the above request.