*** Settings ***
Library RequestsLibrary
Library Collections
Library OperatingSystem
Library BuiltIn
Library WebSocketLibrary # pip install robotframework-websocket
# Note: Use environment variables for secrets in real usage
*** Variables ***
${FYERS_BASE} [Link] # replace w/ official URL if
different
${AUTH_BASE} [Link] # may be same or
[Link]
${CLIENT_ID} YOUR_CLIENT_ID
${SECRET_KEY} YOUR_SECRET_KEY
${REDIRECT_URI} [Link] # must match app
${AUTH_CODE} ${EMPTY} # manually paste auth code if
needed
${ACCESS_TOKEN} ${EMPTY}
${REFRESH_TOKEN} ${EMPTY}
${SYMBOL_TOKEN} 256265 # example symbol token -
replace
${WS_URL} [Link] # replace by docs
*** Keywords ***
Create FYERS Session
[Documentation] Create a requests session for FYERS APIs
Create Session fyers ${FYERS_BASE} headers={"Content-
Type":"application/json"}
Get Auth URL
[Documentation] Returns the authorization URL to visit (open in browser
manually)
${url}= Set Variable ${AUTH_BASE}/api/v3/authorize?client_id=$
{CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&state=xyz
[Return] ${url}
Exchange Auth Code For Tokens
[Arguments] ${auth_code}
[Documentation] Exchange auth code for access_token & refresh_token.
# FYERS docs: POST to token generation endpoint (check /docsv3 Authentication
section)
${payload}= Create Dictionary grant_type=authorization_code code=$
{auth_code} client_id=${CLIENT_ID} client_secret=${SECRET_KEY}
redirect_uri=${REDIRECT_URI}
${resp}= POST On Session fyers /api/v3/token json=${payload}
Should Be Equal As Integers ${resp.status_code} 200
${body}= To Json ${[Link]}
Set Suite Variable ${ACCESS_TOKEN} ${body['access_token']}
Set Suite Variable ${REFRESH_TOKEN} ${body['refresh_token']}
Log Got access token (length=${len(${ACCESS_TOKEN})})
Refresh Access Token
[Documentation] Use refresh token to get a new access token
${payload}= Create Dictionary grant_type=refresh_token refresh_token=$
{REFRESH_TOKEN} client_id=${CLIENT_ID} client_secret=${SECRET_KEY}
${resp}= POST On Session fyers /api/v3/token json=${payload}
Should Be Equal As Integers ${resp.status_code} 200
${body}= To Json ${[Link]}
Set Suite Variable ${ACCESS_TOKEN} ${body['access_token']}
Set Suite Variable ${REFRESH_TOKEN} ${body['refresh_token']}
Log Refreshed tokens.
Add Auth Header
[Documentation] Adds Authorization header for subsequent requests in the
session
${hdr}= Create Dictionary Authorization=Bearer ${ACCESS_TOKEN}
Content-Type=application/json
Delete All Headers From Session fyers
Set Headers fyers ${hdr}
Get LTP
[Arguments] ${symbol_token}=${SYMBOL_TOKEN}
Add Auth Header
${resp}= GET On Session fyers /api/v3/quotes/${symbol_token}
Should Be Equal As Integers ${resp.status_code} 200
${body}= To Json ${[Link]}
[Return] ${body}
Place Order
[Arguments] ${symbol_token}=${SYMBOL_TOKEN} ${qty}=1 ${price}=0 $
{order_type}=MARKET ${transaction_type}=BUY ${product_type}=INTRADAY
Add Auth Header
# Example body - check FYERS doc for exact fields and names
${order}= Create Dictionary
... symbol=${symbol_token}
... qty=${qty}
... type=${order_type}
... side=${transaction_type}
... productType=${product_type}
... limitPrice=${price}
${resp}= POST On Session fyers /api/v3/orders json=${order}
Should Be Equal As Integers ${resp.status_code} 200
${body}= To Json ${[Link]}
Log Order response: ${body}
[Return] ${body}
Cancel Order
[Arguments] ${order_id}
Add Auth Header
${resp}= DELETE On Session fyers /api/v3/orders/${order_id}
Should Be Equal As Integers ${resp.status_code} 200
${body}= To Json ${[Link]}
[Return] ${body}
Subscribe Websocket
[Arguments] ${symbols} ${mode}=lite
[Documentation] Connect to FYERS data websocket and subscribe to symbol
list.
# Use robotframework-websocket library
Open WebSocket ${WS_URL}
${sub_msg}= Create Dictionary type=subscribe symbols=${symbols}
mode=${mode}
${json}= Convert To String ${sub_msg}
Send WebSocket Message ${json}
Log Sent subscribe: ${json}
# Use Receive WebSocket Message to get updates
Receive WS Message
${msg}= Receive WebSocket Message timeout=10
Log WS msg: ${msg}
[Return] ${msg}
Close WS
Close WebSocket