How to access Indian Railway train ticket data via an API using IRCTC (Irctc API program)
To access Indian Railway train ticket data via an API using **IRCTC**, you would typically rely on the Indian Railways Catering and Tourism Corporation (IRCTC) APIs. However, direct access to IRCTC's train ticket booking APIs is usually restricted and requires official permission or partnerships due to security and privacy concerns. You can use the **Indian Railway API** provided by **IRCTC** or third-party services for train information, PNR status, and seat availability.
### Example: Using a third-party Train Ticket API Program
You can use **third-party APIs** like **RailwayAPI** (by **RailwayEnquiry.org**) or **RapidAPI** to get train-related data. Here's an example of how you can write a program in **Python** to interact with a train ticket API.
### Steps:
1. Sign up on a platform like **RapidAPI** or **RailwayAPI** and get an API key.
2. Use endpoints like `Train Between Stations`, `PNR Status`, `Seat Availability`, etc.
### Sample Python Program:
```python
import requests
# Replace with your actual API key
API_KEY = 'your_api_key'
# API endpoint for train between stations
url = "https://indianrailways.p.rapidapi.com/trainsBetweenStations"
# Query parameters: fromStation (source), toStation (destination), and date of journey
querystring = {
"fromStationCode": "NDLS", # New Delhi Station code
"toStationCode": "BCT", # Mumbai Central station code
"date": "2024-09-20" # Date in yyyy-mm-dd format
}
# Request headers including the API key
headers = {
"X-RapidAPI-Key": API_KEY,
"X-RapidAPI-Host": "indianrailways.p.rapidapi.com"
}
# Make the API request
response = requests.get(url, headers=headers, params=querystring)
# Check if the request was successful
if response.status_code == 200:
data = response.json()
# Process the train data
for train in data['trains']:
print(f"Train Name: {train['name']}, Train Number: {train['number']}")
print(f"Departure: {train['departureTime']}, Arrival: {train['arrivalTime']}")
print(f"From: {train['fromStation']['name']}, To: {train['toStation']['name']}")
else:
print(f"Error: {response.status_code}")
```
### Explanation:
- **API Key**: Obtain from platforms like **RapidAPI** or **RailwayAPI**.
- **Query Parameters**: You can specify the source and destination stations using their station codes and the date of travel.
- **Response**: The API returns a list of trains, along with details like train name, number, departure and arrival times.
### API Endpoints (Examples):
- **Trains Between Stations**: Fetches all available trains between two stations.
- **PNR Status**: Get the status of a reserved ticket using PNR.
- **Seat Availability**: Check seat availability on a specific train on a given date.
You can extend this program for booking tickets, checking train schedules, or other train-related queries, provided you have the necessary permissions and API access from IRCTC.
Would you like help with another programming language or with implementing a different API?
No Comments have been Posted.