This guide will help you get started with Yarasp in just a few minutes.
Set the YARASP_API_KEY environment variable:
export YARASP_API_KEY='your-api-key-here'
Or set it in your Python code:
import os
os.environ['YARASP_API_KEY'] = 'your-api-key-here'
from yarasp import YaraspClient
client = YaraspClient()
from yarasp import AsyncYaraspClient
client = AsyncYaraspClient()
# Synchronous
results = client.search(params={
"from": "c213", # Moscow station code
"to": "c2", # Saint Petersburg station code
"date": "2024-01-15"
})
print(f"Found {len(results)} routes")
for route in results[:3]: # Show first 3 routes
print(f"- {route.get('thread', {}).get('title', 'Unknown')}")
# Asynchronous
import asyncio
async def main():
results = await client.search(params={
"from": "c213",
"to": "c2",
"date": "2024-01-15"
})
print(f"Found {len(results)} routes")
asyncio.run(main())
# Get schedule for a station
schedule = client.schedule(params={
"station": "s9600213",
"date": "2024-01-15"
})
print(f"Schedule entries: {len(schedule)}")
# Find nearest stations to coordinates
stations = client.nearest_stations(params={
"lat": 55.7558,
"lng": 37.6173,
"distance": 50
})
print(f"Found {len(stations)} nearby stations")
Yarasp provides convenient methods for all Yandex Schedule API endpoints:
search() - Search for routes between stationsschedule() - Get station schedulethread() - Get route thread informationnearest_stations() - Find nearest stations by coordinatesnearest_settlement() - Find nearest settlement by coordinatescarrier() - Get carrier informationstations_list() - Get list of stationscopyright() - Get copyright informationAll methods support automatic pagination for endpoints that return paginated results.
You can customize the client behavior:
client = YaraspClient(
verbose=True, # Enable verbose logging
safe_mode=True, # Enable safe mode (default: True)
daily_limit=500, # Daily API request limit (default: 500)
cache_enabled=True, # Enable HTTP caching (default: True)
cache_storage=None, # Custom cache storage backend
)
See the Configuration page for all available options.
You can check if the last response came from cache:
result = client.search(params={"from": "c213", "to": "c2"})
if client.is_from_cache():
print("Response was served from cache!")
else:
print("Response was fetched from API")
The client will raise exceptions when:
RuntimeErrorRuntimeErrorhttpx.HTTPErrorfrom yarasp import YaraspClient
import httpx
try:
client = YaraspClient()
results = client.search(params={"from": "c213", "to": "c2"})
except RuntimeError as e:
print(f"Configuration error: {e}")
except httpx.HTTPError as e:
print(f"Network error: {e}")