API endpoint error

Hi,

I am using Fyers v3 but I think the end point has changed. I use the auto login code. Can you please advice where to change this? I tried v3 instead of v2 in base url but no luck.

#setup session with totp

API endpoints

BASE_URL = “https://api-t2.fyers.in/vagator/v2

BASE_URL_2 = “https://api.fyers.in/api/v2

URL_SEND_LOGIN_OTP = BASE_URL + “/send_login_otp”

URL_VERIFY_TOTP = BASE_URL + “/verify_otp”

URL_VERIFY_PIN = BASE_URL + “/verify_pin”

URL_TOKEN = BASE_URL_2 + “/token”

URL_VALIDATE_AUTH_CODE = BASE_URL_2 + “/validate-authcode”

SUCCESS = 1

ERROR = -1

def send_login_otp(fy_id, app_id):

try:

	payload = {

		"fy_id": fy_id,

		"app_id": app_id

	}

	result_string = [requests.post](http://requests.post)(url=URL_SEND_LOGIN_OTP, json=payload)

	if result_string.status_code != 200:

		return [ERROR, result_string.text]

	result = json.loads(result_string.text)

	request_key = result["request_key"]

	return [SUCCESS, request_key]

except Exception as e:

	return [ERROR, e]

def generate_totp(secret):

try:

	generated_totp = pyotp.TOTP(secret).now()

	return [SUCCESS, generated_totp]

except Exception as e:

	return [ERROR, e]

def verify_totp(request_key, totp):

try:

	payload = {

		"request_key": request_key,

		"otp": totp

	}

	result_string = [requests.post](http://requests.post)(url=URL_VERIFY_TOTP, json=payload)

	if result_string.status_code != 200:

		return [ERROR, result_string.text]

	result = json.loads(result_string.text)

	request_key = result["request_key"]

	return [SUCCESS, request_key]

except Exception as e:

	return [ERROR, e]

def verify_PIN(request_key, pin):

try:

	payload = {

		"request_key": request_key,

		"identity_type": "pin",

		"identifier": pin

	}

	result_string = [requests.post](http://requests.post)(url=URL_VERIFY_PIN, json=payload)

	if result_string.status_code != 200:

		return [ERROR, result_string.text]

	result = json.loads(result_string.text)

	access_token = result["data"]["access_token"]

	return [SUCCESS, access_token]

except Exception as e:

	return [ERROR, e]

def token(fy_id, app_id, redirect_uri, app_type, access_token):

try:

	payload = {

		"fyers_id": fy_id,

		"app_id": app_id,

		"redirect_uri": redirect_uri,

		"appType": app_type,

		"code_challenge": "",

		"state": "sample_state",

		"scope": "",

		"nonce": "",

		"response_type": "code",

		"create_cookie": True

	}

	headers={'Authorization': f'Bearer {access_token}'}

	result_string = [requests.post](http://requests.post)(

		url=URL_TOKEN, json=payload, headers=headers

	)

	if result_string.status_code != 308:

		return [ERROR, result_string.text]

	result = json.loads(result_string.text)

	url = result["Url"]

	auth_code = parse_qs(urlparse(url).query)['auth_code'][0]

	return [SUCCESS, auth_code]

except Exception as e:

	return [ERROR, e]

def validate_authcode(app_id_hash, auth_code):

try:

	payload = {

		"grant_type": "authorization_code",

		"appIdHash": app_id_hash,

		"code": auth_code,

	}

	result_string = [requests.post](http://requests.post)(url=URL_VALIDATE_AUTH_CODE, json=payload)

	if result_string.status_code != 200:

		return [ERROR, result_string.text]

	result = json.loads(result_string.text)

	access_token = result["access_token"]

	return [SUCCESS, access_token]

except Exception as e:

	return [ERROR, e]

#setup session automatically

for index, row in df.iterrows():

#if row['user'] != 'C0':

#	continue

# Client Info (ENTER YOUR OWN INFO HERE!! Data varies from users and app types)

[logging.info](http://logging.info)(row['user'] + ' ' + row['broker'])

FY_ID = row['login_id']  # Your fyers ID

APP_ID_TYPE = "2"  # Keep default as 2, It denotes web login

TOTP_KEY = row['code']  # TOTP secret is generated when we enable 2Factor TOTP from myaccount portal

PIN = row['yob']  # User pin for fyers account

APP_ID = row['api_key'][:-4]  # App ID from myapi dashboard is in the form appId-appType. Example - EGNI8CE27Q-100, In this code EGNI8CE27Q will be APP_ID and 100 will be the APP_TYPE

REDIRECT_URI = row['redirect_uri']  # Redirect url from the app.

APP_TYPE = "100"

APP_ID_HASH = hashlib.sha256((row['api_key']+":"+row['api_secret']).encode()).hexdigest()  # SHA-256 hash of appId-appType:appSecret



# Step 1 - Retrieve request_key from send_login_otp API

send_otp_result = send_login_otp(fy_id=FY_ID, app_id=APP_ID_TYPE)

#[logging.info](http://logging.info)(str(send_otp_result))

if send_otp_result[0] != SUCCESS:

	[logging.info](http://logging.info)(f"send_login_otp failure - {send_otp_result[1]}")

	break

#else:

#	[logging.info](http://logging.info)("send_login_otp success")



# Step 2 - Generate totp

generate_totp_result = generate_totp(secret=TOTP_KEY)

#[logging.info](http://logging.info)(str(generate_totp_result))

if generate_totp_result[0] != SUCCESS:

	[logging.info](http://logging.info)(f"generate_totp failure - {generate_totp_result[1]}")

	break

#else:

#	[logging.info](http://logging.info)("generate_totp success")



# Step 3 - Verify totp and get request key from verify_otp API

request_key = send_otp_result[1]

totp = generate_totp_result[1]

verify_totp_result = verify_totp(request_key=request_key, totp=totp)

#[logging.info](http://logging.info)(str(verify_totp_result))

if verify_totp_result[0] != SUCCESS:

	[logging.info](http://logging.info)(f"verify_totp_result failure - {verify_totp_result[1]}")

	break

#else:

#	[logging.info](http://logging.info)("verify_totp_result success")



# Step 4 - Verify pin and send back access token

request_key_2 = verify_totp_result[1]

verify_pin_result = verify_PIN(request_key=request_key_2, pin=PIN)

#[logging.info](http://logging.info)(str(verify_pin_result))

if verify_pin_result[0] != SUCCESS:

	[logging.info](http://logging.info)(f"verify_pin_result failure - {verify_pin_result[1]}")

	break

#else:

#	[logging.info](http://logging.info)("verify_pin_result success")



# Step 5 - Get auth code for API V2 App from trade access token

token_result = token(

	fy_id=FY_ID, app_id=APP_ID, redirect_uri=REDIRECT_URI, app_type=APP_TYPE,

	access_token=verify_pin_result[1]

)

#[logging.info](http://logging.info)(str(token_result))

if token_result[0] != SUCCESS:

	[logging.info](http://logging.info)(f"token_result failure - {token_result[1]}")

	break

#else:

#	[logging.info](http://logging.info)("token_result success")



# Step 6 - Get API V2 access token from validating auth code

auth_code = token_result[1]

validate_authcode_result = validate_authcode(

	app_id_hash=APP_ID_HASH, auth_code=auth_code

)

#[logging.info](http://logging.info)(str(validate_authcode_result))

if token_result[0] != SUCCESS:

	[logging.info](http://logging.info)(f"validate_authcode failure - {validate_authcode_result[1]}")

	break

#else:

#	[logging.info](http://logging.info)("validate_authcode success")

access_token = validate_authcode_result[1]

#[logging.info](http://logging.info)(f"access_token - {access_token}")

is_async = False

fyers = fyersModel.FyersModel(client_id=row['api_key'], token=access_token, log_path="C:/Users/arind/FyersAPI/Scheduled")

chk = fyers.get_profile()

[logging.info](http://logging.info)('profile: ' + str(chk))

Hey @arindam_ghosh ,

The V2 endpoints are no longer valid , please DM us your contact details so that we can help you regarding the login

I am able to solve using the youtube link

https://www.youtube.com/watch?v=Laf37xtcyU8

. I previously did it using

https://www.youtube.com/watch?v=hcnpYSxB3Fg

.

In summary, the initialisation should be as below.

API endpoints

BASE_URL = “https://api-t2.fyers.in/vagator/v2

#BASE_URL_2 = “https://api.fyers.in/api/v2

BASE_URL_2 = “https://api-t1.fyers.in/api/v3

URL_SEND_LOGIN_OTP = BASE_URL + “/send_login_otp”

#URL_SEND_LOGIN_OTP = BASE_URL + “/send_login_otp_v3” #https://api-t2.fyers.in/vagator/v2/send_login_otp_v3 (this is based on 2nd video but not working, not sure why)

URL_VERIFY_TOTP = BASE_URL + “/verify_otp” #https://api-t2.fyers.in/vagator/v2/verify_otp

URL_VERIFY_PIN = BASE_URL + “/verify_pin”

#URL_VERIFY_PIN = BASE_URL + “/verify_pin_v2” #https://api-t2.fyers.in/vagator/v2/verify_pin_v2 (this is based on 2nd video but not working, not sure why)

URL_TOKEN = BASE_URL_2 + “/token” #https://api-t1.fyers.in/api/v3/token

URL_VALIDATE_AUTH_CODE = BASE_URL_2 + “/validate-authcode”

Can you please explain how to get these end points? As it is not matching as explained by 2nd video. The 2nd video worked while migrating from V1 to V2.

Hi @arindam_ghosh

Is your issue resolved, I am facing the same issue

Hi @108085257174762172736, I am able to solve using the youtube link

https://www.youtube.com/watch?v=Laf37xtcyU8

.