Authenticating in Python

You can authenticate with the Refinitiv Data Library for Python directly inline using a function in the package’s session module.

It requires that provide your application key, as well as the username and password you use to access the Refinitiv web portal. In this example, they will be stored in environment variables to avoid exposing private information in the source code.

import os
import refinitiv.data as rd

session = rd.session.platform.Definition(
    app_key=os.getenv('RDP_APP_KEY'),
    grant=rd.session.platform.GrantPassword(
        username=os.getenv('RDP_USERNAME'),
        password=os.getenv('RDP_PASSWORD')
    )
).get_session()
/home/runner/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/session/platform.py:90:FutureWarning: 
You open a platform session using the default value of the signon_control parameter (signon_control=True).
In future library version v2.0, this default will be changed to False.
If you want to keep the same behavior as today, you will need to set the signon_control parameter to True either in the library configuration file
({'sessions':{'platform':{'your_session_name':{'signon_control':true}}}}) or in your code where you create the Platform Session.
These alternative options are already supported in the current version of the library.

Once that’s successful, you can open a session:

session.open()
<OpenState.Opened: 'Opened'>

Then it as the default for the library.

rd.session.set_default(session)

Verify the session is open by executing a query for the current price of Thomson Reuters stock. You should back a table of data.

rd.get_history('TRI.N')
TRI.N TRDPRC_1 HIGH_1 LOW_1 ACVOL_UNS OPEN_PRC BID ASK TRNOVR_UNS VWAP BLKCOUNT BLKVOLUM NUM_MOVES TRD_STATUS SALTIM
Date
2024-03-28 155.83 157.6 155.03 136750 156.17 155.69 155.77 21297952 155.7437 1 38347 1451 1 72600
2024-04-01 155.3 155.84 154.07 49537 155.84 155.24 155.25 7684388 155.1242 1 18664 783 1 72600
2024-04-02 152.73 154.26 152.12 74566 153.79 152.67 152.73 11394046 152.8048 1 23812 992 1 72600
2024-04-03 152.44 153.49 151.98 59196 152.08 152.36 152.44 9032983 152.5945 1 22021 839 1 72600
2024-04-04 152.15 153.71 151.31 144144 153.12 152.05 152.15 21929318 152.1348 1 89214 878 1 72600
2024-04-05 151.94 152.91 151.72 73655 151.76 151.93 152.0 11198767 152.0435 1 18916 901 1 72600
2024-04-08 153.97 154.2 152.25 58333 152.25 153.93 153.94 8971913 153.8051 1 16132 810 1 72600
2024-04-09 154.54 154.59 152.28 63643 154.18 154.54 154.59 9813995 154.2038 1 13254 725 1 72600
2024-04-10 153.17 153.69 152.14 75560 153.35 153.17 153.21 11577309 153.2201 1 25115 807 1 72600
2024-04-11 154.55 154.66 151.89 80012 153.93 154.54 154.55 12323739 154.0236 1 16442 979 1 72600
2024-04-12 152.38 154.02 152.18 114429 153.27 152.3 152.31 17457709 152.5637 1 14924 1395 1 72600
2024-04-15 152.55 154.71 152.41 72123 153.47 152.62 152.63 11035400 153.0081 1 23057 1018 1 72600
2024-04-16 152.29 153.15 151.88 70968 152.74 152.29 152.41 10818902 152.4476 1 19346 882 1 72600
2024-04-17 152.85 153.72 151.56 67852 153.19 152.85 152.9 10371667 152.8572 1 17920 844 1 72600
2024-04-18 150.79 153.05 150.42 91115 153.05 150.76 150.77 13784004 151.2814 1 17998 1203 1 72600
2024-04-19 150.22 151.63 149.55 89600 150.79 150.22 150.33 13481424 150.4623 1 16898 1359 1 72600
2024-04-22 152.99 153.99 151.14 74409 151.2 152.99 153.0 11372959 152.8439 1 19596 927 1 72600
2024-04-23 153.59 154.61 153.28 72989 153.9 153.54 153.64 11219982 153.7216 1 19619 846 1 72600
2024-04-24 152.63 155.12 152.16 70534 153.56 152.63 152.71 10792980 153.0181 1 22383 801 1 72600
2024-04-25 152.57 153.11 150.07 77710 151.05 152.57 152.58 11843493 152.4063 1 17395 918 1 72600

The library expects you to close your session when you’re finished. You can do so by running the following code:

rd.close_session()