How to create a long lived token programmatically using web socket (username and password)

I’m having issues obtaining a long lived token programmatically. How can I obtain a long lived token when establishing a web socket connection using my username and password?

I’m able to obtain the token after a web socket connection is created, but I need a token to establish the connection first. Chicken and egg situation…

The below Node Red flow works fine once connected, but again how can I establish a web socket connection when I don’t have an existing long lived token to connect with initially?

NOTE - the assumption here is headless, meaning the application needs to perform this action using the API. I don’t want to manually log into the UI to create the long lived token. I want to create the token using my username and password via the API.

If I understood correctly, you want to login with username/password and generate long lived token.

I think, we don’t want credentials (password) to move around freely and that is because you should generate the first token yourself. Checking the floe diagram below page might help

Thanks for the quick reply, but I already looked through the documentation. My issue here is that I want to automatically provision a remote Node Red system with a new long lived token without logging into the HA UI to create the initial long lived token. There is got to be a way to do this… Any suggestions?

There’s an official JS websocket client that will handle the auth for you.

Check out: