What is a Session Key and how to generate it for using Breeze API?
A session key is a temporary key that is only used once, during one stretch of time, for encrypting and decrypting data. This is similar to One Time Passwords (OTP) that have to be generated once in the morning to login, and help in encrypting a communication session for each app separately. This key or token expires in 24 hours after generation or at midnight.
How can you generate your Session Key?
- Login with your ICICIdirect Account on https://api.icicidirect.com/apiuser/home and then navigate to ‘View Apps’ tab.
- Select the app for which you want to generate the Session Key and copy its API Key.
API Key: K%834G9%)2M9z75111V2dP
Secret Key: a69+y9097T9r279342xC513O82#ucU1a0
Redirect URL: https://127.0.0.1
#Note: API key has to be encoded in a different format before it can be used to generate a session key.
You can encode your API key by using any of the encoder sites freely available on the internet. Simply search for a URL encoder on your browser as shown below:
After you open the Decoder/Encoder website (we have used the one provided by meyerweb.com as an example), you will see a screen as shown below. Paste your API key in the given space and click on encode. The new encoded key will appear in the same space.
The new encoded API key in our example is: K%25834G9%25)2M9z75111V2dP%20
#Note: Use the new encoded key in the URL only as mentioned below:
3. Open a new tab, and switch to Developer Tools (on Chrome, Firefox, and Edge use the keyboard shortcut: Ctrl+Shift+I)
- Open the URL:
https://api.icicidirect.com/apiuser/login?api_key= in your browser
Using our above example, the URL should look like:
Place the above URL in your browser as shown below:
#Note that the URL changes to : https://api.icicidirect.com/apiuser/tradelogin after you press Enter
- Give ICICIdirect account credentials as usual and submit.
- Your browser screen should appear like the one shown below:
#Note that the URL has changed again to the Re-direct URL you mentioned while registering the App. In our example it is 127.0.0.1.
- In the Network Log table, find the resource with the same name as the redirect URL you gave when creating the App. For example, if you gave the redirect URL as https://127.0.0.1, look for a row named “127.0.0.1”.Click the row and in the new split, click tab named “Payload” → ”Form Data” → ”API_Session”.
Against “API_Session” will be an alpha-numeric (could be only numbers or only alphabets) token. This is your Session Key for that app. For example: in the above image, the session key is 1117193.
After completing the above process, you have successfully generated the Session Key, which can be used to start using all features of Breeze API. Please note that session key is valid for 24hrs or before midnight, whichever is earlier. You will have to generate a new session key for the next trading day. Though registering an App is not required for every trading session, make sure the status of your App is active before you generate session key for it.
Congratulations, you can start coding your strategy on an IDE (Jupyter, Spyder etc.) now! A demo code to start using Breeze API can be seen below:
#install Breeze Python SDK when using Breeze API for the first time
pip install --upgrade breeze-connect
#use your Breeze App credentials to set up the connection
from breeze_connect import BreezeConnect
isec = BreezeConnect(api_key="K%834G9%)2M9z75111V2dP")
# get historical data for option premium for a particular strike price and underlying data
data = isec.get_historical_data(interval="5minute",
put_data = pd.DataFrame(data["Success"])
The above code will:
- Install the latest version of Breeze Python SDK.
- It will then establish a connection using your Breeze App credentials.
- The third block of code, downloads a 5 minute historical data for 22nd Apr 2022 and stores it in an excel named “ITC data” in your system.
Please visit the Breeze community forum and the Breeze SDK package to know more on Breeze API usage. The links for community page and the SDK package are: