.0/ttarrivals.aspx?key=API_KEY&mapid=40380&outputType=JSON
The CTA API documentation is at />
It succeeds (HTTP status 200) in the browser, but in Python, using requests.get
, the server returns 403.
I tried with header and sessions but I still received a 403 status.
url = ".0/ttarrivals.aspx"
# Request parameters
params = {
'key': api_key,
'mapid': station_id,
'max': max_results,
'outputType': output_type
}
# Headers to mimic a browser
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'application/json',
}
try:
# Make the API request
response = requests.get(url, params=params, headers=headers)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
session = requests.Session()
session.headers.update(headers)
response = session.get(url)
https://lapi.transitchicago/api/1.0/ttarrivals.aspx?key=API_KEY&mapid=40380&outputType=JSON
The CTA API documentation is at https://www.transitchicago/developers/ttdocs/>
It succeeds (HTTP status 200) in the browser, but in Python, using requests.get
, the server returns 403.
I tried with header and sessions but I still received a 403 status.
url = "https://lapi.transitchicago/api/1.0/ttarrivals.aspx"
# Request parameters
params = {
'key': api_key,
'mapid': station_id,
'max': max_results,
'outputType': output_type
}
# Headers to mimic a browser
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'application/json',
}
try:
# Make the API request
response = requests.get(url, params=params, headers=headers)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
session = requests.Session()
session.headers.update(headers)
response = session.get(url)
Share
Improve this question
edited Mar 17 at 10:09
VLAZ
29.1k9 gold badges63 silver badges84 bronze badges
asked Mar 14 at 19:59
usamausama
157 bronze badges
2
|
1 Answer
Reset to default 0Given a valid API key the following will work without error.
import requests
import json
from apikeys import CTA_API_KEY
URL = "https://lapi.transitchicago/api/1.0/ttarrivals.aspx"
params = {
"key": CTA_API_KEY,
"mapid": 40380,
"outputType": "JSON",
"max": 1
}
with requests.Session() as session:
with session.get(URL, params=params) as response:
response.raise_for_status()
j = response.json()
print(json.dumps(j, indent=2))
Output:
{
"ctatt": {
"tmst": "2025-03-15T01:57:46",
"errCd": "0",
"errNm": null,
"eta": [
{
"staId": "40380",
"stpId": "30374",
"staNm": "Clark/Lake",
"stpDe": "Subway service toward Forest Park",
"rn": "225",
"rt": "Blue",
"destSt": "0",
"destNm": "UIC-Halsted",
"trDr": "5",
"prdt": "2025-03-15T01:57:37",
"arrT": "2025-03-15T02:00:37",
"isApp": "0",
"isSch": "0",
"isDly": "0",
"isFlt": "0",
"flags": null,
"lat": "41.89119",
"lon": "-87.64758",
"heading": "123"
}
]
}
}
Note that this API doesn't require explicit headers
'Accept': 'application/json',
. – Yaniek Commented Mar 14 at 21:14