[Solved] how pass variables in api calll python?


You can use params in requests to use arguments in url

import requests

data = ["AAPL", "SQ", "PLTR"]
data_str = ",".join(data)

url = "https://stocknewsapi.com/api/v1"

payload = {
    "tickers": data_str,
    "items": 50,
    "date": "last7days",
    "token": "myapikey",
}

response = requests.get(url, params=payload)

Eventually you can use string formatting with {} and .format(data_str)

data_str = ",".join(data)

url = "https://stocknewsapi.com/api/v1?tickers={}&items=50&date=last7days&token=myapikey".format(data_str)

or f-string with {data_str}

data_str = ",".join(data)

url = f"https://stocknewsapi.com/api/v1?tickers={data_str}&items=50&date=last7days&token=myapikey"

solved how pass variables in api calll python?